EVO PDF 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 EVO PDF Server type you have installed.
TCP/IP Server. If you installed the EVO PDF Server in an Azure Cloud Service Worker Role, in an in an Azure Service Fabric Application or in a Windows Service on a remote Windows machine you can use the WordToPdfConverterWordToPdfConverter(String, UInt32) constructor which takes as parameters the server IP address and the TCP port.
HTTP Server. If you installed the EVO PDF Server in an Azure Cloud Service Web Role or in an IIS ASP.NET Web Application you can use the WordToPdfConverterWordToPdfConverter(Boolean, String) constructor which takes as parameters a flag to be set on true to indicate the usage of a HTTP service and the HTTP Server URL string as the second parameter.
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.
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 EvoPdfClient;
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 = "0lxNXUtPXU1dS1NNXU5MU0xPU0RERERdTQ==";
// 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, "EVO 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");
}
}
}
}