What is PDF

PDF (Portable Document Format) is one kind of electronic document format. PDF is the ideal document format to publish electronic document and deliver digital information on internet for it has nothing to do with operation system. PDF file format can package font, format, color and image in a file. Also, it can include hyperlink, voice and video. What's more, it supports long file. And the most important is that it has high security and integration.

Why Convert Word to PDF

However, we can't edit the PDF file so that we need to convert PDF to other format file to edit it. As the same, if we don't want other to edit our Word document, we may convert Word to PDF for PDF can remain the format which has been set in Word document. But how should we do to convert Word to PDF with C#/VB.NET?

How to Convert Word to PDF via Spire.Doc

Spire.Doc presents you an easy way to convert Word to PDF. In this way, you should edit your document, after you finish writing, you can get your PDF file by using SaveToFile("Sample.pdf", FileFormat.PDF) method to convert the document to PDF format.

The following code displays the way to convert Word to PDF:

using System;

using Spire.Doc;

using Spire.Doc.Documents;

namespace WordToPDF

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a new document 

            Document document = new Document();

            //Create a section

            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;

            //Add a paragraph

            Paragraph paragraph = section.AddParagraph();

            paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;

            //Add text in the paragraph

            String p1 = "Microsoft Word is a word processor designed by Microsoft.”;

            String p2 = "Microsoft Office Word instead of merely Microsoft Word. ";

            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);

            //Launching the pdf reader to open.

            System.Diagnostics.Process.Start("Sample.pdf");

    }

}

 

Visual Basic

 

Imports System

Imports Spire.Doc

Imports Spire.Doc.Documents

Module Module1

    Sub Main()

        'Create a new document

        Dim document As New Document()

        'Create a section

        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

        'Add a paragraph 

        Dim paragraph As Paragraph = section.AddParagraph()

        paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left

        'Add text in the paragraph

        Dim p1 As String = "Microsoft Word is a word processor designed by Microsoft."

        Dim p2 As String = "Microsoft Office Word instead of merely Microsoft Word."

        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)

        'Launching the MS Word file.

        Process.Start("Sample.pdf")

    End Sub

End Module

    }

        }

}



After running the demo, you may find a PDF document launched on your computer: