Winnovative client library allows you to easily convert in just a few lines of code Excel documents to PDF. The Excel to PDF Converter object of ExcelToPdfConverter 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.
TCP/IP Server. If you installed the Winnovative 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 ExcelToPdfConverterExcelToPdfConverter(String, UInt32) constructor which takes as parameters the server IP address and the TCP port.
HTTP Server. If you installed the Winnovative Server in an Azure Cloud Service Web Role or in an IIS ASP.NET Web Application you can use the ExcelToPdfConverterExcelToPdfConverter(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 Excel to PDF Converter allows you to set the Excel content location in PDF document and to add custom headers and footers to generated PDF document. You can also set the PDF page size, orientation and margins. These features of the Excel 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.UI;
using System.Web.UI.WebControls;
using WinnovativeClient;
namespace ExcelToPdfClientDemo
{
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 Excel to PDF converter object
ExcelToPdfConverter excelToPdfConverter = null;
if (useTcpService)
excelToPdfConverter = new ExcelToPdfConverter(serverIP, serverPort);
else
excelToPdfConverter = new ExcelToPdfConverter(true, webServiceUrl);
// Set optional service password
if (useServicePassword)
excelToPdfConverter.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
excelToPdfConverter.LicenseKey = "P7GgsKSisKCwpr6gsKOhvqGivqmpqamwoA==";
// PDF Page Options
// Set PDF page size which can be a predefined size like A4 or a custom size in points
// Leave it not set to have a default A4 PDF page
excelToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();
// Set PDF page orientation to Portrait or Landscape
// Leave it not set to have a default Portrait orientation for PDF page
excelToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();
// Set PDF page margins in points or leave them not set to have a PDF page without margins
excelToPdfConverter.PdfDocumentOptions.LeftMargin = float.Parse(leftMarginTextBox.Text);
excelToPdfConverter.PdfDocumentOptions.RightMargin = float.Parse(rightMarginTextBox.Text);
excelToPdfConverter.PdfDocumentOptions.TopMargin = float.Parse(topMarginTextBox.Text);
excelToPdfConverter.PdfDocumentOptions.BottomMargin = float.Parse(bottomMarginTextBox.Text);
// Excel Content Destination and Spacing Options
// Set Excel content destination in PDF page
if (xLocationTextBox.Text.Length > 0)
excelToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text);
if (yLocationTextBox.Text.Length > 0)
excelToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text);
if (contentWidthTextBox.Text.Length > 0)
excelToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text);
if (contentHeightTextBox.Text.Length > 0)
excelToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text);
// Set Excel content top and bottom spacing or leave them not set to have no spacing for the Excel content
excelToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text);
excelToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text);
// Scaling Options
// Use this option to fit the Excel content width in PDF page width
// By default this property is true and the Excel content can be resized to fit the PDF page width
excelToPdfConverter.PdfDocumentOptions.FitWidth = fitWidthCheckBox.Checked;
// Use this option to enable the Excel content stretching when its width is smaller than PDF page width
// This property has effect only when FitWidth option is true
// By default this property is false and the Excel content is not stretched
excelToPdfConverter.PdfDocumentOptions.StretchToFit = stretchCheckBox.Checked;
// Use this option to automatically dimension the PDF page to display the Excel content unscaled
// This property has effect only when the FitWidth property is false
// By default this property is true and the PDF page is automatically dimensioned when FitWidth is false
excelToPdfConverter.PdfDocumentOptions.AutoSizePdfPage = autoSizeCheckBox.Checked;
// Use this option to fit the Excel content height in PDF page height
// If both FitWidth and FitHeight are true then the Excel content will resized if necessary to fit both width and height
// preserving the aspect ratio at the same time
// By default this property is false and the Excel content is not resized to fit the PDF page height
excelToPdfConverter.PdfDocumentOptions.FitHeight = fitHeightCheckBox.Checked;
// Use this option to render the whole Excel content into a single PDF page
// The PDF page size is limited to 14400 points
// By default this property is false
excelToPdfConverter.PdfDocumentOptions.SinglePage = singlePageCheckBox.Checked;
// Add Header
// Enable header in the generated PDF document
excelToPdfConverter.PdfDocumentOptions.ShowHeader = addHeaderCheckBox.Checked;
// Draw header elements
if (excelToPdfConverter.PdfDocumentOptions.ShowHeader)
DrawHeader(excelToPdfConverter, true);
// Add Footer
// Enable footer in the generated PDF document
excelToPdfConverter.PdfDocumentOptions.ShowFooter = addFooterCheckBox.Checked;
// Draw footer elements
if (excelToPdfConverter.PdfDocumentOptions.ShowFooter)
DrawFooter(excelToPdfConverter, true, true);
string excelFile = filePathTextBox.Text;
// Convert the Excel document to a PDF document
byte[] outPdfBuffer = excelToPdfConverter.ConvertExcelFile(excelFile);
// 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=ExcelToPdf.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="excelToPdfConverter">The Excel 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(ExcelToPdfConverter excelToPdfConverter, bool drawHeaderLine)
{
string headerImagePath = Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg");
// Set the header height in points
excelToPdfConverter.PdfHeaderOptions.HeaderHeight = 60;
// Set header background color
excelToPdfConverter.PdfHeaderOptions.HeaderBackColor = RgbColor.WhiteSmoke;
// Set logo
ImageElement headerImage = new ImageElement(5, 5, 100, 50, headerImagePath);
excelToPdfConverter.PdfHeaderOptions.AddElement(headerImage);
// Set header text
TextElement headerText = new TextElement(0, 5, "Winnovative Excel 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
excelToPdfConverter.PdfHeaderOptions.AddElement(headerText);
if (drawHeaderLine)
{
// Calculate the header width based on PDF page size and margins
float headerWidth = excelToPdfConverter.PdfDocumentOptions.PdfPageOrientation == PdfPageOrientation.Portrait ?
excelToPdfConverter.PdfDocumentOptions.PdfPageSize.Width : excelToPdfConverter.PdfDocumentOptions.PdfPageSize.Height -
excelToPdfConverter.PdfDocumentOptions.LeftMargin - excelToPdfConverter.PdfDocumentOptions.RightMargin;
// Calculate header height
float headerHeight = excelToPdfConverter.PdfHeaderOptions.HeaderHeight;
// Create a line element for the bottom of the header
LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1);
// Set line color
headerLine.ForeColor = RgbColor.Gray;
// Add line element to the bottom of the header
excelToPdfConverter.PdfHeaderOptions.AddElement(headerLine);
}
}
/// <summary>
/// Draw the footer elements
/// </summary>
/// <param name="excelToPdfConverter">The Excel 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(ExcelToPdfConverter excelToPdfConverter, bool addPageNumbers, bool drawFooterLine)
{
string footerImagePath = Server.MapPath("~/DemoAppFiles/Input/Images/logo.jpg");
// Set the footer height in points
excelToPdfConverter.PdfFooterOptions.FooterHeight = 60;
// Set footer background color
excelToPdfConverter.PdfFooterOptions.FooterBackColor = RgbColor.WhiteSmoke;
// Set logo
ImageElement headerImage = new ImageElement(5, 5, 100, 50, footerImagePath);
excelToPdfConverter.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
excelToPdfConverter.PdfFooterOptions.AddElement(footerText);
}
if (drawFooterLine)
{
// Calculate the footer width based on PDF page size and margins
float footerWidth = excelToPdfConverter.PdfDocumentOptions.PdfPageOrientation == PdfPageOrientation.Portrait ?
excelToPdfConverter.PdfDocumentOptions.PdfPageSize.Width : excelToPdfConverter.PdfDocumentOptions.PdfPageSize.Height -
excelToPdfConverter.PdfDocumentOptions.LeftMargin - excelToPdfConverter.PdfDocumentOptions.RightMargin;
// Create a line element for the top of the footer
LineElement footerLine = new LineElement(0, 0, footerWidth, 0);
// Set line color
footerLine.ForeColor = RgbColor.Gray;
// Add line element to the bottom of the footer
excelToPdfConverter.PdfFooterOptions.AddElement(footerLine);
}
}
private PdfPageSize SelectedPdfPageSize()
{
switch (pdfPageSizeDropDownList.SelectedValue)
{
case "A0":
return PdfPageSize.A0;
case "A1":
return PdfPageSize.A1;
case "A10":
return PdfPageSize.A10;
case "A2":
return PdfPageSize.A2;
case "A3":
return PdfPageSize.A3;
case "A4":
return PdfPageSize.A4;
case "A5":
return PdfPageSize.A5;
case "A6":
return PdfPageSize.A6;
case "A7":
return PdfPageSize.A7;
case "A8":
return PdfPageSize.A8;
case "A9":
return PdfPageSize.A9;
case "ArchA":
return PdfPageSize.ArchA;
case "ArchB":
return PdfPageSize.ArchB;
case "ArchC":
return PdfPageSize.ArchC;
case "ArchD":
return PdfPageSize.ArchD;
case "ArchE":
return PdfPageSize.ArchE;
case "B0":
return PdfPageSize.B0;
case "B1":
return PdfPageSize.B1;
case "B2":
return PdfPageSize.B2;
case "B3":
return PdfPageSize.B3;
case "B4":
return PdfPageSize.B4;
case "B5":
return PdfPageSize.B5;
case "Flsa":
return PdfPageSize.Flsa;
case "HalfLetter":
return PdfPageSize.HalfLetter;
case "Ledger":
return PdfPageSize.Ledger;
case "Legal":
return PdfPageSize.Legal;
case "Letter":
return PdfPageSize.Letter;
case "Letter11x17":
return PdfPageSize.Letter11x17;
case "Note":
return PdfPageSize.Note;
default:
return PdfPageSize.A4;
}
}
private PdfPageOrientation SelectedPdfPageOrientation()
{
return (pdfPageOrientationDropDownList.SelectedValue == "Portrait") ?
PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
filePathTextBox.Text = Server.MapPath("~/DemoAppFiles/Input/Excel/Demo.xlsx");
pdfPageSizeDropDownList.SelectedValue = "A4";
pdfPageOrientationDropDownList.SelectedValue = "Portrait";
Master.CollapseAll();
Master.ExpandNode("Excel_to_PDF");
Master.SelectNode("Getting_Started");
}
}
protected void fitWidthCheckBox_CheckedChanged(object sender, EventArgs e)
{
autoSizeCheckBox.Visible = !fitWidthCheckBox.Checked;
stretchCheckBox.Visible = fitWidthCheckBox.Checked;
}
}
}