Table of Contents

Interface IPdfEditor

Namespace
DocToolkit.Extensions.DependencyInjection
Assembly
DocToolkit.Extensions.DependencyInjection.dll

Operations on a PDF that already exists — page count, merge, page extraction and document information. Registered by AddDocToolkit(IServiceCollection, Action<DocToolkitOptions>?).

public interface IPdfEditor

Remarks

The only injectable service here that reads a PDF; every other one writes or renders. Nothing on it re-renders, so pages keep the text, fonts and images they arrived with.

Methods

ExtractPages(byte[], int, int)

A new document holding count pages starting at firstPage.

byte[] ExtractPages(byte[] pdf, int firstPage, int count)

Parameters

pdf byte[]

The document to take pages out of. It is not modified.

firstPage int

1-based, because that is how a reader numbers pages.

count int

How many pages to take, starting at firstPage.

Returns

byte[]

Exceptions

ArgumentNullException

pdf is null.

ArgumentOutOfRangeException

The range is not entirely inside the document.

DocumentConversionException

The bytes are not a readable PDF.

ExtractPagesAsync(Stream, int, int, Stream, CancellationToken)

A new document holding count pages starting at firstPage.

Task ExtractPagesAsync(Stream source, int firstPage, int count, Stream destination, CancellationToken ct = default)

Parameters

source Stream
firstPage int

1-based, because that is how a reader numbers pages.

count int

How many pages to take, starting at firstPage.

destination Stream
ct CancellationToken

Returns

Task

Exceptions

ArgumentNullException

pdf is null.

ArgumentOutOfRangeException

The range is not entirely inside the document.

DocumentConversionException

The bytes are not a readable PDF.

OperationCanceledException

ct was cancelled.

Merge(IEnumerable<byte[]>)

Joins pdfs into one document, keeping the order given.

byte[] Merge(IEnumerable<byte[]> pdfs)

Parameters

pdfs IEnumerable<byte[]>

Returns

byte[]

Exceptions

ArgumentNullException

pdfs is null.

ArgumentException

pdfs is empty; merging nothing would produce a zero-page PDF, which several readers refuse to open.

DocumentConversionException

One of the inputs is not a readable PDF.

MergeAsync(IEnumerable<Stream>, Stream, CancellationToken)

Joins pdfs into one document, keeping the order given.

Task MergeAsync(IEnumerable<Stream> sources, Stream destination, CancellationToken ct = default)

Parameters

sources IEnumerable<Stream>
destination Stream
ct CancellationToken

Returns

Task

Exceptions

ArgumentNullException

pdfs is null.

ArgumentException

pdfs is empty; merging nothing would produce a zero-page PDF, which several readers refuse to open.

DocumentConversionException

One of the inputs is not a readable PDF.

OperationCanceledException

ct was cancelled.

PageCount(byte[])

The number of pages in pdf.

int PageCount(byte[] pdf)

Parameters

pdf byte[]

Returns

int

Exceptions

ArgumentNullException

pdf is null.

DocumentConversionException

The bytes are not a readable PDF.

PageCountAsync(Stream, CancellationToken)

The number of pages in pdf.

Task<int> PageCountAsync(Stream source, CancellationToken ct = default)

Parameters

source Stream
ct CancellationToken

Returns

Task<int>

Exceptions

ArgumentNullException

pdf is null.

DocumentConversionException

The bytes are not a readable PDF.

OperationCanceledException

ct was cancelled.

ReadMetadata(byte[])

The document information pdf carries.

PdfMetadata ReadMetadata(byte[] pdf)

Parameters

pdf byte[]

Returns

PdfMetadata

Remarks

An absent entry reads back as null rather than an empty string, so "no title" stays distinguishable from "a title deliberately set to empty".

Exceptions

ArgumentNullException

pdf is null.

DocumentConversionException

The bytes are not a readable PDF.

WithMetadata(byte[], PdfMetadata)

A copy of pdf carrying metadata.

byte[] WithMetadata(byte[] pdf, PdfMetadata metadata)

Parameters

pdf byte[]
metadata PdfMetadata

Returns

byte[]

Remarks

A null property leaves what the document already had in place, so stamping a title does not silently erase an author. Pass an empty string to clear one.

Exceptions

ArgumentNullException

Either argument is null.

DocumentConversionException

The bytes are not a readable PDF.