How to Start Convert PDF Read PDF Edit PDF PDF Report Builder Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Word Converter Library
How to convert Word .docx to PDF file in byte array programmatically without Office, interop using free C#.net code in ASP.NET MVC web application


C# Demo Code to Create PDF Document from Word in C# Program with .NET XDoc.PDF Component





In this tutorial, you learn how to convert Microsoft Word document to PDF file using C# Word to PDF Converter library in .NET Windows, ASP.NET applications.

  • Convert Word (.docx) to PDF
  • Conversion with PDF bookmark support
  • Convert multiple Word files to PDFs
  • Convert byte array Word to PDF in byte array
  • Insert Word document into PDF pages directly
  • No Office installed, No interop needed

How to convert Word to PDF file programmatically using C#

  1. Download XDoc.PDF Word conversion C# library
  2. Install C# library to convert Office Word to PDF document
  3. Step by Step Tutorial












  • Best Microsoft Office Word to adobe PDF file converter SDK for Visual Studio .NET
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • A convenient C#.NET control able to turn all Word text and image content into high quality PDF without losing formatting
  • Convert multiple pages Word to fillable and editable PDF documents in both .NET WinForms and ASP.NET
  • Convert both DOC and DOCX formats to PDF files
  • Professional .NET control for batch conversion in C#.NET class
  • About PDF convert library: convert pdf to svg c#, convert pptx to pdf c#, convert pdf to tiff c#, c# html image to pdf, convert rtf to pdf c# code, pdf to image converter in c#.
  • Easy to create searchable and scanned PDF files from Word
  • Convert to PDF with embedded fonts or without original fonts fast
  • Able to get word count in PDF pages
  • Change Word hyperlink to PDF hyperlink and bookmark
  • Free online Word to PDF converter without email
  • C# source code for integration in to .NET console application
  • Free C#.NET library and components for .NET framework






C#.NET Sample Code: Convert Word to PDF in C#.NET Project


Following is C# demo code for Word (.docx/.dotm/.docm/.dotx) to PDF conversion.



            String inputPath = @"C:\demo.docx";
            String outputPath = @"C:\output.pdf";
            //convert .docx to .pdf
            DOCXDocument doc = new DOCXDocument(inputPath);
            doc.ConvertToDocument(DocumentType.PDF, outputPath);


Following demo code will show how to convert Word2003(.doc) to PDF.



            String inputPath = @"C:\demo.doc";
            String outputPath = @"C:\output.pdf";
            //convert .doc to .pdf
            DOCDocument doc = new DOCDocument(inputPath);
            doc.ConvertToDocument(DocumentType.PDF, outputPath);






C#: convert Word (.docx) to Adobe PDF with bookmark support


When you convert Office Word (.docx) document to PDF file, you can generate PDF outline (bookmark) from Word document heading content or bookmarks.

There are four options for OutlineMode:


  1. None: Do not create any outline entry.

  2. ByOutlineLevel: Create outline entries by paragraph's outline level.

  3. ByHeading: Create outline entries by paragraph's style IDs (heading styles).

  4. ByBookmark: Create outline entries by bookmarks.


String inputFilePath = @"C:\1.docx");
String outputFilePath = @"C:\output.pdf");

DOCXDocument doc = new DOCXDocument(inputFilePath);
//  Set strategy used to create outline entries for the document.
//  Default: OutlineMode.ByOutlineLevel
doc.OutlineSetting.Mode = OutlineMode.ByHeading;
//  Set the maximum level of entires in the outline.
//  Valid range: 1 ~ 9; default value: 3
doc.OutlineSetting.MaxLevel = 2;

//  Convert DOCX document to PDF file.
doc.ConvertToDocument(DocumentType.PDF, outputFilePath);






C# convert two or multiple Word files to PDF (batch convert)


Convert MS Office Word 2007 (.docx) file to PDF using single thread



            String inputDirectory = @"C:\input\";
            String outputDirectory = @"C:\output\";
            String[] files = Directory.GetFiles(inputDirectory, "*.docx");
            foreach (String filePath in files)
            {
                int startIdx = filePath.LastIndexOf("\\");
                int endIdx = filePath.LastIndexOf(".");
                String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
                DOCXDocument doc = new DOCXDocument(filePath);
                doc.ConvertToDocument(DocumentType.PDF, outputDirectory + docName + ".pdf");
            }


Convert MS Office Word 2007 (.docx) file to PDF using multiple threads



        #region word to pdf (batch file and multiple threads)
        internal static void docxfiles2pdf()
        {
            String inputDirectory = @"C:\input\";
            String outputDirectory = @"C:\output\";
            String[] files = Directory.GetFiles(inputDirectory, "*.docx");
            List<ConversionArgs> args = new List<ConversionArgs>();
            foreach (String filePath in files)
            {
                int startIdx = filePath.LastIndexOf("\\");
                int endIdx = filePath.LastIndexOf(".");
                String docName = filePath.Substring(startIdx + 1, endIdx - startIdx - 1);
                ConversionArgs arg = new ConversionArgs(filePath, outputDirectory + docName + ".pdf");
                args.Add(arg);
            }

            List<Thread> threads = new List<Thread>();
            foreach (ConversionArgs arg in args)
            {
                Thread thread = new Thread(docxToPdfThread);
                thread.Start(arg);
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }
        }

        private static void docxToPdfThread(object args)
        {
            ConversionArgs toPdfArgs = (ConversionArgs)args;
            DOCXDocument srcDoc = new DOCXDocument(toPdfArgs.SrcPath);
            if (srcDoc != null)
            {
                srcDoc.ConvertToDocument(DocumentType.PDF, toPdfArgs.DstPath);
                srcDoc.Dispose();
            }
        }
        #endregion




C# combine multiple Word files, and convert to PDF


        #region combine and convert word(2007 or higher) to single pdf
        internal static void combineAndConvertwordToPdf()
        {
            String[] files = new String[] { @"C:\demo1.docx", @"C:\demo2.docx", @"C:\demo3.docx" };
            String outputFilePath = @"C:\output.pdf";
            List<MemoryStream> streams = new List<MemoryStream>();
            foreach (String file in files)
            {
                MemoryStream outputStream = new MemoryStream();
                DOCXDocument doc = new DOCXDocument(file);
                doc.ConvertToDocument(DocumentType.PDF, outputStream);
                streams.Add(outputStream);
            }
            PDFDocument.CombineDocument(streams.ToArray(), outputFilePath);
        }
        #endregion




C# insert word file into pdf document, and create a new PDF file


        #region insert word(2007 or higher) to pdf
        internal static void insertwordToPdf()
        {
            String filePath = @"C:\demo.docx";
            DOCXDocument doc = new DOCXDocument(filePath);
            MemoryStream stream = new MemoryStream();
            doc.ConvertToDocument(DocumentType.PDF, stream);
            PDFDocument pdf = new PDFDocument(stream);
            int pageCount = pdf.GetPageCount();
            List<BasePage> pages = new List<BasePage>();
            for (int i = 0; i < pageCount; i++)
            {
                pages.Add(pdf.GetPage(i));
            }
            String outputPdf = @"C:\output.pdf";
            PDFDocument desDoc = new PDFDocument(outputPdf);
            int insertLocation = 2;
            desDoc.InsertPages(pages.ToArray(), insertLocation);
            desDoc.Save(@"C:\desDocumcnet.pdf");
        }
        #endregion