Example #1
0
        public virtual IList <PdfAnnotation> GetAnnotations()
        {
            IList <PdfAnnotation> annotations = new List <PdfAnnotation>();
            PdfArray annots = GetPdfObject().GetAsArray(PdfName.Annots);

            if (annots != null)
            {
                for (int i = 0; i < annots.Size(); i++)
                {
                    PdfDictionary annot = annots.GetAsDictionary(i);
                    annotations.Add(PdfAnnotation.MakeAnnotation(annot).SetPage(this));
                }
            }
            return(annotations);
        }
 public virtual void TestCopyingPageWithAnnotationContainingIrtKey()
 {
     NUnit.Framework.Assert.That(() => {
         // TODO remove expected exception and thus enable assertions when DEVSIX-3585 is implemented
         String inFilePath            = sourceFolder + "annotation-with-irt.pdf";
         String outFilePath           = destinationFolder + "copy-annotation-with-irt.pdf";
         PdfDocument originalDocument = new PdfDocument(new PdfReader(inFilePath));
         PdfDocument outDocument      = new PdfDocument(new PdfWriter(outFilePath));
         originalDocument.CopyPagesTo(1, 1, outDocument);
         // During the second copy call we have to rebuild/preserve all the annotation relationship (IRT in this case),
         // so that we don't end up with annotation on one page referring to an annotation on another page as its IRT
         // or as its parent
         originalDocument.CopyPagesTo(1, 1, outDocument);
         originalDocument.Close();
         outDocument.Close();
         outDocument = new PdfDocument(new PdfReader(outFilePath));
         for (int pageNum = 1; pageNum <= outDocument.GetNumberOfPages(); pageNum++)
         {
             PdfPage page = outDocument.GetPage(pageNum);
             NUnit.Framework.Assert.AreEqual(4, page.GetAnnotsSize());
             NUnit.Framework.Assert.AreEqual(4, page.GetAnnotations().Count);
             bool foundMarkupAnnotation = false;
             foreach (PdfAnnotation annotation in page.GetAnnotations())
             {
                 PdfDictionary annotationPageDict = annotation.GetPageObject();
                 if (annotationPageDict != null)
                 {
                     NUnit.Framework.Assert.AreSame(page.GetPdfObject(), annotationPageDict);
                 }
                 if (annotation is PdfMarkupAnnotation)
                 {
                     foundMarkupAnnotation   = true;
                     PdfDictionary inReplyTo = ((PdfMarkupAnnotation)annotation).GetInReplyToObject();
                     NUnit.Framework.Assert.IsTrue(page.ContainsAnnotation(PdfAnnotation.MakeAnnotation(inReplyTo)), "IRT reference must point to annotation present on the same page"
                                                   );
                 }
             }
             NUnit.Framework.Assert.IsTrue(foundMarkupAnnotation, "Markup annotation expected to be present but not found"
                                           );
         }
         outDocument.Close();
     }
                                 , NUnit.Framework.Throws.InstanceOf <AssertionException>())
     ;
 }
Example #3
0
        /// <summary>Copies page to the specified document.</summary>
        /// <remarks>
        /// Copies page to the specified document.
        /// <br/><br/>
        /// NOTE: Works only for pages from the document opened in reading mode, otherwise an exception is thrown.
        /// </remarks>
        /// <param name="toDocument">a document to copy page to.</param>
        /// <param name="copier">a copier which bears a specific copy logic. May be NULL</param>
        /// <returns>copied page.</returns>
        public virtual iText.Kernel.Pdf.PdfPage CopyTo(PdfDocument toDocument, IPdfPageExtraCopier copier)
        {
            PdfDictionary dictionary = GetPdfObject().CopyTo(toDocument, excludedKeys, true);

            iText.Kernel.Pdf.PdfPage page = new iText.Kernel.Pdf.PdfPage(dictionary);
            CopyInheritedProperties(page, toDocument);
            foreach (PdfAnnotation annot in GetAnnotations())
            {
                if (annot.GetSubtype().Equals(PdfName.Link))
                {
                    GetDocument().StoreLinkAnnotation(this, (PdfLinkAnnotation)annot);
                }
                else
                {
                    page.AddAnnotation(-1, PdfAnnotation.MakeAnnotation(((PdfDictionary)annot.GetPdfObject().CopyTo(toDocument
                                                                                                                    , false))), false);
                }
            }
            if (toDocument.IsTagged())
            {
                page.structParents = (int)toDocument.GetNextStructParentIndex();
                page.GetPdfObject().Put(PdfName.StructParents, new PdfNumber(page.structParents));
            }
            if (copier != null)
            {
                copier.Copy(this, page);
            }
            else
            {
                if (!toDocument.GetWriter().isUserWarnedAboutAcroFormCopying&& GetDocument().GetCatalog().GetPdfObject().
                    ContainsKey(PdfName.AcroForm))
                {
                    ILogger logger = LoggerFactory.GetLogger(typeof(iText.Kernel.Pdf.PdfPage));
                    logger.Warn(LogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY);
                    toDocument.GetWriter().isUserWarnedAboutAcroFormCopying = true;
                }
            }
            return(page);
        }