-
Notifications
You must be signed in to change notification settings - Fork 305
Save As PDF
This feature allows EPPlus to save a workbook, worksheets and ranges to PDF.
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
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.
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");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");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");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);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);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");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);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 |
To configure Header and footer see Header and Footer
- [Sample](Link to sample here)
- Header and Footer
- Issues
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles