mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-19 19:07:56 +08:00
make bookmarknode immutable and use scanner when retrieving bookmarks
This commit is contained in:
@@ -40,5 +40,15 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanGetBookmarks()
|
||||
{
|
||||
using (var document = PdfDocument.Open(GetFilename()))
|
||||
{
|
||||
var foundBookmarks = document.TryGetBookmarks(out var bookmarks);
|
||||
Assert.True(foundBookmarks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,8 @@ using UglyToad.PdfPig.Geometry;
|
||||
|
||||
namespace UglyToad.PdfPig.Outline
|
||||
{
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// A node in the <see cref="Bookmarks"/> of a PDF document.
|
||||
/// </summary>
|
||||
@@ -11,55 +13,60 @@ namespace UglyToad.PdfPig.Outline
|
||||
/// <summary>
|
||||
/// The text displayed for this node.
|
||||
/// </summary>
|
||||
public string Title { get; internal set; }
|
||||
public string Title { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The bookmark's coordinates in the pdf page.
|
||||
/// </summary>
|
||||
public PdfPoint TopLeft { get; internal set; }
|
||||
public PdfPoint TopLeft { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The bookmark's bounding box in the pdf page.
|
||||
/// </summary>
|
||||
public PdfRectangle BoundingBox { get; internal set; }
|
||||
public PdfRectangle BoundingBox { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The node's hierarchical level.
|
||||
/// </summary>
|
||||
public int Level { get; internal set; }
|
||||
public int Level { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The page's number where the bookmark is located.
|
||||
/// The page number where the bookmark is located.
|
||||
/// </summary>
|
||||
public int PageNumber { get; internal set; }
|
||||
public int PageNumber { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The link to an external source.
|
||||
/// </summary>
|
||||
public string ExternalLink { get; internal set; }
|
||||
public string ExternalLink { get; }
|
||||
|
||||
/// <summary>
|
||||
/// True if bookmark refers to an external source.
|
||||
/// </summary>
|
||||
public bool IsExternal { get; internal set; }
|
||||
public bool IsExternal { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The bookmark's sub-bookmark.
|
||||
/// </summary>
|
||||
public List<BookmarkNode> Children { get; internal set; }
|
||||
public IReadOnlyList<BookmarkNode> Children { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of BookmarkNode.
|
||||
/// </summary>
|
||||
public BookmarkNode()
|
||||
/// <inheritdoc />
|
||||
public BookmarkNode(string title, PdfPoint topLeft, PdfRectangle boundingBox, int level, int pageNumber,
|
||||
string externalLink,
|
||||
bool isExternal,
|
||||
IReadOnlyList<BookmarkNode> children)
|
||||
{
|
||||
Title = string.Empty;
|
||||
Children = new List<BookmarkNode>();
|
||||
Title = title;
|
||||
TopLeft = topLeft;
|
||||
BoundingBox = boundingBox;
|
||||
Level = level;
|
||||
PageNumber = pageNumber;
|
||||
ExternalLink = externalLink;
|
||||
IsExternal = isExternal;
|
||||
Children = children ?? throw new ArgumentNullException(nameof(children));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return "page #" + PageNumber + ", " + Level + ", " + Title;
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -21,6 +21,7 @@
|
||||
using Graphics;
|
||||
using IO;
|
||||
using Logging;
|
||||
using Outline;
|
||||
using Parts;
|
||||
using Parts.CrossReference;
|
||||
using Tokenization.Scanner;
|
||||
@@ -136,12 +137,14 @@
|
||||
var caching = new ParsingCachingProviders(bruteForceSearcher, resourceContainer);
|
||||
|
||||
var acroFormFactory = new AcroFormFactory(pdfScanner, filterProvider);
|
||||
var bookmarksProvider = new BookmarksProvider(log, pdfScanner, isLenientParsing);
|
||||
|
||||
return new PdfDocument(log, inputBytes, version, crossReferenceTable, isLenientParsing, caching, pageFactory, catalog, information,
|
||||
encryptionDictionary,
|
||||
pdfScanner,
|
||||
filterProvider,
|
||||
acroFormFactory);
|
||||
acroFormFactory,
|
||||
bookmarksProvider);
|
||||
}
|
||||
|
||||
private static (IndirectReference, DictionaryToken) ParseTrailer(CrossReferenceTable crossReferenceTable, bool isLenientParsing, IPdfTokenScanner pdfTokenScanner,
|
||||
|
@@ -14,7 +14,7 @@
|
||||
using Parser;
|
||||
using Tokenization.Scanner;
|
||||
using Tokens;
|
||||
using UglyToad.PdfPig.Outline;
|
||||
using Outline;
|
||||
using Util.JetBrains.Annotations;
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -45,6 +45,7 @@
|
||||
private readonly IPdfTokenScanner pdfScanner;
|
||||
|
||||
private readonly IFilterProvider filterProvider;
|
||||
private readonly BookmarksProvider bookmarksProvider;
|
||||
|
||||
[NotNull]
|
||||
private readonly Pages pages;
|
||||
@@ -88,7 +89,8 @@
|
||||
EncryptionDictionary encryptionDictionary,
|
||||
IPdfTokenScanner pdfScanner,
|
||||
IFilterProvider filterProvider,
|
||||
AcroFormFactory acroFormFactory)
|
||||
AcroFormFactory acroFormFactory,
|
||||
BookmarksProvider bookmarksProvider)
|
||||
{
|
||||
this.log = log;
|
||||
this.inputBytes = inputBytes;
|
||||
@@ -98,6 +100,7 @@
|
||||
this.encryptionDictionary = encryptionDictionary;
|
||||
this.pdfScanner = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
|
||||
this.filterProvider = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
|
||||
this.bookmarksProvider = bookmarksProvider ?? throw new ArgumentNullException(nameof(bookmarksProvider));
|
||||
Information = information ?? throw new ArgumentNullException(nameof(information));
|
||||
pages = new Pages(catalog, pageFactory, isLenientParsing, pdfScanner);
|
||||
Structure = new Structure(catalog, crossReferenceTable, pdfScanner);
|
||||
@@ -209,8 +212,7 @@
|
||||
throw new ObjectDisposedException("Cannot access the bookmarks after the document is disposed.");
|
||||
}
|
||||
|
||||
var bookmarksProvider = new BookmarksProvider(this.log, this.Structure);
|
||||
bookmarks = bookmarksProvider.GetBookmarks();
|
||||
bookmarks = bookmarksProvider.GetBookmarks(Structure.Catalog);
|
||||
if (bookmarks != null) return true;
|
||||
return false;
|
||||
}
|
||||
|
Reference in New Issue
Block a user