Skip to content

Commit

Permalink
Make AnnotationProvider public
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Mar 16, 2024
1 parent 8163d9f commit bf6c519
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.Annotations.AnnotationFlags",
"UglyToad.PdfPig.Annotations.AnnotationType",
"UglyToad.PdfPig.Annotations.AppearanceStream",
"UglyToad.PdfPig.Annotations.AnnotationProvider",
"UglyToad.PdfPig.Annotations.QuadPointsQuadrilateral",
"UglyToad.PdfPig.Content.ArtifactMarkedContentElement",
"UglyToad.PdfPig.Content.BasePageFactory`1",
Expand Down
31 changes: 23 additions & 8 deletions src/UglyToad.PdfPig/Annotations/AnnotationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
using Tokens;
using Util;

internal class AnnotationProvider
/// <summary>
/// Annotation provider.
/// </summary>
public class AnnotationProvider
{
private readonly IPdfTokenScanner tokenScanner;
private readonly DictionaryToken pageDictionary;
private readonly NamedDestinations namedDestinations;
private readonly ILog log;
private readonly TransformationMatrix matrix;

public AnnotationProvider(IPdfTokenScanner tokenScanner, DictionaryToken pageDictionary,
TransformationMatrix matrix, NamedDestinations namedDestinations, ILog log)
/// <summary>
/// Create a <see cref="AnnotationProvider"/>.
/// </summary>
public AnnotationProvider(IPdfTokenScanner tokenScanner,
DictionaryToken pageDictionary,
TransformationMatrix matrix,
NamedDestinations namedDestinations,
ILog log)
{
this.matrix = matrix;
this.tokenScanner = tokenScanner ?? throw new ArgumentNullException(nameof(tokenScanner));
Expand All @@ -30,6 +39,9 @@ public AnnotationProvider(IPdfTokenScanner tokenScanner, DictionaryToken pageDic
this.log = log;
}

/// <summary>
/// Get the annotations.
/// </summary>
public IEnumerable<Annotation> GetAnnotations()
{
var lookupAnnotations = new Dictionary<IndirectReference, Annotation>();
Expand All @@ -43,7 +55,7 @@ public IEnumerable<Annotation> GetAnnotations()
{
if (!DirectObjectFinder.TryGet(token, tokenScanner, out DictionaryToken annotationDictionary))
{
continue;
continue;
}

Annotation replyTo = null;
Expand All @@ -56,20 +68,23 @@ public IEnumerable<Annotation> GetAnnotations()
var type = annotationDictionary.Get<NameToken>(NameToken.Subtype, tokenScanner);
var annotationType = type.ToAnnotationType();
var action = GetAction(annotationDictionary);
var rectangle = matrix.Transform(annotationDictionary.Get<ArrayToken>(NameToken.Rect, tokenScanner).ToRectangle(tokenScanner));
var rectangle = matrix.Transform(annotationDictionary.Get<ArrayToken>(NameToken.Rect, tokenScanner)
.ToRectangle(tokenScanner));
var contents = GetNamedString(NameToken.Contents, annotationDictionary);
var name = GetNamedString(NameToken.Nm, annotationDictionary);
// As indicated in PDF reference 8.4.1, the modified date can be anything, but is usually a date formatted according to sec. 3.8.3
var modifiedDate = GetNamedString(NameToken.M, annotationDictionary);

var flags = (AnnotationFlags)0;
if (annotationDictionary.TryGet(NameToken.F, out var flagsToken) && DirectObjectFinder.TryGet(flagsToken, tokenScanner, out NumericToken flagsNumericToken))
if (annotationDictionary.TryGet(NameToken.F, out var flagsToken) &&
DirectObjectFinder.TryGet(flagsToken, tokenScanner, out NumericToken flagsNumericToken))
{
flags = (AnnotationFlags)flagsNumericToken.Int;
}

var border = AnnotationBorder.Default;
if (annotationDictionary.TryGet(NameToken.Border, out var borderToken) && DirectObjectFinder.TryGet(borderToken, tokenScanner, out ArrayToken borderArray)
if (annotationDictionary.TryGet(NameToken.Border, out var borderToken) &&
DirectObjectFinder.TryGet(borderToken, tokenScanner, out ArrayToken borderArray)
&& borderArray.Length >= 3)
{
var horizontal = borderArray.GetNumeric(0).Data;
Expand Down Expand Up @@ -147,7 +162,7 @@ public IEnumerable<Annotation> GetAnnotations()
var annotation = new Annotation(
annotationDictionary,
annotationType,
rectangle,
rectangle,
contents,
name,
modifiedDate,
Expand Down

0 comments on commit bf6c519

Please sign in to comment.