Convert Word to PDF Easily with C#/VB.NET

PDF Introduction

PDF (Portable Document Format), developed by Adobe, has been become one of the most frequently used electronic document format now.

Why is PDF so popular? Firstly, PDF has nothing to do with operation system. It means that it can work well on Windows, Mac and Unix. This function enables PDF to be the best choice to publish electronic document and deliver digital information online. Secondly, PDF can reproduce characters, colors and images in original data exactly because it is based on PostScript image model. Therefore, PDF document can give readers a good reading effect.

Convert Word to PDF

Then, how to create PDF document? The method people often use is to convert from other format document, for example, Word to PDF.

In this post, I want to introduce a simple method to convert Word to PDF with C# and VB.NET quickly.

In this method,  a component, Spire.Doc for .NET is used. If you want to use the following code, please download and install this component on your computer firstly and then add Spire.Doc dll file as reference.

Detailed Steps Shown as Following:          

Step 1:

Load file which you want to convert to PDF from your computer by using document.LoadFromFile() method.

C#

            Document document = new Document();

            document.LoadFromFile(@”E:\work\Antarctic.docx”);

VB.NET

            Dim document As New Document()

            document.LoadFromFile(“E:\work\Antarctic.docx”)

Step 2:

Convert this document to PDF by using document.SaveToFile() method. Two parameters passed to this method, file name and file format.

C#

            document.SaveToFile(“ToPDF.PDF”, FileFormat.PDF);

VB.NET

            document.SaveToFile(“ToPDF.PDF”, FileFormat.PDF)

Step 3:

Launch new PDF document.

C#

            System.Diagnostics.Process.Start(“ToPDF.PDF”);

VB.NET

            System.Diagnostics.Process.Start(“ToPDF.PDF”)

Result Shown as Following:

Freely Download Spire.Doc for .NET

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

6 thoughts on “Convert Word to PDF Easily with C#/VB.NET”

    1. you can do it without a third party dll as follows:

      first
      Imports Word = Microsoft.Office.Interop.Word

      then use this procedure
      Private Sub Word2Pdf(ByVal infile As String, ByVal outfile As String)
      Dim wordApp As Word.Application = Nothing
      Try
      wordApp = New Word.Application()
      wordApp.Documents.Open(infile)
      wordApp.ActiveDocument.ExportAsFixedFormat(outfile, Word.WdExportFormat.wdExportFormatPDF)
      Finally

      If wordApp IsNot Nothing Then
      wordApp.Quit()
      End If

      Dim ps As New ProcessStartInfo
      ps.UseShellExecute = True
      ps.FileName = outfile
      Process.Start(ps)

      End Try
      End Sub

      you can call it as follows:
      Word2Pdf(“c:\test.doc”,”c:\test.pdf”)

      This works for me perfectly. A similar method can be used for excel to pdf.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s