Click or drag to resize
Pdf Library for .NET

Convert Excel Workbooks to Pdf

Using the SelectPdf Excel to Pdf Converter for .NET, an Excel workbook can be converted to pdf with a few lines of code. The converter reads the formats Microsoft Excel writes - xlsx, xlsm, xltx, xltm, xls, xlt - plus comma and tab separated values, the Excel 2003 XML spreadsheet (SpreadsheetML) and markdown, whose tables become worksheets. The conversion runs in-process: no engine is launched.

Note  Note

The Excel to Pdf converter is available only in the full commercial SelectPdf library. It is not available in the free SelectPdf community edition.

The ExcelToPdf class exposes ConvertFile(String), ConvertStream(Stream) and ConvertBytes(Byte), each with an overload that names the ExcelFormat to read. Without one, the format is taken from the file extension, or detected from the content for a stream or a byte array (xlsx family, xls, SpreadsheetML, csv). Tab separated values and markdown have no signature and must be named when given as a stream.

Every visible sheet is converted, in workbook order, as Excel prints it: its print area, its page breaks and its own printed header and footer. Hidden sheets are left out. Worksheets converts only the sheets it names. Each sheet becomes an entry in the outline of the pdf (SheetBookmarks).

The result is a PdfDocument, exactly as the html converter returns one: it can be modified further, saved to a file, a stream or a byte array, and must be closed. The Options, Header and Footer properties mirror the html and Word converters', so a document setup written for HtmlToPdf applies here unchanged - page size and margins, header and footer bands, PDF/A and PDF/X standards, security, viewer preferences, document properties, compression, page ranges and single-page mode. The options are reviewed in Options Review.

Page setup: the sheets' own, or the converter's

Every sheet of a workbook has a page setup of its own, so PageSetup decides what the page size options mean:

  • Document (the default) prints every sheet on the paper size, orientation and margins its own page setup gives it, as Excel prints it. The page size and orientation options are ignored; the margins and the header and footer bands are added around the printed page. With zero margins and no bands every output page is the page Excel prints.

  • Options lays every sheet out on the page size, orientation and margins of the options, replacing the sheets' own paper and margins.

Layout decides how the content of a sheet is scaled onto its pages: the sheet's own print scaling (the default), its actual size, the whole sheet on one page, all its columns on the width of one page, or all its rows on the height of one page (ExcelLayout). GridLines draws or hides the cell gridlines; by default each sheet's print setting decides.

Sample Code

This sample converts two sheets of a workbook, each with all its columns fitted to the width of an A4 landscape page, under a header band and page numbers, and saves it as PDF/A-2b.

ExcelToPdf converter = new ExcelToPdf();

// lay the sheets out on the page set here, all the columns on one page
converter.Options.PageSetup = ExcelPageSetup.Options;
converter.Options.PdfPageSize = PdfPageSize.A4;
converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape;
converter.Options.MarginLeft = 20;
converter.Options.MarginRight = 20;
converter.Options.Layout = ExcelLayout.FitAllColumnsOnOnePage;
converter.Options.Worksheets = "Summary, Sales";
converter.Options.PdfStandard = PdfStandard.PdfA2B;

// a text header band and a page-number footer
converter.Options.DisplayHeader = true;
converter.Header.Height = 40;
converter.Header.Add(new PdfTextSection(0, 12, 500, "Quarterly figures",
    PdfFont.CreateStandardFont(PdfStandardFont.HelveticaBold, 11),
    new PdfColor(44, 62, 80)));
converter.Options.DisplayFooter = true;
converter.Footer.Height = 30;
converter.Footer.Add(new PdfTextSection(0, 10, 500,
    "Page {page_number} of {total_pages}",
    PdfFont.CreateStandardFont(PdfStandardFont.Helvetica, 9),
    new PdfColor(80, 80, 80)));

// the format is taken from the extension
PdfDocument doc = converter.ConvertFile("sales.xlsx");

// one outline entry per sheet
int sheets = doc.Bookmarks.Count;

doc.Save("sales.pdf");
doc.Close();

A csv file given as a stream must name its format:

ExcelToPdf converter = new ExcelToPdf();
converter.Options.GridLines = ExcelGridLines.Visible;

using (FileStream csv = File.OpenRead("orders.csv"))
{
    PdfDocument doc = converter.ConvertStream(csv, ExcelFormat.Csv);
    doc.Save("orders.pdf");
    doc.Close();
}
Fonts

Every font the workbook uses is embedded in the pdf as a subset. A font family the host does not have is served from the fonts the library bundles - the Liberation faces, metric-compatible with Arial, Times New Roman and Courier New - so the same workbook converts on a Windows server, a Linux container with no fonts installed, or a Mac. Scripts those faces do not cover (CJK, Arabic, Indic) need fonts installed on the host; AutoDetectComplexScript shapes complex scripts the workbook does not mark.

Archiving standards and accessibility

Every PDF/A level and PDF/X-1a are produced the way the html converter produces them, through PdfStandard.

Accessible (tagged) output is declared the way it is on the html and Word converters: Tagged builds the logical structure - the sheets' tables with their header cells, charts and pictures as figures with their alternative text, and links - and AccessibilityStandard declares PDF/UA-1 or PDF/UA-2 (which is written as PDF 2.0). A declared document needs a Title and a Language. PdfStandard.PdfA3A turns tagging on by itself, and combines with PDF/UA-1; PDF/A-4 is the archiving level that combines with PDF/UA-2.

A chart is described by its own alternative text, else by its title; give every picture and chart a description in Excel for a document that is accessible in substance and not only in form. Check the produced document at the level it declares with PdfValidator.Validate (Pdf Validation).

See Also