Convert Word File to PDF Using WordToPDF.dll in C#

Step 1

Create Word file

You can create a Word file programmatically or using Microsoft Office Word pad.

Step 2

Create a new project (console, Windows or any).

console application

Step 3

Download the “WordToPDF.dll“ file.

Step 4

Add this reference to the project.

Add reference

Step 5

Include the namespace WordToPDF in your class file.

using wordtoPDF

For example: 

  1. using WordToPDF;  
Step 6

Create an object of the class Word2Pdf as in the following:
  1. Word2Pdf objWorPdf=new Word2Pdf();  
Step 7

Assign properties

 

    InputLocation: Word file path.

    OutputLocation: In which location the PDF should be saved.

    1. Word2Pdf objWorPdf = new Word2Pdf();  
    2. string backfolder1 = "D:\\WOrdToPDF\\";  
    3. string strFileName = "TestFile.docx";  
    4. object FromLocation = backfolder1 + "\\" + strFileName;  
    5. string FileExtension = Path.GetExtension(strFileName);  
    6. string ChangeExtension = strFileName.Replace(FileExtension, ".pdf");  
    7. if (FileExtension == ".doc" || FileExtension == ".docx")  
    8. {  
    9.      object ToLocation = backfolder1 + "\\" + ChangeExtension;  
    10.      objWorPdf.InputLocation = FromLocation;  
    11.      objWorPdf.OutputLocation = ToLocation;  
    12.      objWorPdf.Word2PdfCOnversion();  
    13. }  
    Note: To use Path.GetExtension() you should include System.IO in the project.

    Word File

    test file

     

Step 8

Save, build and run the code.

Then the Pdf File will be generated in the folder location.

show Pdf file

save file

Conclusion

This is the same process as is done using code.

Up Next
    Ebook Download
    View all
    Learn
    View all