Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:

I have to convert the word document .docx to pdf in .net 4.0

It has to be done internally. ie The prg has to go the specific path n if there is any word document, it has to get converted to pdf.

Can anyone help me with this.

Updated 28-Mar-16 19:17pm
Gooppoiner 5-Dec-19 4:35am    
Here is how you can convert word document to pdf in C# using this Word library for C# on any .NET Framework that's equal or above version 3.5.

If you can find a command line converter, then you can execute the command line. Another option would be to open the document in word ([^]) and then save it as pdf ([^])
Share this answer
I found a bit hacky solution, but for my purposes it works well and uses Foxit Reader to convert to pdf.[^]
Share this answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;

public partial class Default2 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
        wordDocument = appWord.Documents.Open(@"D:\desktop\xxxxxx.docx");
        wordDocument.ExportAsFixedFormat(@"D:\desktop\DocTo.pdf", WdExportFormat.wdExportFormatPDF);

    public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }

try this code... working successfully...
Share this answer
Thomas Daniels 20-Dec-12 5:48am    
Why do you post an answer to a question from may? The question is solved already!
Member 9460500 8-Apr-13 2:53am    
i like it, so i dont have to load the other pages.
Member 9802762 9-Mar-13 9:50am    
yes, i tryed it and got successful result. thank you
Member 9802762 9-Mar-13 9:51am    
now i need a code to convert ms word file to tiff format, if anyone can help then email me at

Sudheer Sid 2-Jul-13 1:26am    
Awesome worked for me
You'll have to use a 3rd party library to do this, like Apose.Words[^].

I don't believe iTextSharp can do it internally like you want. It's better for creating a PDF document from scratch.

Truthfully, I don't know of a free component that will do this.
Share this answer
If you do not want Office Interop since it has to run on a build machine that has no Office installed, you may do the following:
Create a simple interop based exe that you control over remoting or WCF. The remote interface takes the docx file content and returns the PDF content.
Run that program on a PC that has office installed. The build machine then can access that service via remoting/WCF.
Share this answer
I believe that I have created a solution for your problem (which was my problem too). I published it under the free MIT license.
It works like this: You create a Word document with placeholders. These placeholders can be replaced programmatically with the library easily.
Then, you can choose into which format (e. g. PDF) you want to convert it.

The project is here: GitHub - smartinmedia/Net-Core-DocX-HTML-To-PDF-Converter: .NET Core library to create custom reports based on Word docx or HTML documents and convert to PDF[^]
Share this answer
OriginalGriff 25-Oct-19 6:48am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 6 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

I'd also suggest that you post this as a Project:

As long as you have a good solid readme.MD file in GutHub, it will reach a wider audience, and be more visible than the fourteenth solution to an old question!
[no name] 26-Oct-19 3:06am    
Thank you for your comment!
In fact, I posted this here - although it is old - because I recently ran into that problem that I was not able to find a non-commercial C# library to convert DOCX to PDF. I looked at EVERY single Google search result and it was all futile. So, then I developed a library myself, which is under MIT license and free. I just want to give back to the community and release others from their code-suffering with that problem. When you google "C# docx to pdf", this exact page here is number 4 of the non-commercial search results. This is why I posted this here.
Again, I am acting in good faith and hope that it will help fellow developers.
Thanks for your idea of making this a "Project" page, which I will do!
OriginalGriff 26-Oct-19 3:25am    
You're welcome - the problem is that we get a lot of idiots either posting spam to commercial products on old questions, or posting anything they can copy'n'paste in order to boost their Codeproject reputation. And some of our more trigger-happy members respond to that by starting the "kick 'em off the site" process without reading everything too carefully.

As I said, keep to recent questions and there isn't a problem - and the Project should be interesting!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900