Click here to Skip to main content
14,392,287 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

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.


Karthika.
Posted
Updated 28-Mar-16 20:17pm
Comments
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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 (http://omegacoder.com/?p=555[^]) and then save it as pdf (http://msdn.microsoft.com/en-US/library/microsoft.office.tools.word.document.saveas(v=VS.80).aspx[^])
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 10

I found a bit hacky solution, but for my purposes it works well and uses Foxit Reader to convert to pdf.

http://wpf4developers.blogspot.de/2014/05/c-convert-docx-to-pdf-document.html[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

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...
   
v2
Comments
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 mmaqbool@prosoftmd.com

thanks
Sudheer Sid 2-Jul-13 1:26am
   
Awesome worked for me
Member 8446116 13-Jul-13 8:47am
   
wordDocument = appWord.Documents.Open(@"D:\desktop\xxxxxx.docx");
wordDocument.ExportAsFixedFormat(@"D:\desktop\DocTo.pdf", WdExportFormat.wdExportFormatPDF);

suppose i use this code this error occur No overload method with 1 arguments
so how to use this code
kindly explain
ravinsingh 28-Jul-15 8:33am
   
i use the code in my ASP.NET as below, it's worked perfect
document.ExportAsFixedFormat(HostingEnvironment.MapPath(@"~/Target")+"/DocTo.pdf", WdExportFormat.wdExportFormatPDF);
Anwer Jamal 9-Sep-15 7:23am
   
Very short and easy to undersatand...
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 7

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.
Cheers
Andi
   
Rate this:
Please Sign up or sign in to vote.

Solution 14

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[^]
   
v2
Comments
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:

https://www.codeproject.com/script/Articles/ImportArticle.aspx

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!
Martin
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100