Skip to content

Latest commit

 

History

History
143 lines (104 loc) · 4.24 KB

File metadata and controls

143 lines (104 loc) · 4.24 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.SaveAsPdf();

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

Surface mapping warnings

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

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

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

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

Import PDF tables

using OfficeIMO.Excel.Pdf;

var results = PdfExcelTableConverterExtensions.SavePdfTablesAsExcel(
    "statement.pdf",
    "statement-tables.xlsx");

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

Import only selected PDF pages

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

var results = PdfExcelTableConverterExtensions.SavePdfTablesAsExcel(
    "bank-statement.pdf",
    "bank-statement-q1.xlsx",
    new PdfExcelTableImportOptions {
        PageRanges = new[] { new PdfPageRange(1, 3) },
        MaxRows = 250
    });

Console.WriteLine($"Imported {results.Count} table(s).");

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.
  • Conversion warnings through ExcelPdfSaveOptions.Warnings and ExcelPdfSaveOptions.ConversionReport.

Boundaries

  • Workbook reading stays in OfficeIMO.Excel.
  • PDF layout and writing stay in OfficeIMO.Pdf.
  • This package should remain a translation adapter, not a second PDF engine.
  • Fidelity gaps should be documented as warnings or deeper current-state notes, not hidden in marketing text.

Related packages

Targets and license