Hi.
I want to generate a document automatically. The document should contain some images.
I first want to draw a image all by coding, not import a image from an external file. But that goal was a little hard. So I want to import a external image file, and only add some text to it. Then I want to insert this image into a RichTextBox?
Here is what I do:
I create a new form project and add 1 Picturebox, 1 RichTextBox and 3 buttons(btnGen to generate the document, btnSave to save the document as a .rtf file and btnImport to import a external image file).
This is the function to import a external image file to my PictureBox.
Private Sub ImportPicture(ByVal PictureName As String, ByRef PicBox As PictureBox)
Try
Dim img As Image = Image.FromFile(PictureName)
PicBox.Image = img
Catch ex As Exception
Console.WriteLine(vbCrLf + ex.StackTrace + vbCrLf + ex.Message)
End Try
End Sub
Double click the btnImport button. Now I want to import the external file, and then I want to draw some words on it. Here is what I can not do.
Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
Dim ImagePath As String = My.Application.Info.DirectoryPath + "\sample.png"
ImportPicture(ImagePath, Me.PictureBox1)
End Sub
Double click the btnGen button to generate the document automatically.
Private Sub btnGen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGen.Click
Me.WindowState = FormWindowState.Maximized
Me.RichTextBox1.SetBounds(15, 50, Me.Width - 45, Me.Height - 90)
Me.PictureBox1.Visible = False
GenerateDoc(Me.RichTextBox1)
End Sub
And these are the two funtions to add text and image to the doucment.
Private Sub GenerateDoc(ByRef RtfBox As RichTextBox)
RtfBox.SelectionAlignment = HorizontalAlignment.Center
RtfBox.SelectedText = "Tittle" + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Left
RtfBox.SelectedText = "First texts" + vbCrLf
InsertPic2Doc(Me.PictureBox1)
RtfBox.SelectedText = vbCrLf
RtfBox.SelectedText = "This is the second line of the texts" + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Center
InsertPic2Doc(Me.PictureBox1)
RtfBox.SelectedText = vbCrLf
RtfBox.SelectedText = "Fig.2" + vbCrLf
RtfBox.SelectionAlignment = HorizontalAlignment.Left
RtfBox.SelectedText = "This is the third line"
End Sub
Private Sub InsertPic2Doc(ByRef PicBox As PictureBox)
Clipboard.Clear()
Clipboard.SetImage(PicBox.Image)
RichTextBox1.Paste()
End Sub