You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SaveAsPdf throws NullReferenceException in EPPlusToPdfWriter.GetRanges when a worksheet has a single-area print area #2631
Linux x64 (Rocky Linux 8), .NET 8.0 (SDK 8.0.125); reproduced with both JIT and Native AOT
Epplus version
9.0.0-preview2
Spreadsheet application
Excel
Description
ExcelWorkbook.SaveAsPdf(string) throws a NullReferenceException when a worksheet's print area (_xlnm.Print_Area) is a single range, which is the usual case. A print area made of several ranges works, and so does a sheet without a print area.
Repro:
using OfficeOpenXml;
ExcelPackage.License.SetNonCommercialPersonal("repro");
using var package = new ExcelPackage();
var ws = package.Workbook.Worksheets.Add("Sheet1");
ws.Cells["A1"].Value = "Hello";
ws.Cells["B2"].Value = 42;
ws.PrinterSettings.PrintArea = ws.Cells["A1:B2"];
package.Workbook.SaveAsPdf("single-area.pdf"); // throws NullReferenceException
ws.PrinterSettings.PrintArea = ws.Cells["A1:A2,B1:B2"];
package.Workbook.SaveAsPdf("two-areas.pdf"); // works
Stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at OfficeOpenXml.Export.PdfExport.EPPlusToPdfWriter.GetRanges(ExcelWorksheet worksheet)
at OfficeOpenXml.Export.PdfExport.EPPlusToPdfWriter.GetPdfWorksheet(PdfPageSettings pageSettings, ExcelWorksheet worksheet)
at OfficeOpenXml.Export.PdfExport.EPPlusToPdfWriter.GetPdfWorksheets(PdfPageSettings pageSettings, ExcelWorksheet[] worksheets)
at OfficeOpenXml.Export.PdfExport.EPPlusToPdfWriter.HandleWorksheetCollection(PdfPageSettings pageSettings, ExcelWorksheet[] worksheets, Action`1 writePdf)
at OfficeOpenXml.Export.PdfExport.EPPlusToPdfWriter.<>c__DisplayClass7_0.<.ctor>b__0(Action`1 writePdf)
at OfficeOpenXml.Export.PdfExport.EPPlusToPdfWriter.Save(String fileName)
at OfficeOpenXml.ExcelWorkbook.SaveAsPdf(String fileName)
Cause: GetRanges (src/EPPlus/Export/PdfExport/EPPlusToPdfWriter.cs, around line 464 on develop9) loops over worksheet.Names["_xlnm.Print_Area"].Addresses.Count. But ExcelAddressBase.Addresses is only created when the address contains a comma (ExcelAddressBase.cs, around line 996), so it is null for a single area. Elsewhere, EPPlus handles this with Addresses == null || Addresses.Count < 2, or by calling GetAllAddresses(), which returns [this] for a single area.
Suggested fix:
var printArea = worksheet.Names["_xlnm.Print_Area"];
foreach (var address in printArea.GetAllAddresses())
{
ranges.Add(new PdfRange(worksheet.Cells[address.Address], false));
}
Workaround: call ExcelWorkbook.SaveAsPdf(string, ExcelRangeBase[]) and pass the print area ranges yourself, since that overload never calls GetRanges.
Activity
AdrianEPPlus commentedon Dec 1, 2026
Thank you for reporting this issue. A PR has been made with a fix.