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 Parser.Parts;
|
|
|
|
|
using Util;
|
|
|
|
|
using Util.JetBrains.Annotations;
|
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
/// <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,
|
2017-11-10 03:14:09 +08:00
|
|
|
|
Catalog catalog)
|
|
|
|
|
{
|
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));
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PdfDocument Open(byte[] fileBytes, ParsingOptions options = null) => PdfDocumentFactory.Open(fileBytes, options);
|
|
|
|
|
public static PdfDocument Open(string filename, ParsingOptions options = null) => PdfDocumentFactory.Open(filename, options);
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|