Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/

PDF Quick Start

Edit on GitHub

Create a PDF with headings, paragraphs, a table, and an image, then load it and read its text, with OfficeIMO.Pdf, PSWriteOffice, or the officeimo CLI.

OfficeIMO.Pdf writes and reads PDF files directly in .NET. It has no native dependencies and does not need a browser, printer driver, or Office installation.

dotnet add package OfficeIMO.Pdf

Create a PDF

PdfDocument.Create takes a builder callback. Content flows down the page and breaks onto new pages automatically.

C#

using OfficeIMO;
using OfficeIMO.Pdf;

byte[] logo = File.ReadAllBytes("logo.png");

PdfDocument.Create(pdf => pdf.Content(content => content
        .H1("Service review")
        .AddParagraph("All scheduled checks passed. Two follow-ups remain open.")
        .AddTable(new[] {
            new[] { "Task", "Owner", "Due" },
            new[] { "Rotate certificates", "Platform", "4 Nov" },
            new[] { "Renew backup licence", "Operations", "12 Nov" } })
        .AddImage(logo, OfficeLength.Points(120), OfficeLength.Points(48), OfficeAlignment.Right)),
        new PdfCreateOptions { CreateOutlineFromHeadings = true })
    .Meta(title: "Service review", author: "Operations")
    .Save("Service-review.pdf");

CreateOutlineFromHeadings turns headings into bookmarks. For rich text inside a paragraph, use the callback overload:

using OfficeIMO.Pdf;

PdfDocument.Create(pdf => pdf.Content(content => content
        .AddParagraph(p => {
            p.AddText("Status: ");
            p.AddText("Healthy").SetBold();
        })))
    .Save("Status.pdf");

PowerShell

$services = @(
    [pscustomobject]@{ Name = 'Directory'; Status = 'Healthy'; Incidents = 0 }
    [pscustomobject]@{ Name = 'Mail'; Status = 'Watch'; Incidents = 2 }
)

New-OfficePdf -Path .\Service-review.pdf -CreateOutlineFromHeadings {
    PdfHeading 'Service review'
    PdfParagraph 'All scheduled checks passed. Two follow-ups remain open.'
    PdfTable -InputObject $services
}

CLI

Convert an existing document instead of composing one:

officeimo convert report.docx report.pdf
officeimo convert notes.md notes.pdf

Load a PDF and read it

C#

using OfficeIMO.Pdf;

PdfDocument pdf = PdfDocument.Load("Service-review.pdf");
Console.WriteLine($"{pdf.Pages.Count} page(s)");
Console.WriteLine(pdf.ExtractText());

PowerShell

Read-OfficeDocument -Path .\Service-review.pdf -As Text

CLI

officeimo read Service-review.pdf --format markdown

Next steps

Limits and reference

Limits: see the PDF package row in the compatibility matrix.

Reference: OfficeIMO.Pdf API

API used on this page

Example source