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
pdfbyte[]The document to take pages out of. It is not modified.
firstPageint1-based, because that is how a reader numbers pages.
countintHow many pages to take, starting at
firstPage.
Returns
- byte[]
Exceptions
- ArgumentNullException
pdfis 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
sourceStreamfirstPageint1-based, because that is how a reader numbers pages.
countintHow many pages to take, starting at
firstPage.destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
pdfis null.- ArgumentOutOfRangeException
The range is not entirely inside the document.
- DocumentConversionException
The bytes are not a readable PDF.
- OperationCanceledException
ctwas cancelled.
Merge(IEnumerable<byte[]>)
Joins pdfs into one document, keeping the order given.
byte[] Merge(IEnumerable<byte[]> pdfs)
Parameters
pdfsIEnumerable<byte[]>
Returns
- byte[]
Exceptions
- ArgumentNullException
pdfsis null.- ArgumentException
pdfsis 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
sourcesIEnumerable<Stream>destinationStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
pdfsis null.- ArgumentException
pdfsis 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
ctwas cancelled.
PageCount(byte[])
The number of pages in pdf.
int PageCount(byte[] pdf)
Parameters
pdfbyte[]
Returns
Exceptions
- ArgumentNullException
pdfis 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
sourceStreamctCancellationToken
Returns
Exceptions
- ArgumentNullException
pdfis null.- DocumentConversionException
The bytes are not a readable PDF.
- OperationCanceledException
ctwas cancelled.
ReadMetadata(byte[])
The document information pdf carries.
PdfMetadata ReadMetadata(byte[] pdf)
Parameters
pdfbyte[]
Returns
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
pdfis 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
pdfbyte[]metadataPdfMetadata
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.