Word to pdf in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

The sample demonstrates how to export doc document to PDF file.

//Create word document
Document document = new Document();

Section section = document.AddSection();
section.PageSetup.PageSize = PageSize.A4;
section.PageSetup.Margins.Top = 72f;
section.PageSetup.Margins.Bottom = 72f;
section.PageSetup.Margins.Left = 89.85f;
section.PageSetup.Margins.Right = 89.85f;

Paragraph paragraph = section.AddParagraph();
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
paragraph.AppendPicture(Image.FromFile("Word.png"));

String p1
    = "Microsoft Word is a word processor designed by Microsoft. "
    + "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. "
    + "Subsequent versions were later written for several other platforms including "
    + "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), "
    + "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). ";
String p2
    = "Microsoft Office Word instead of merely Microsoft Word. "
    + "The 2010 version appears to be branded as Microsoft Word, "
    + "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac.";
section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14;
section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14;

//Save doc file to pdf.
document.SaveToFile("Sample.pdf", FileFormat.PDF);

'Create word document
Dim document As New Document()

Dim section As Section = document.AddSection()
section.PageSetup.PageSize = PageSize.A4
section.PageSetup.Margins.Top = 72.0F
section.PageSetup.Margins.Bottom = 72.0F
section.PageSetup.Margins.Left = 89.85F
section.PageSetup.Margins.Right = 89.85F

Dim paragraph As Paragraph = section.AddParagraph()
paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left
paragraph.AppendPicture(Image.FromFile("Word.png"))

Dim p1 As String _
    = "Microsoft Word is a word processor designed by Microsoft. " _
    + "It was first released in 1983 under the name Multi-Tool Word for Xenix systems. " _
    + "Subsequent versions were later written for several other platforms including " _
    + "IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), " _
    + "Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989). "
Dim p2 As String _
    = "Microsoft Office Word instead of merely Microsoft Word. " _
    + "The 2010 version appears to be branded as Microsoft Word, " _
    + "once again. The current versions are Microsoft Word 2010 for Windows and 2008 for Mac."
section.AddParagraph().AppendText(p1).CharacterFormat.FontSize = 14
section.AddParagraph().AppendText(p2).CharacterFormat.FontSize = 14

'Save doc file to pdf.
document.SaveToFile("Sample.pdf", FileFormat.PDF)