Winnovative Software Logo

 HTML to PDF Converter - Excel Library - ASP.NET Charts - RTF to PDF Converter
 PDF Merge and Split - PDF Security - PDF Viewers - PDF to Text - Images Extractor

 
Skip Navigation Links
 
Excel Logo

Winnovative Excel To PDF Converter for .NET

Winnovative Excel to PDF Converter can be used in any type of .NET application to convert Excel documents to PDF. The integration with existing .NET applications is extremely easy and no installation is necessary in order to run the converter. The downloaded archive contains the assembly for .NET and demo applications for Excel to PDF conversion. The result of conversion can be produced in a memory buffer, in a file on disk or in a stream.

The Excel to PDF Converter does not require Microsoft Excel or other third party tools.

Features

Excel to PDF Converter for .NET Box
Convert Excel XLS and XLSX documents to PDF
Does not require Microsoft Excel or other third party tools
Convert to memory buffer, file, stream or to a PDF object for further processing
Convert all the worksheets or select the worksheets in workbook to convert
Set PDF page size to a standard (A4, etc.) or a custom size
Set PDF page orientation and PDF document margins
Add headers and footers with page numbering to PDF pages
Append or prepend external PDF files to conversion result
Password protect and set permissions of the PDF document
Add a digital signature to generated PDF document
Add graphic elements to generated PDF document
Generate PDF/A and PDF/X compliant documents
Generate CMYK and Gray Scale PDF documents
Edit existing PDF documents
Merge multiple PDF documents in a single PDF document
Split a PDF document in multiple PDF documents
Support for .NET 4.0 framework and later
Documentation and C# samples for all the features


Code Sample for Excel to PDF Conversion

The code below was taken from the Excel to PDF demo application available for download in the Excel to PDF Converter archive. In this example an instance of the ExcelToPdfConverter class is constructed and used to convert an Excel 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 btnExcelToPdf_Click(object sender, EventArgs e)
{
    // Create an Excel to PDF converter object with default settings
    ExcelToPdfConverter excelToPdfConverter = new ExcelToPdfConverter();

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

    Cursor = Cursors.WaitCursor;

    string outPdfFile = @"DemoAppFiles\Output\ExcelToPdf.pdf";
    try
    {
        string excelFile = excelFilePathTextBox.Text;

        // Convert the Excel document to a PDF document
        byte[] outPdfBuffer = excelToPdfConverter.ConvertExcelFile(excelFile);

        // Write the memory buffer in a PDF file
        System.IO.File.WriteAllBytes(outPdfFile, outPdfBuffer);
    }
    catch (Exception ex)
    {
        // The Excel to PDF conversion failed
        MessageBox.Show(String.Format("Excel 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));
    }
}

/// <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 = System.IO.Path.Combine(Application.StartupPath,
                @"DemoAppFiles\Input\Images\logo.jpg");

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

    // Set header background color
    excelToPdfConverter.PdfHeaderOptions.HeaderBackColor = Color.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 System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));
    // Align the text at the right of the footer
    headerText.TextAlign = HorizontalTextAlign.Right;
    // Set text color
    headerText.ForeColor = Color.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 = Color.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 = System.IO.Path.Combine(Application.StartupPath,
                @"DemoAppFiles\Input\Images\logo.jpg");

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

    // Set footer background color
    excelToPdfConverter.PdfFooterOptions.FooterBackColor = Color.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 System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));

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

        // Set page numbering text color
        footerText.ForeColor = Color.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 = Color.Gray;

        // Add line element to the bottom of the footer
        excelToPdfConverter.PdfFooterOptions.AddElement(footerLine);
    }
}