Skip to content

Latest commit

 

History

History
163 lines (117 loc) · 5.93 KB

File metadata and controls

163 lines (117 loc) · 5.93 KB

OfficeIMO.Excel.Pdf - Excel to PDF export

nuget version nuget downloads

OfficeIMO.Excel.Pdf exports OfficeIMO.Excel workbooks to PDF through the first-party OfficeIMO.Pdf engine. It also imports logical PDF tables into editable Excel worksheets.

Install

dotnet add package OfficeIMO.Excel.Pdf

Quick start

using OfficeIMO.Excel;
using OfficeIMO.Excel.Pdf;

using var workbook = ExcelDocument.Load("report.xlsx");
workbook.SaveAsPdf("report.pdf");

Examples

Export selected sheets with worksheet print settings

using OfficeIMO.Excel;
using OfficeIMO.Excel.Pdf;
using OfficeIMO.Pdf;

using var workbook = ExcelDocument.Load("monthly-report.xlsx");

var options = new ExcelPdfSaveOptions {
    SheetNames = new[] { "Summary", "Revenue", "Costs" },
    UseWorksheetPrintAreas = true,
    UseWorksheetPageSetup = true,
    UseWorksheetHeadersAndFooters = true,
    UseWorksheetPageBreaks = true,
    PageSize = PageSizes.A4.Landscape(),
    Margins = PageMargins.UniformCentimeters(1.2)
};

workbook.SaveAsPdf("monthly-report.pdf", options);

Export a workbook to bytes or a stream

using OfficeIMO.Excel;
using OfficeIMO.Excel.Pdf;

using var workbook = ExcelDocument.Load("statement.xlsx");

byte[] pdfBytes = workbook.ToPdf();

using var stream = File.Create("statement.pdf");
workbook.SaveAsPdf(stream);

Surface mapping warnings

using OfficeIMO.Excel;
using OfficeIMO.Excel.Pdf;
using OfficeIMO.Pdf;

using var workbook = ExcelDocument.Load("dashboard.xlsx");
var options = new ExcelPdfSaveOptions {
    IncludeSheetHeadings = true,
    RespectWorksheetHiddenRowsAndColumns = true,
    UseWorksheetCharts = true
}.UseProfile(PdfExportProfile.Faithful);

options.TextFallbacks = PdfTextFallbackFeatures.Default;
options.ResourcePolicy = PdfResourcePolicy.CreateTrustedHost();

var result = workbook.TrySaveAsPdf("dashboard.pdf", options);
if (!result.Succeeded) {
    foreach (string diagnostic in result.Diagnostics) {
        Console.WriteLine(diagnostic);
    }
}

foreach (var warning in result.Warnings) {
    Console.WriteLine($"{warning.Source}: {warning.Message}");
}

result.Report.RequireNoErrorWarnings();

Import structured PDF data

using OfficeIMO.Excel.Pdf;
using OfficeIMO.Pdf;

PdfDocument pdf = PdfDocument.Open("statement.pdf");
PdfExcelTableImportReport report = pdf.SaveTablesAsExcel("statement-tables.xlsx");

foreach (var table in report.Entries) {
    Console.WriteLine($"{table.SheetName}: page {table.PageNumber}");
}

Console.WriteLine($"Non-table page content detected: {report.HasOmittedPageContent}");

Import only selected PDF pages

using OfficeIMO.Excel.Pdf;
using OfficeIMO.Pdf;

PdfDocument pdf = PdfDocument.Open("bank-statement.pdf");
PdfLogicalDocument source = pdf.Read.Logical(
    PdfPageSelection.Parse("1-3"));

PdfExcelTableImportReport report = source.SaveTablesAsExcel(
    "bank-statement-q1.xlsx",
    new PdfExcelTableImportOptions {
        MaxRows = 250
    });

Console.WriteLine($"Imported {report.Entries.Count} table(s).");
report.RequireNoLoss(); // checks table-row truncation, not unrelated page content

What it maps

  • Workbook sheets, selected sheet lists, visible used ranges, print areas, page setup, margins, orientation, and worksheet page breaks.
  • Repeated print-title rows, headers, footers, page/date/time/sheet/workbook tokens, and supported header/footer images.
  • Cell display values, common number formats, fills, font emphasis, alignment, borders, merged cells, links, row heights, column widths, conditional fills/data bars/icons, and table layout primitives.
  • Supported worksheet images and common chart snapshots through shared OfficeIMO drawing primitives.
  • Deterministic profile presets through ExcelPdfSaveOptions.UseProfile(...). Applying a profile always resets the complete profile-owned option set, so reusing an options instance is history-independent.
  • Shared TextFallbacks and ResourcePolicy controls for Unicode, symbols, emoji, and host-resource trust. The balanced default uses installed fonts while denying arbitrary local and remote reads; portable deterministic mode is explicit.
  • Source-faithful zero-options output: worksheet-name headings are opt-in through IncludeSheetHeadings.
  • Per-operation conversion warnings through PdfDocumentConversionResult.Report or PdfSaveResult.Report.

Current limits

  • Workbook content is read through OfficeIMO.Excel; layout and PDF writing use OfficeIMO.Pdf.
  • PDF import is structured-data recovery. It reconstructs detected tables as worksheets; SourceScope and HasOmittedPageContent report text, source vector graphics, images, links, forms, annotations, or actions that are not represented by those tables.
  • The current reverse route recovers detected tables and structured values; arbitrary PDF page art is reported rather than presented as an editable workbook. Open recovery work is tracked in the repository roadmap.

Related packages

Targets and license

Dependency footprint

  • External: None beyond the dependencies of its OfficeIMO format packages; no native or commercial PDF renderer.
  • OfficeIMO: OfficeIMO.Excel, OfficeIMO.Pdf, and OfficeIMO.Core own layout mapping, rendering, table recovery, and reports.

See the complete OfficeIMO package map for related formats and conversion paths.