Add Excel Workbooks to PDF Documents

An Excel workbook can be rendered in the pages of a PDF document created with the PdfDocument class, between the text, images and other elements added from code. The worksheets start at a position in the current page and flow to as many new pages as they need, laid out inside the margins of the PDF document. The worksheets keep the formatting, the images and the shapes of the workbook.

The workbook is given to a PdfExcelElement object created from a byte array or from a file path and rendered with the ExcelPdfDocumentExtensionsAddExcel(PdfDocument, PdfExcelElement) extension method of the PDF document. The method returns a PdfContentRenderInfo object describing the pages and the areas where the content was rendered. The ExcelPdfDocumentExtensionsAddExcelAsync(PdfDocument, PdfExcelElement, CancellationToken) method is the asynchronous variant.

Position, Width and Zoom of the Content

The X and Y properties give the position where the content starts, in points and relative to the top left corner of the content area of the current page, inside the margins. The content keeps the left offset on the next pages, where it starts at the top margin. Width is the width of the content in points, or 0 to extend it to the right margin of the page. Zoom scales the rendered content, in percents from 10 to 200, with 100 as the default.

Add an Excel Workbook to the Current Page
PdfExcelElement excelElement = new PdfExcelElement(inputExcelBytes)
{
    X = 0,
    Y = crtYPos,
    // 0 extends the content to the right margin of the page
    Width = 0,
    Zoom = 100
};

PdfContentRenderInfo excelRenderInfo = pdfDocument.AddExcel(excelElement);

Worksheet Options

The element has the worksheet options of the Excel to PDF Converter. KeepColumnWidths and KeepRowHeights keep the column widths and the row heights of the worksheets or let the cells take the size of their content. ConvertOnlyFirstWorksheet renders only the first worksheet, PageBreakBetweenWorksheets starts each worksheet on a new page and DisplayWorksheetTitle renders the title of each worksheet above its content. The FontFiles and FallbackFontFiles lists give more fonts to the content, in addition to the fonts installed on the machine.

The Rendering Result

The returned PdfContentRenderInfo object exposes the FirstPageNumber, LastPageNumber and PagesCount of the content and the area it covers on each page in RenderedRectangles. The elements added after the content go to its last page, below the LastPageRectangle.

Code Sample - Add Excel Workbooks to PDF Documents

C#
// Use EVO PDF Namespace
using EvoPdf.Next;

public byte[] AddExcelToPdfDocument(byte[] inputExcelBytes, string fontFilePath)
{
    // Set the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
    // Licensing.LicenseKey = "your-license-key";

    PdfDocumentCreateSettings pdfCreateSettings = new PdfDocumentCreateSettings()
    {
        PageSize = PdfPageSize.A4,
        PageOrientation = PdfPageOrientation.Portrait,
        Margins = new PdfMargins(36, 36, 36, 36),
        Language = "en-US"
    };

    using PdfDocument pdfDocument = new PdfDocument(pdfCreateSettings);
    pdfDocument.PdfDocumentInfo.Title = "Excel Content in a PDF Document";

    PdfBaseFont baseFont = PdfFontManager.CreateBaseFont(fontFilePath);
    PdfFont titleFont = PdfFontManager.CreateFont(baseFont, 16f, PdfFontStyle.Bold, PdfColor.DarkBlue);
    PdfFont textFont = PdfFontManager.CreateFont(baseFont, 10f, PdfFontStyle.Normal, PdfColor.Black);

    // Elements created with the PDF creator before the document content
    PdfTextElement titleElement = new PdfTextElement("Excel Content in a PDF Document", titleFont)
    {
        X = 0,
        Y = 0,
        Width = pdfDocument.ContentWidth,
        Alignment = PdfTextAlignment.Center
    };
    titleElement.Accessibility.StructureType = PdfStructureType.Heading1;
    float crtYPos = pdfDocument.AddText(titleElement).LastPageRectangle.Bounds.Bottom + 10;

    PdfTextElement introElement = new PdfTextElement(
        "The text above and this paragraph are text elements of the PDF document. The worksheets of the Excel " +
        "workbook start below them and continue on new pages when they do not fit in this one. " +
        "The elements added after the Excel content go to its last page, below it.", textFont)
    {
        X = 0,
        Y = crtYPos,
        Width = pdfDocument.ContentWidth
    };
    crtYPos = pdfDocument.AddText(introElement).LastPageRectangle.Bounds.Bottom + 15;

    // The Excel workbook rendered in the PDF document, from the given position of the current
    // page and inside the page margins. The X position is kept on the next pages
    PdfExcelElement excelElement = new PdfExcelElement(inputExcelBytes)
    {
        X = 0,
        Y = crtYPos,
        // 0 extends the content to the right margin of the page
        Width = 0,
        Zoom = 100,
        // The column widths and the row heights of the worksheets are kept, or the cells take
        // the size of their content
        KeepColumnWidths = true,
        KeepRowHeights = true,
        // The worksheets to render and how they are separated
        ConvertOnlyFirstWorksheet = false,
        PageBreakBetweenWorksheets = true,
        DisplayWorksheetTitle = true
    };

    PdfContentRenderInfo excelRenderInfo = pdfDocument.AddExcel(excelElement);

    // The rendered area reported for the last page places the elements after the content
    crtYPos = excelRenderInfo.LastPageRectangle.Bounds.Bottom + 15;

    PdfRectangleElement separator = new PdfRectangleElement(0, crtYPos, pdfDocument.ContentWidth, 1)
    {
        FillColor = PdfColor.DarkBlue,
        BorderColor = null
    };
    pdfDocument.AddRectangle(separator);

    PdfTextElement closingElement = new PdfTextElement(
        string.Format("The Excel content was rendered on {0} page(s), from page {1} to page {2}. This text was added after it.",
            excelRenderInfo.PagesCount, excelRenderInfo.FirstPageNumber, excelRenderInfo.LastPageNumber), textFont)
    {
        X = 0,
        Y = crtYPos + 8,
        Width = pdfDocument.ContentWidth
    };
    pdfDocument.AddText(closingElement);

    // The buffer to receive the generated PDF document
    byte[] outPdfBuffer = pdfDocument.Save();

    return outPdfBuffer;
}

See Also