How to convert Word files to PDF programmatically in C#?

How to convert Word files to PDF programmatically in C#?

To convert Word files to PDF programmatically in C#, you can use the Microsoft.Office.Interop.Word library, which provides access to the Word object model. Here is an example of how to convert a Word file to PDF using this library:

using Microsoft.Office.Interop.Word;

// Open a Word document
var wordApp = new Application();
var wordDoc = wordApp.Documents.Open("C:\\path\\to\\document.docx");

// Convert the document to PDF
wordDoc.ExportAsFixedFormat("C:\\path\\to\\document.pdf", WdExportFormat.wdExportFormatPDF);

// Close the Word document and application
wordDoc.Close();
wordApp.Quit();

In this example, we first create a new instance of the Application class from the Microsoft.Office.Interop.Word namespace. We then open the Word document using the Documents.Open method, passing in the file path as a parameter.

Next, we use the ExportAsFixedFormat method of the Document object to convert the document to PDF. This method takes two parameters: the output file path and the format to which to export the document. In this case, we specify WdExportFormat.wdExportFormatPDF to export the document as a PDF file.

Finally, we close the Word document and the Word application using the Close and Quit methods, respectively. It's important to remember to close the Word application when you're done using it, as leaving it open can cause memory leaks and other issues.

Examples

  1. "C# convert Word to PDF using Interop"

    // Example using Microsoft.Office.Interop.Word
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open("path/to/word/document.docx");
    doc.ExportAsFixedFormat("path/to/output/document.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
    

    Description: Utilizes Microsoft Office Interop for Word to convert a Word document to PDF.

  2. "C# convert Word to PDF with Open XML SDK"

    // Example using Open XML SDK
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open("path/to/word/document.docx", false))
    {
        PdfConverter.ConvertToPdf(wordDoc, "path/to/output/document.pdf");
    }
    

    Description: Demonstrates how to use Open XML SDK for Word to convert to PDF.

  3. "Convert Word to PDF in C# without Microsoft Word installed"

    // Example using third-party library (e.g., Aspose.Words)
    Document doc = new Document("path/to/word/document.docx");
    doc.Save("path/to/output/document.pdf", SaveFormat.Pdf);
    

    Description: Uses a third-party library, like Aspose.Words, to convert Word to PDF without requiring Microsoft Word installation.

  4. "C# convert Word to PDF with iTextSharp"

    // Example using iTextSharp
    using (var stream = new FileStream("path/to/output/document.pdf", FileMode.Create))
    {
        var document = new Document();
        PdfWriter.GetInstance(document, stream);
        document.Open();
        // Add content from Word document to PDF
        document.Close();
    }
    

    Description: Demonstrates how to use iTextSharp to convert a Word document to PDF.

  5. "Convert multiple Word files to a single PDF in C#"

    // Example using iTextSharp to merge multiple PDFs
    var pdfMerger = new PdfMerger();
    pdfMerger.AddDocument("path/to/word/document1.docx");
    pdfMerger.AddDocument("path/to/word/document2.docx");
    pdfMerger.Merge("path/to/output/merged_document.pdf");
    

    Description: Shows how to merge multiple Word files into a single PDF using iTextSharp.

  6. "C# convert Word to PDF with DocX library"

    // Example using DocX library
    var doc = DocX.Load("path/to/word/document.docx");
    doc.SaveAs("path/to/output/document.pdf");
    

    Description: Utilizes the DocX library to convert a Word document to PDF.

  7. "C# convert Word to PDF with GemBox.Document"

    // Example using GemBox.Document library
    var document = DocumentModel.Load("path/to/word/document.docx");
    document.Save("path/to/output/document.pdf");
    

    Description: Demonstrates how to use GemBox.Document library for Word to PDF conversion.

  8. "C# convert Word to PDF with Spire.Doc"

    // Example using Spire.Doc library
    Document document = new Document("path/to/word/document.docx");
    document.SaveToFile("path/to/output/document.pdf", FileFormat.PDF);
    

    Description: Shows how to use the Spire.Doc library for Word to PDF conversion.

  9. "Convert Word to PDF with images in C#"

    // Example handling images using iTextSharp
    var document = new Document();
    var writer = PdfWriter.GetInstance(document, new FileStream("path/to/output/document.pdf", FileMode.Create));
    document.Open();
    // Add content and handle images from Word document
    document.Close();
    

    Description: Addresses how to handle images during the conversion from Word to PDF using iTextSharp.

  10. "C# convert Word to PDF with PDFSharp"

    // Example using PDFSharp
    var document = MigraDoc.DocumentObjectModel.DocumentObjectModel.Document.Parse("path/to/word/document.docx", DmlFromOuterXmlOptions.ForLibraries);
    var renderer = new PdfDocumentRenderer();
    renderer.Document = document;
    renderer.RenderDocument();
    renderer.PdfDocument.Save("path/to/output/document.pdf");
    

    Description: Demonstrates how to use PDFSharp for Word to PDF conversion.


More Tags

ng2-file-upload jitpack combinations django url.action extending data-files ora-00904 .net-4.5 bootstrap-5

More C# Questions

More Everyday Utility Calculators

More General chemistry Calculators

More Geometry Calculators

More Entertainment Anecdotes Calculators