Add GetPage<TPage> and AddPageFactory<TPage and TPageFactory> methods

This commit is contained in:
BobLd
2023-11-02 08:12:46 +00:00
parent 3fbf8aaa6c
commit 3a96af3dcd
4 changed files with 692 additions and 6 deletions

View File

@@ -112,7 +112,7 @@
/// <param name="options">Optional parameters controlling parsing.</param>
/// <returns>A <see cref="PdfDocument"/> providing access to the file contents.</returns>
public static PdfDocument Open(byte[] fileBytes, ParsingOptions options = null) => PdfDocumentFactory.Open(fileBytes, options);
/// <summary>
/// Opens a file and creates a <see cref="PdfDocument"/> for reading from the provided file path.
/// </summary>
@@ -133,6 +133,26 @@
/// <returns>A <see cref="PdfDocument"/> providing access to the file contents.</returns>
public static PdfDocument Open(Stream stream, ParsingOptions options = null) => PdfDocumentFactory.Open(stream, options);
/// <summary>
/// TODO
/// </summary>
/// <typeparam name="TPage"></typeparam>
/// <param name="pageFactory"></param>
public void AddPageFactory<TPage>(IPageFactory<TPage> pageFactory)
{
pages.AddPageFactory(pageFactory);
}
/// <summary>
/// TODO
/// </summary>
/// <typeparam name="TPage"></typeparam>
/// <typeparam name="TPageFactory"></typeparam>
public void AddPageFactory<TPage, TPageFactory>() where TPageFactory : IPageFactory<TPage>
{
pages.AddPageFactory<TPage, TPageFactory>();
}
/// <summary>
/// Get the page with the specified page number (1 indexed).
/// </summary>
@@ -162,6 +182,36 @@
}
}
/// <summary>
/// Get the page with the specified page number (1 indexed), using the specified page factory.
/// </summary>
/// <typeparam name="TPage"></typeparam>
/// <param name="pageNumber">The number of the page to return, this starts from 1.</param>
/// <returns>The page.</returns>
public TPage GetPage<TPage>(int pageNumber)
{
if (isDisposed)
{
throw new ObjectDisposedException("Cannot access page after the document is disposed.");
}
parsingOptions.Logger.Debug($"Accessing page {pageNumber}.");
try
{
return pages.GetPage<TPage>(pageNumber, namedDestinations, parsingOptions);
}
catch (Exception ex)
{
if (IsEncrypted)
{
throw new PdfDocumentEncryptedException("Document was encrypted which may have caused error when retrieving page.", encryptionDictionary, ex);
}
throw;
}
}
/// <summary>
/// Gets all pages in this document in order.
/// </summary>
@@ -173,6 +223,17 @@
}
}
/// <summary>
/// Gets all pages in this document in order, using the specified page factory.
/// </summary>
public IEnumerable<TPage> GetPages<TPage>()
{
for (var i = 0; i < NumberOfPages; i++)
{
yield return GetPage<TPage>(i + 1);
}
}
/// <summary>
/// Get the document level metadata if present.
/// The metadata is XML in the (Extensible Metadata Platform) XMP format.
@@ -247,6 +308,7 @@
Advanced.Dispose();
pdfScanner.Dispose();
inputBytes.Dispose();
pages.Dispose();
}
catch (Exception ex)
{