Exemplo n.º 1
0
 /// <summary>
 /// Create a new <see cref="Annotation"/>.
 /// </summary>
 public Annotation(DictionaryToken annotationDictionary, AnnotationType type, PdfRectangle rectangle, string content, string name, string modifiedDate,
                   AnnotationFlags flags, AnnotationBorder border)
 {
     AnnotationDictionary = annotationDictionary ?? throw new ArgumentNullException(nameof(annotationDictionary));
     Type         = type;
     Rectangle    = rectangle;
     Content      = content;
     Name         = name;
     ModifiedDate = modifiedDate;
     Flags        = flags;
     Border       = border;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Create a new <see cref="Annotation"/>.
 /// </summary>
 public Annotation(DictionaryToken annotationDictionary, AnnotationType type, PdfRectangle rectangle, string content, string name, string modifiedDate,
                   AnnotationFlags flags, AnnotationBorder border, IReadOnlyList <QuadPointsQuadrilateral> quadPoints)
 {
     AnnotationDictionary = annotationDictionary ?? throw new ArgumentNullException(nameof(annotationDictionary));
     Type         = type;
     Rectangle    = rectangle;
     Content      = content;
     Name         = name;
     ModifiedDate = modifiedDate;
     Flags        = flags;
     Border       = border;
     QuadPoints   = quadPoints ?? EmptyArray <QuadPointsQuadrilateral> .Instance;
 }
Exemplo n.º 3
0
        public IEnumerable <Annotation> GetAnnotations()
        {
            if (!pageDictionary.TryGet(NameToken.Annots, out IToken annotationsToken) ||
                !DirectObjectFinder.TryGet(annotationsToken, tokenScanner, out ArrayToken annotationsArray))
            {
                yield break;
            }

            foreach (var token in annotationsArray.Data)
            {
                if (!DirectObjectFinder.TryGet(token, tokenScanner, out DictionaryToken annotationDictionary))
                {
                    if (isLenientParsing)
                    {
                        continue;
                    }

                    throw new PdfDocumentFormatException($"The annotations dictionary contained an annotation which wasn't a dictionary: {token}.");
                }

                if (!isLenientParsing && annotationDictionary.TryGet(NameToken.Type, out NameToken dictionaryType))
                {
                    if (dictionaryType != NameToken.Annot)
                    {
                        throw new PdfDocumentFormatException($"The annotations dictionary contained a non-annotation type dictionary: {annotationDictionary}.");
                    }
                }

                var type = annotationDictionary.Get <NameToken>(NameToken.Subtype, tokenScanner);

                var annotationType = type.ToAnnotationType();
                var rectangle      = annotationDictionary.Get <ArrayToken>(NameToken.Rect, tokenScanner).ToRectangle(tokenScanner);

                var contents     = GetNamedString(NameToken.Contents, annotationDictionary);
                var name         = GetNamedString(NameToken.Nm, annotationDictionary);
                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))
                {
                    flags = (AnnotationFlags)flagsNumericToken.Int;
                }

                var border = AnnotationBorder.Default;
                if (annotationDictionary.TryGet(NameToken.Border, out var borderToken) && DirectObjectFinder.TryGet(borderToken, tokenScanner, out ArrayToken borderArray) &&
                    borderArray.Length >= 3)
                {
                    var horizontal = borderArray.GetNumeric(0).Data;
                    var vertical   = borderArray.GetNumeric(1).Data;
                    var width      = borderArray.GetNumeric(2).Data;
                    var dashes     = default(IReadOnlyList <decimal>);

                    if (borderArray.Length == 4 && borderArray.Data[4] is ArrayToken dashArray)
                    {
                        dashes = dashArray.Data.OfType <NumericToken>().Select(x => x.Data).ToList();
                    }

                    border = new AnnotationBorder(horizontal, vertical, width, dashes);
                }

                var quadPointRectangles = new List <QuadPointsQuadrilateral>();
                if (annotationDictionary.TryGet(NameToken.Quadpoints, tokenScanner, out ArrayToken quadPointsArray))
                {
                    var values = new List <decimal>();
                    for (var i = 0; i < quadPointsArray.Length; i++)
                    {
                        if (!(quadPointsArray[i] is NumericToken value))
                        {
                            continue;
                        }

                        values.Add(value.Data);

                        if (values.Count == 8)
                        {
                            quadPointRectangles.Add(new QuadPointsQuadrilateral(new[]
                            {
                                new PdfPoint(values[0], values[1]),
                                new PdfPoint(values[2], values[3]),
                                new PdfPoint(values[4], values[5]),
                                new PdfPoint(values[6], values[7])
                            }));

                            values.Clear();
                        }
                    }
                }

                yield return(new Annotation(annotationDictionary, annotationType, rectangle, contents, name, modifiedDate, flags, border,
                                            quadPointRectangles));
            }
        }