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);
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:
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);
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 args = new List(); 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 threads = new List(); 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
#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 streams = new List(); 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
#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 pages = new List(); 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
Copyright © <2000-2024>by . All Rights Reserved. RasterEdge.com is professional ASP.NET Document Viewer Control, ASP.NET MVC Document Viewer, ASP.NET PDF Viewer, MVC PDF Viewer and ASP.NET PDF Editor Control provider for document, content and imaging solutions, available for ASP.NET AJAX, Silverlight, Windows Forms as well as WPF. We are dedicated to provide powerful & profession imaging controls and components for capturing, viewing, processing, converting, compressing and storing images, documents and more.2000-2024>