Export a workbook to PDF
Export worksheets to PDF and verify the result.
Requirements
- .NET Framework 4.8 for the released package path.
DRIT.SpreadsheetNuGet package.- No Microsoft Excel installation is required.
Run the example
dotnet run --project .\DRIT.Spreadsheet.Examples.Console\PdfExport\PdfExport.csproj -f net48
Source
Code
using DRIT.Pdf;
using DRIT.Spreadsheet;
using DRIT.Spreadsheet.Examples.Shared;
using DRIT.Spreadsheet.Export.Pdf;
var workbook = new Workbook();
var summary = workbook.Worksheets[0];
summary.Name = "Summary";
summary["A1"].Value = "Phase 4 PDF export";
summary["A2"].Value = 2026;
var details = workbook.AddWorksheet("Details");
details["A1"].Value = "Detail row";
var path = ExampleSupport.OutputPath("PdfExport.pdf");
workbook.SaveAsPdf(path);
ExampleSupport.Require(File.Exists(path), "The PDF file was not created.");
ExampleSupport.Require(new FileInfo(path).Length > 0, "The PDF file is empty.");
using var pdf = Document.Load(path);
ExampleSupport.Require(pdf.Pages.Count >= 2, "The PDF should contain both visible worksheets.");
Console.WriteLine($"PDF saved with {pdf.Pages.Count} pages.");