Code Sample for Word to PDF Conversion |
The code below was taken from the Word to PDF demo application available for download in the Word to PDF Converter archive. In this example an instance of the WordToPdfConverter class is constructed and used to convert an Word file to a PDF document in a memory buffer. The resulted buffer is saved in a PDF file on disk. The demo also contains code for adding a header and a footer with page numbering to the resulted PDF document. |
private void btnWordToPdf_Click(object sender, EventArgs e) { // Create a Word to PDF converter object with default settings WordToPdfConverter wordToPdfConverter = new WordToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode wordToPdfConverter.LicenseKey = "DYOSgpaRgpKClIySgpGTjJOQjJubm5uCkg=="; // Word Content Destination and Spacing Options // Set Word content destination in PDF page if (xLocationTextBox.Text.Length > 0) wordToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text); if (yLocationTextBox.Text.Length > 0) wordToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text); if (contentWidthTextBox.Text.Length > 0) wordToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text); if (contentHeightTextBox.Text.Length > 0) wordToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text); // Set Word content top and bottom spacing or leave them not set to have no spacing for the Word content wordToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text); wordToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text); // Add Header // Enable header in the generated PDF document wordToPdfConverter.PdfDocumentOptions.ShowHeader = addHeaderCheckBox.Checked; // Draw header elements if (wordToPdfConverter.PdfDocumentOptions.ShowHeader) DrawHeader(wordToPdfConverter, true); // Add Footer // Enable footer in the generated PDF document wordToPdfConverter.PdfDocumentOptions.ShowFooter = addFooterCheckBox.Checked; // Draw footer elements if (wordToPdfConverter.PdfDocumentOptions.ShowFooter) DrawFooter(wordToPdfConverter, true, true); Cursor = Cursors.WaitCursor; string outPdfFile = @"DemoAppFiles\Output\WordToPdf.pdf"; try { string wordFile = wordFilePathTextBox.Text; // Convert the Word document to a PDF document byte[] outPdfBuffer = wordToPdfConverter.ConvertWordFile(wordFile); // Write the memory buffer in a PDF file System.IO.File.WriteAllBytes(outPdfFile, outPdfBuffer); } catch (Exception ex) { // The Word to PDF conversion failed MessageBox.Show(String.Format("Word to PDF Error. {0}", ex.Message)); return; } finally { Cursor = Cursors.Arrow; } // Open the created PDF document in default PDF viewer try { System.Diagnostics.Process.Start(outPdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("Cannot open created PDF file '{0}'. {1}", outPdfFile, ex.Message)); } } /// |