views:

4145

answers:

8

I have found several open-source/freeware programs that allow you to convert .doc files to .pdf files, but they're all of the application/printer driver variety, with no SDK attached.

I have found several programs that do have an SDK allowing you to convert .doc files to .pdf files, but they're all of the proprietary type, $2,000 a license or thereabouts.

Does anyone know of any clean, inexpensive (preferably free) programmatic solution to my problem, using C# or VB.NET?

Thanks!

+1  A: 

There's an entire discussion of libraries for converting Word to PDF on Joel's discussion forums. Some suggestions from the thread:

tgamblin
Thanks, but all the suggestions there fall under the two categories I described above: either not programmatic, or hugely expensive. I specifically need .doc to .pdf programmatically.
Shaul
A: 

Seems to be some relevent info here:

http://stackoverflow.com/questions/159744/converting-ms-word-documents-to-pdf-in-asp-net

Also, with Office 2007 having publish to PDF functionality, I guess you could use office automation to open the *.DOC file in Word 2007 and Save as PDF. I'm not too keen on office automation as it's slow and prone to hanging, but just throwing that out there...

MikeW
Aspose may work, but it's grossly expensive.
Shaul
+2  A: 

PDFCreator has a COM component, callable from .NET or VBScript (samples included in the download).

But, it seems to me that a printer is just what you need - just mix that with Word's automation, and you should be good to go.

Mark Brackett
where's this COM component? And what does "mik" mean? Was that meant to be "mix"?
Shaul
The COM component is included in the download, along with samples. And yes, that was supposed to be "mix".
Mark Brackett
+1  A: 

I do this as part of a release process - convert a Word Doc to PDF.

http://www.suodenjoki.dk/us/productions/articles/word2pdf.htm and http://www.oooforum.org/forum/viewtopic.phtml?t=3772&highlight=pdf+form

not exactly programmatically, but may help you.

Tim
A: 

I have used iTextSharp to generate PDFs before. It's an open source port of iText from the Java world and is pretty powerful.

I haven't explicitly done a Word to PDF conversion, but I have programmatically created and manipulated PDFs with it.

Here is another link to to the project.

Mike L
I am actually already using iTextSharp in my project, but it doesn't convert Word.
Shaul
+9  A: 

Here is a modification of a program that worked for me. It uses Word 2007 with the Save As PDF add-in installed. It searches a directory for .doc files, opens them in Word and then saves them as a PDF. Note that you'll need to add a reference to Microsoft.Office.Interop.Word to the solution.

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

...

// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    doc.Activate();

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
Eric Ness
Thank you! I may just go with Aspose anyway, if it's faster than Word automation. But if I can tolerate a little bit of slowness, I'll prolly use your solution. Thanks again!
Shaul
Yes, it's not the fastest but it's hard to beat the price. :-) Glad I could help.
Eric Ness
Thanks Eric - useful
Sam Meldrum
+1, this is a useful technique if you want free PDF conversion.
RichardOD
With Office 2007 SP2 you no longer need the save as PDF download. I've also used this technique successfully for Excel and Powerpoint.
RichardOD
Have you use this method on a server with a web application? I'm getting alot of issues not mention its not recommended by MS.http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2I heard ASPose is great but its quite dear.
pwee167
I haven't tried this with a web application so I can't guarantee that it will work in that environment.
Eric Ness
Would the program need to be deployed together with the Microsoft.Office.Interop.Word assembly?
Chry Cheng
If Word isn't installed on the machine that you're deploying it on then you'd have to include that assembly.
Eric Ness
A: 

When I stumbled upon some problems with server side office automation we looked into the technique described here on codeproject. It uses the portable version (which can be deployed via xcopy) of OpenOffice in combination with a macro. Although we haven't done the switch ourselves yet, it looks very promissing.

Cohen
A: 

Compared to other versions of pdf converters ,i find Advanced word To pdf more appealing as it more faster to convert a huge file to pdf without any quality lost, it also has more configuration option.plus,it only costs you $19.95. http://www.advancedpdfconverter.com/products/wordtopdf.html