How do I convert Word files to PDF programmatically? [closed]

Closed. This question is seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago . The community reviewed whether to reopen this question 2 years ago and left it closed:
Original close reason(s) were not resolved

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!

asked Mar 3, 2009 at 19:03 Shaul Behr Shaul Behr 37.8k 70 70 gold badges 257 257 silver badges 396 396 bronze badges

Check if Pandoc has bindings for your favourite language. The command line interface is also dead easy pandoc manual.docx -o manual.pdf

Commented Sep 30, 2016 at 15:29

Also, check GemBox.Document SDK. It has a free version and an inexpensive version. It not using neither a printer driver nor ms office to convert Word files to PDF.

Commented Feb 11, 2020 at 8:47 You can use docx2pdf to make this conversion: github.com/AlJohri/docx2pdf Commented Feb 18, 2020 at 7:33

For people looking for a solution, this is much easier with LibreOffice. If you have docker installed, it is a simple command: docker "run" "--rm" "--entrypoint" "soffice" "-v" "$(pwd):/usr/src/project" "linuxserver/libreoffice:latest" "--headless" "--convert-to" "pdf" "--outdir" "/usr/src/project" "/usr/src/project/foo.docx"

Commented Feb 13, 2023 at 15:22 You can use ApyHub's Doc to PDF API - apyhub.com/utility/converter-doc-pdf Commented Mar 4 at 11:43

8 Answers 8

Use a foreach loop instead of a for loop - it solved my problem.

int j = 0; foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages) < var bits = p.EnhMetaFileBits; var target = path1 +j.ToString()+ "_image.doc"; try < using (var ms = new MemoryStream((byte[])(bits))) < var image = System.Drawing.Image.FromStream(ms); var pngTarget = Path.ChangeExtension(target, "png"); image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png); >> catch (System.Exception ex) < MessageBox.Show(ex.Message); >j++; > 

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;