2017-11-10 03:14:09 +08:00
namespace UglyToad.Pdf
{
using System ;
using Content ;
using Cos ;
using IO ;
2017-12-23 07:54:54 +08:00
using Logging ;
2017-11-10 03:14:09 +08:00
using Parser ;
using Util.JetBrains.Annotations ;
2018-01-07 20:37:48 +08:00
/// <inheritdoc />
/// <summary>
/// Provides access to document level information for this PDF document as well as access to the <see cref="T:UglyToad.Pdf.Content.Page" />s contained in the document.
/// </summary>
2017-11-10 03:14:09 +08:00
public class PdfDocument : IDisposable
{
[NotNull]
private readonly IRandomAccessRead reader ;
[NotNull]
private readonly HeaderVersion version ;
[NotNull]
private readonly CrossReferenceTable crossReferenceTable ;
2017-12-23 07:54:54 +08:00
private readonly ILog log ;
2017-11-10 03:14:09 +08:00
private readonly bool isLenientParsing ;
[NotNull]
private readonly ParsingCachingProviders cachingProviders ;
[NotNull]
2017-12-28 21:14:03 +08:00
internal Catalog Catalog { get ; }
2017-11-10 03:14:09 +08:00
[NotNull]
2017-12-28 21:14:03 +08:00
internal Pages Pages { get ; }
2018-01-03 07:26:58 +08:00
[NotNull]
public DocumentInformation Information { get ; }
2018-01-04 04:15:25 +08:00
/// <summary>
/// The version number of the PDF specification which this file conforms to, for example 1.4.
/// </summary>
public decimal Version = > version . Version ;
2017-12-28 21:14:03 +08:00
/// <summary>
/// Get the number of pages in this document.
/// </summary>
public int NumberOfPages = > Pages . Count ;
2017-11-10 03:14:09 +08:00
2017-12-23 07:54:54 +08:00
internal PdfDocument ( ILog log , IRandomAccessRead reader , HeaderVersion version , CrossReferenceTable crossReferenceTable ,
2017-11-10 03:14:09 +08:00
bool isLenientParsing ,
ParsingCachingProviders cachingProviders ,
2017-12-23 07:54:54 +08:00
IPageFactory pageFactory ,
IPdfObjectParser pdfObjectParser ,
2018-01-03 07:26:58 +08:00
Catalog catalog ,
DocumentInformation information )
2017-11-10 03:14:09 +08:00
{
2017-12-23 07:54:54 +08:00
this . log = log ;
2017-11-10 03:14:09 +08:00
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 ) ) ;
2018-01-03 07:26:58 +08:00
Information = information ? ? throw new ArgumentNullException ( nameof ( information ) ) ;
2017-11-10 03:14:09 +08:00
Catalog = catalog ? ? throw new ArgumentNullException ( nameof ( catalog ) ) ;
2017-12-23 07:54:54 +08:00
Pages = new Pages ( log , Catalog , pdfObjectParser , pageFactory , reader , isLenientParsing ) ;
2017-11-10 03:14:09 +08:00
}
2018-01-09 06:43:48 +08:00
/// <summary>
/// Creates a <see cref="PdfDocument"/> for reading from the provided file bytes.
/// </summary>
/// <param name="fileBytes">The bytes of the PDF file.</param>
/// <param name="options">Optional parameters controlling parsing.</param>
/// <returns>A <see cref="PdfDocument"/> providing access to the file contents.</returns>
2017-11-10 03:14:09 +08:00
public static PdfDocument Open ( byte [ ] fileBytes , ParsingOptions options = null ) = > PdfDocumentFactory . Open ( fileBytes , options ) ;
2018-01-09 06:43:48 +08:00
/// <summary>
/// Opens a file and creates a <see cref="PdfDocument"/> for reading from the provided file path.
/// </summary>
/// <param name="filePath">The full path to the file location of the PDF file.</param>
/// <param name="options">Optional parameters controlling parsing.</param>
/// <returns>A <see cref="PdfDocument"/> providing access to the file contents.</returns>
public static PdfDocument Open ( string filePath , ParsingOptions options = null ) = > PdfDocumentFactory . Open ( filePath , options ) ;
2017-11-10 03:14:09 +08:00
2017-12-28 21:14:03 +08:00
/// <summary>
/// Get the page with the specified page number.
/// </summary>
/// <param name="pageNumber">The number of the page to return, this starts from 1.</param>
/// <returns>The page.</returns>
public Page GetPage ( int pageNumber )
{
return Pages . GetPage ( pageNumber ) ;
}
2017-11-10 03:14:09 +08:00
public void Dispose ( )
{
try
{
reader . Dispose ( ) ;
}
catch
{
// TODO: something
}
}
}
}