namespace UglyToad.PdfPig
{
using System;
using Content;
using Cos;
using IO;
using Logging;
using Parser;
using Util.JetBrains.Annotations;
///
///
/// Provides access to document level information for this PDF document as well as access to the s contained in the document.
///
public class PdfDocument : IDisposable
{
[NotNull]
private readonly IRandomAccessRead reader;
[NotNull]
private readonly HeaderVersion version;
[NotNull]
private readonly CrossReferenceTable crossReferenceTable;
private readonly ILog log;
private readonly bool isLenientParsing;
[NotNull]
private readonly ParsingCachingProviders cachingProviders;
[NotNull]
internal Catalog Catalog { get; }
[NotNull]
internal Pages Pages { get; }
///
/// The metadata associated with this document.
///
[NotNull]
public DocumentInformation Information { get; }
///
/// The version number of the PDF specification which this file conforms to, for example 1.4.
///
public decimal Version => version.Version;
///
/// Get the number of pages in this document.
///
public int NumberOfPages => Pages.Count;
internal PdfDocument(ILog log, IRandomAccessRead reader, HeaderVersion version, CrossReferenceTable crossReferenceTable,
bool isLenientParsing,
ParsingCachingProviders cachingProviders,
IPageFactory pageFactory,
IPdfObjectParser pdfObjectParser,
Catalog catalog,
DocumentInformation information)
{
this.log = log;
this.reader = reader ?? throw new ArgumentNullException(nameof(reader));
this.version = version ?? throw new ArgumentNullException(nameof(version));
this.crossReferenceTable = crossReferenceTable ?? throw new ArgumentNullException(nameof(crossReferenceTable));
this.isLenientParsing = isLenientParsing;
this.cachingProviders = cachingProviders ?? throw new ArgumentNullException(nameof(cachingProviders));
Information = information ?? throw new ArgumentNullException(nameof(information));
Catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
Pages = new Pages(log, Catalog, pdfObjectParser, pageFactory, reader, isLenientParsing);
}
///
/// Creates a for reading from the provided file bytes.
///
/// The bytes of the PDF file.
/// Optional parameters controlling parsing.
/// A providing access to the file contents.
public static PdfDocument Open(byte[] fileBytes, ParsingOptions options = null) => PdfDocumentFactory.Open(fileBytes, options);
///
/// Opens a file and creates a for reading from the provided file path.
///
/// The full path to the file location of the PDF file.
/// Optional parameters controlling parsing.
/// A providing access to the file contents.
public static PdfDocument Open(string filePath, ParsingOptions options = null) => PdfDocumentFactory.Open(filePath, options);
///
/// Get the page with the specified page number.
///
/// The number of the page to return, this starts from 1.
/// The page.
public Page GetPage(int pageNumber)
{
return Pages.GetPage(pageNumber);
}
///
///
/// Dispose the and close any unmanaged resources.
///
public void Dispose()
{
try
{
reader.Dispose();
}
catch
{
// TODO: something
}
}
}
}