Word Image in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to insert an image into a Word document.

private void button1_Click(object sender, EventArgs e)
{
    //Create word document
    Document document = new Document();

    InsertImage(document.AddSection());

    //Save doc file.
    document.SaveToFile("Sample.doc",FileFormat.Doc);

    //Launching the MS Word file.
    WordDocViewer("Sample.doc");


}

private void InsertImage(Section section)
{
    Paragraph paragraph = section.AddParagraph();
    paragraph.AppendText("The sample demonstrates how to insert a image into a document.");
    paragraph.ApplyStyle(BuiltinStyle.Heading2);

    paragraph = section.AddParagraph();
    paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
    paragraph.AppendPicture(Image.Properties.Resources.Word);
}

private void WordDocViewer(string fileName)
{
    try
    {
        System.Diagnostics.Process.Start(fileName);
    }
    catch { }
}

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
	'Create word document
	Dim document_Renamed As New Document()

	InsertImage(document_Renamed.AddSection())

	'Save doc file.
	document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)

	'Launching the MS Word file.
	WordDocViewer("Sample.doc")


End Sub

Private Sub InsertImage(ByVal section_Renamed As Section)
	Dim paragraph_Renamed As Paragraph = section_Renamed.AddParagraph()
	paragraph_Renamed.AppendText("The sample demonstrates how to insert a image into a document.")
	paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)

	paragraph_Renamed = section_Renamed.AddParagraph()
	paragraph_Renamed.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left
	paragraph_Renamed.AppendPicture(My.Resources.Word)
End Sub

Private Sub WordDocViewer(ByVal fileName As String)
	Try
		Process.Start(fileName)
	Catch
	End Try
End Sub