Skip to content

Save As PDF

AdrianEPPlus edited this page Jul 3, 2026 · 1 revision

Overview

This feature allows EPPlus to save a workbook, worksheets and ranges to PDF.

Limitations

There are some limitations as to what can be saved to a PDF. Missing features may be added in future versions. Here are some missing features:

  • When saving a workbook, only the first worksheets printer setting is used for the whole document.
  • When saving multiple worksheets, only the first passed worksheets printer settings is used for the document.
  • Drawings - This includes all types of pictures, shapes, charts and so on.
  • Pivot tables
  • Vertical text

See our issue tracker for a complete list: https://github.com/EPPlusSoftware/EPPlus/issues?q=is%3Aissue%20state%3Aopen%20type%3AFeature%20pdf

Basic usage

There are several ways to save to PDF. Save methods exist in ExcelWorkbook, ExcelWorksheet and ExcelRangeBase. You can adjust settings by using the printer settings object on the worksheet. See Printer settings for options available.

You can also set header and footer using the Header and Footer component of the worksheet. See Header and Footer.

Save worksheet as PDF

To save a worksheet as a PDF you can use the SaveAsPdf method on the worksheet.

//Get the worksheet.
 var worksheet = workbook.Worksheets[0];
 //Save the worksheet as PDF.
 worksheet.SaveAsPdf("MyWorksheet.pdf");

You can adjust the PDF by setting options on the printer settings component of the worksheet

//Get the worksheet.
 var worksheet = workbook.Worksheets[0];
 //Set it to print in A3 in landscape.
 worksheet.PrinterSettings.PaperSize = ePaperSize.A3;
 worksheet.PrinterSettings.PaperSize = eOrientation.Landscape;
 //Save the worksheet as PDF.
 worksheet.SaveAsPdf("MyWorksheet.pdf");

Save range as PDF

To save a range as PDF you can use the SaveAsPdf method on the range.

//Get the worksheet.
 var worksheet = workbook.Worksheets[0];
//Setup printer settings on the worksheet.
worksheet.PrinterSettings.PaperSize = ePaperSize.A4;
worksheet.PrinterSettings.ShowGridLines = true;
worksheet.PrinterSettings.ShowHeaders = true;
//Get the range to export.
var range = worksheet.Cells["A1:D42"];
 //Save the range as pdf.
range.SaveAsPdf(outputFolder + "Sample 99.4.pdf");

Save workbook as PDF

To save a workbook as PDF you can use the SaveAsPdf method on the workbook.

//Get the workbook.
var workbook = package.Workbook;
//Currently only the first worksheets printer settings is used for the whole workbook(This will change in a future release).
workbook.Worksheets[0].PrinterSettings.PaperSize = ePaperSize.A3;
workbook.Worksheets[0].PrinterSettings.Orientation = eOrientation.Landscape;
//The workbook is saved as pdf.
workbook.SaveAsPdf(outputFolder + "Sample 99.2.pdf");

Save multiple worksheets as PDF

You can use the overload on the SaveAsPdf method on the workbook to select the worksheets you want to save to PDF.

//Get the workbook.
var workbook = p.Workbook;
//Get the worksheets
var worksheet1 = workbook.Worksheets[0];
var worksheet2 = workbook.Worksheets[0];
var worksheet3 = workbook.Worksheets[0];
//Currently only the first worksheets printer settings is used for the whole workbook(This will change in a future release).
workbook.Worksheets[0].PrinterSettings.PaperSize = ePaperSize.A4;
//Worksheet1 and worksheet2 will be saved into the same pdf. worksheet3 will be excluded.
workbook.SaveAsPdf(outputFolder + "Sample 99.2.pdf", worksheet1, worksheet2);

Save multiple ranges as PDF

You can use the overload on the SaveAsPdf on the workbook to select ranges you want to save to PDF.

//Get the worksheet
var worksheet = workbook.Worksheets[0];
//Setup printer settings on the worksheet.
worksheet.PrinterSettings.PaperSize = ePaperSize.A4;
worksheet.PrinterSettings.ShowGridLines = true;
worksheet.PrinterSettings.ShowHeaders = true;
//Get the ranges from the worksheet.
var range1 = worksheet.Cells["F1:H4"];
var range2 = worksheet.Cells["J1:M45"];
//Using the SaveAsPdf on the workbook allows multiple ranges.
workbook.SaveAsPdf(outputFolder + "Sample 99.3.pdf", range1, range2);

Save asynchronously

You can also save to PDF asynchronously. Example below shows how to do it with a worksheet.

//Get the worksheet.
var worksheet = workbook.Worksheets[0];
//Save with SaveAsPdfAsync.
await wb.SaveAsPdfAsync(outputFolder + "Sample 99.3.pdf");

Save using streams

You can save the pdf to a stream by using the overload on SaveToPdf on Workbook, worksheet and range.

//Get the worksheet
var worksheet = workbook.Worksheets[0];
//Create a memory stream.
using var ms = new MemoryStream();
//write the PDF to the stream.
worksheet.SaveAsPdf(ms);

You can save to a stream asynchronously too.

//Get the worksheet.
var worksheet = workbook.Worksheets[0];
//Create the stream.
using var ms = new MemoryStream();
//Write the PDFto stream asynchronously.
await worksheet.SaveAsPdfAsync(ms);

Printer Settings

There are many options you can control how the resulting PDF will look like.

Property / Method Type Description Implemented in PDF export
LeftMargin double Sets the left margin in inches Yes
RightMargin double Sets the right margin in inches Yes
TopMargin double Sets the top margin in inches Yes
BottomMargin double Sets the bottom margin in inches Yes
HeaderMargin double Sets the header margin in inches Yes
FooterMargin double Sets the footer margin in inches Yes
Orientation eOrientation Set orientation, either landscape or portrait Yes
FitToWidth int Scale the worksheet to fit the width of paper No
FitToHeight int Scale the worksheet to fit the height of paper No
Scale int Set scaling No
FitToPage bool Scale whole worksheet to fit the paper No
ShowHeaders bool Show row numbers and column letters Yes
RepeatRows ExcelAddress Row address to repeat at the start of each page Yes
RepeatColumns ExcelAddress Column to repeat at the start of each page Yes
PrintArea ExcelRangeBase Range of areas to print Yes
ShowGridLines bool If grid lines shoud be shown Yes
HorizontalCentered bool Center the content on the page horizontally No
VerticalCentered bool Center the content on the page vertically No
PageOrder ePageOrder Set page order to be Down, then over or Over then down. Yes
BlackAndWhite bool Turn off color No
Draft bool Set to be draft No
PaperSize ePaperSize Set the page size, Many formats are supported Yes
CellComments eCellComments Set to Hide, show comments and note at the end or on the page Yes, hide and show at end is supported
Errors ePrintError Set how to diplay errors. Yes
FirstPageNumber int The starting page number Yes

Header Footer

To configure Header and footer see Header and Footer

See also

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally