Winnovative Logo

Word to PDF Component

Winnovative Client for .NET Framework

Winnovative client library allows you to easily convert in just a few lines of code Word documents to PDF. The Word to PDF Converter object of WordToPdfConverter type can be initialized with the TCP/IP address of the server or with the HTTP URL address of the server, function of the Winnovative Server type you have installed.

Word to PDF Converter Options

The Word to PDF Converter allows you to set the Word content location in PDF document and to add custom headers and footers to generated PDF document, in addition to the headers and footer defined in the Word document. The PDF page size and orientation are inherited from Word document properties. These features of the Word to PDF converter are exemplified in the code sample below. The full Visual Studio demo project for ASP.NET Web Forms or MVC is available in product package you can download from website.

Code Sample - Convert Word to PDF in ASP.NET with WordToPdfConverter Class

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.WebControls;

using WinnovativeClient;

namespace WordToPdfClientDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void convertToPdfButton_Click(object sender, EventArgs e)
        {
            /// Get the server options
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string servicePassword = textBoxServicePassword.Text;
            bool useServicePassword = servicePassword.Length > 0;
            bool useTcpService = radioButtonUseTcpService.Checked;
            string webServiceUrl = textBoxWebServiceUrl.Text;

            // Create the Word to PDF converter
            WordToPdfConverter wordToPdfConverter = null;
            if (useTcpService)
                wordToPdfConverter = new WordToPdfConverter(serverIP, serverPort);
            else
                wordToPdfConverter = new WordToPdfConverter(true, webServiceUrl);

            // Set optional service password
            if (useServicePassword)
                wordToPdfConverter.ServicePassword = servicePassword;

            // 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);

            string wordFile = filePathTextBox.Text;

            // Convert the Word document to a PDF document
            byte[] outPdfBuffer = wordToPdfConverter.ConvertWordFile(wordFile);

            // Send the PDF as response to browser

            // Set response content type
            Response.AddHeader("Content-Type", "application/pdf");

            // Instruct the browser to open the PDF file as an attachment or inline
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename=WordToPdf.pdf; size={0}", outPdfBuffer.Length.ToString()));

            // Write the PDF document buffer to HTTP response
            Response.BinaryWrite(outPdfBuffer);

            // End the HTTP response and stop the current page processing
            Response.End();
        }

        /// <summary>
        /// Draw the header elements
        /// </summary>
        /// <param name="wordToPdfConverter">The Word to PDF Converter object</param>
        /// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param>
        private void DrawHeader(WordToPdfConverter WordToPdfConverter, bool drawHeaderLine)
        {
            string headerImagePath = Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg");

            // Set the header height in points
            WordToPdfConverter.PdfHeaderOptions.HeaderHeight = 60;

            // Set header background color
            WordToPdfConverter.PdfHeaderOptions.HeaderBackColor = RgbColor.WhiteSmoke;

            // Set logo
            ImageElement headerImage = new ImageElement(5, 5, 100, 50, headerImagePath);
            WordToPdfConverter.PdfHeaderOptions.AddElement(headerImage);

            // Set header text
            TextElement headerText = new TextElement(0, 5, "Winnovative Word to PDF Converter ", new PdfFont("Times New Roman", 10, true));
            // Align the text at the right of the footer
            headerText.TextAlign = HorizontalTextAlign.Right;
            // Set text color
            headerText.ForeColor = RgbColor.Navy;
            // Embed the text element font in PDF
            headerText.EmbedSysFont = true;
            // Add the text element to header
            WordToPdfConverter.PdfHeaderOptions.AddElement(headerText);
        }

        /// <summary>
        /// Draw the footer elements
        /// </summary>
        /// <param name="wordToPdfConverter">The Word to PDF Converter object</param>
        /// <param name="addPageNumbers">A flag indicating if the page numbering is present in footer</param>
        /// <param name="drawFooterLine">A flag indicating if a line should be drawn at the top of the footer</param>
        private void DrawFooter(WordToPdfConverter WordToPdfConverter, bool addPageNumbers, bool drawFooterLine)
        {
            string footerImagePath = Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg");

            // Set the footer height in points
            WordToPdfConverter.PdfFooterOptions.FooterHeight = 60;

            // Set footer background color
            WordToPdfConverter.PdfFooterOptions.FooterBackColor = RgbColor.WhiteSmoke;

            // Set logo
            ImageElement headerImage = new ImageElement(5, 5, 100, 50, footerImagePath);
            WordToPdfConverter.PdfFooterOptions.AddElement(headerImage);

            // Add page numbering
            if (addPageNumbers)
            {
                // Create a text element with page numbering place holders &p; and & P;
                TextElement footerText = new TextElement(0, 30, "Page &p; of &P;  ", new PdfFont("Times New Roman", 10, true));

                // Align the text at the right of the footer
                footerText.TextAlign = HorizontalTextAlign.Right;

                // Set page numbering text color
                footerText.ForeColor = RgbColor.Navy;

                // Embed the text element font in PDF
                footerText.EmbedSysFont = true;

                // Add the text element to footer
                WordToPdfConverter.PdfFooterOptions.AddElement(footerText);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                filePathTextBox.Text = Server.MapPath("~/DemoAppFiles/Input/Word/Demo.docx");

                Master.CollapseAll();
                Master.ExpandNode("Word_to_PDF");
                Master.SelectNode("Getting_Started");
            }
        }
    }
}