Exemplo n.º 1
0
        /// <summary>Removes an annotation from the page.</summary>
        /// <remarks>
        /// Removes an annotation from the page.
        /// <br /><br />
        /// NOTE: If document is tagged, PdfDocument's PdfTagStructure instance will point at annotation tag parent after method call.
        /// </remarks>
        /// <param name="annotation">an annotation to be removed.</param>
        /// <returns>this PdfPage instance.</returns>
        public virtual iText.Kernel.Pdf.PdfPage RemoveAnnotation(PdfAnnotation annotation)
        {
            PdfArray annots = GetAnnots(false);

            if (annots != null)
            {
                if (annots.Contains(annotation.GetPdfObject()))
                {
                    annots.Remove(annotation.GetPdfObject());
                }
                else
                {
                    annots.Remove(annotation.GetPdfObject().GetIndirectReference());
                }
                if (annots.IsEmpty())
                {
                    GetPdfObject().Remove(PdfName.Annots);
                }
            }
            if (GetDocument().IsTagged())
            {
                TagTreePointer tagPointer = GetDocument().GetTagStructureContext().RemoveAnnotationTag(annotation);
                if (tagPointer != null)
                {
                    bool standardAnnotTagRole = tagPointer.GetRole().Equals(PdfName.Annot) || tagPointer.GetRole().Equals(PdfName
                                                                                                                          .Form);
                    if (tagPointer.GetKidsRoles().Count == 0 && standardAnnotTagRole)
                    {
                        tagPointer.RemoveTag();
                    }
                }
            }
            return(this);
        }
Exemplo n.º 2
0
        public PdfObjRef(PdfAnnotation annot, PdfStructElem parent, int nextStructParentIndex)
            : base(new PdfDictionary(), parent)
        {
            annot.GetPdfObject().Put(PdfName.StructParent, new PdfNumber(nextStructParentIndex));
            annot.SetModified();
            PdfDictionary dict = (PdfDictionary)GetPdfObject();

            dict.Put(PdfName.Type, PdfName.OBJR);
            dict.Put(PdfName.Obj, annot.GetPdfObject());
        }
Exemplo n.º 3
0
        public PdfObjRef(PdfAnnotation annot, PdfStructElem parent)
            : base(new PdfDictionary(), parent)
        {
            PdfDictionary parentObject = parent.GetPdfObject();

            EnsureObjectIsAddedToDocument(parentObject);
            PdfDocument doc = parentObject.GetIndirectReference().GetDocument();

            annot.GetPdfObject().Put(PdfName.StructParent, new PdfNumber((int)doc.GetNextStructParentIndex()));
            PdfDictionary dict = (PdfDictionary)GetPdfObject();

            dict.Put(PdfName.Type, PdfName.OBJR);
            dict.Put(PdfName.Obj, annot.GetPdfObject());
        }
Exemplo n.º 4
0
        /// <summary>Removes annotation content item from the tag structure.</summary>
        /// <remarks>
        /// Removes annotation content item from the tag structure.
        /// If annotation is not added to the document or is not tagged, nothing will happen.
        /// </remarks>
        /// <param name="annotation">
        /// the
        /// <see cref="iText.Kernel.Pdf.Annot.PdfAnnotation"/>
        /// that will be removed from the tag structure
        /// </param>
        /// <returns>
        ///
        /// <see cref="TagTreePointer"/>
        /// instance which points at annotation tag parent if annotation was removed,
        /// otherwise returns null
        /// </returns>
        public virtual TagTreePointer RemoveAnnotationTag(PdfAnnotation annotation)
        {
            PdfStructElem structElem        = null;
            PdfDictionary annotDic          = annotation.GetPdfObject();
            PdfNumber     structParentIndex = (PdfNumber)annotDic.Get(PdfName.StructParent);

            if (structParentIndex != null)
            {
                PdfObjRef objRef = document.GetStructTreeRoot().FindObjRefByStructParentIndex(annotDic.GetAsDictionary(PdfName
                                                                                                                       .P), structParentIndex.IntValue());
                if (objRef != null)
                {
                    PdfStructElem parent = (PdfStructElem)objRef.GetParent();
                    parent.RemoveKid(objRef);
                    structElem = parent;
                }
            }
            annotDic.Remove(PdfName.StructParent);
            annotDic.SetModified();
            if (structElem != null)
            {
                return(new TagTreePointer(document).SetCurrentStructElem(structElem));
            }
            return(null);
        }
        private void CopyParentFormField(PdfPage toPage, IDictionary <String, PdfFormField> fieldsTo, PdfAnnotation
                                         annot, PdfFormField parentField)
        {
            PdfString parentName = parentField.GetFieldName();

            if (!fieldsTo.ContainsKey(parentName.ToUnicodeString()))
            {
                PdfFormField field = CreateParentFieldCopy(annot.GetPdfObject(), documentTo);
                PdfArray     kids  = field.GetKids();
                field.GetPdfObject().Remove(PdfName.Kids);
                formTo.AddField(field, toPage);
                field.GetPdfObject().Put(PdfName.Kids, kids);
            }
            else
            {
                PdfFormField field = MakeFormField(annot.GetPdfObject());
                if (field == null)
                {
                    return;
                }
                PdfString fieldName = field.GetFieldName();
                if (fieldName != null)
                {
                    PdfFormField existingField = fieldsTo.Get(fieldName.ToUnicodeString());
                    if (existingField != null)
                    {
                        PdfFormField mergedField = MergeFieldsWithTheSameName(field);
                        formTo.GetFormFields().Put(mergedField.GetFieldName().ToUnicodeString(), mergedField);
                    }
                    else
                    {
                        HashSet <String> existingFields = new HashSet <String>();
                        GetAllFieldNames(formTo.GetFields(), existingFields);
                        AddChildToExistingParent(annot.GetPdfObject(), existingFields, fieldsTo);
                    }
                }
                else
                {
                    if (!parentField.GetKids().Contains(field.GetPdfObject()))
                    {
                        HashSet <String> existingFields = new HashSet <String>();
                        GetAllFieldNames(formTo.GetFields(), existingFields);
                        AddChildToExistingParent(annot.GetPdfObject(), existingFields);
                    }
                }
            }
        }
        private void CopyField(PdfPage toPage, IDictionary <String, PdfFormField> fieldsFrom, IDictionary <String, PdfFormField
                                                                                                           > fieldsTo, PdfAnnotation currentAnnot)
        {
            PdfDictionary parent = currentAnnot.GetPdfObject().GetAsDictionary(PdfName.Parent);

            if (parent != null)
            {
                PdfFormField parentField = GetParentField(parent, documentTo);
                if (parentField == null)
                {
                    return;
                }
                PdfString parentName = parentField.GetFieldName();
                if (parentName == null)
                {
                    return;
                }
                CopyParentFormField(toPage, fieldsTo, currentAnnot, parentField);
            }
            else
            {
                PdfString annotName       = currentAnnot.GetPdfObject().GetAsString(PdfName.T);
                String    annotNameString = null;
                if (annotName != null)
                {
                    annotNameString = annotName.ToUnicodeString();
                }
                if (annotNameString != null && fieldsFrom.ContainsKey(annotNameString))
                {
                    PdfFormField field = MakeFormField(currentAnnot.GetPdfObject());
                    if (field == null)
                    {
                        return;
                    }
                    if (fieldsTo.Get(annotNameString) != null)
                    {
                        field = MergeFieldsWithTheSameName(field);
                    }
                    // Form may be already added to the page. PdfAcroForm will take care about it.
                    formTo.AddField(field, toPage);
                    field.UpdateDefaultAppearance();
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>Creates a Movie annotation (section 12.6.4.9 of ISO 32000-1).</summary>
 /// <remarks>Creates a Movie annotation (section 12.6.4.9 of ISO 32000-1). Deprecated in PDF 2.0.</remarks>
 /// <param name="annotation">a movie annotation identifying the movie that shall be played</param>
 /// <param name="title">the title of a movie annotation identifying the movie that shall be played</param>
 /// <param name="operation">
 /// the operation that shall be performed on the movie. Shall be one of the following:
 /// <see cref="iText.Kernel.Pdf.PdfName.Play"/>
 /// ,
 /// <see cref="iText.Kernel.Pdf.PdfName.Stop"/>
 /// ,
 /// <see cref="iText.Kernel.Pdf.PdfName.Pause"/>
 /// ,
 /// <see cref="iText.Kernel.Pdf.PdfName.Resume"/>
 /// </param>
 /// <returns>created annotation</returns>
 public static iText.Kernel.Pdf.Action.PdfAction CreateMovie(PdfAnnotation annotation, String title, PdfName
                                                             operation)
 {
     iText.Kernel.Pdf.Action.PdfAction action = new iText.Kernel.Pdf.Action.PdfAction().Put(PdfName.S, PdfName.
                                                                                            Movie).Put(PdfName.T, new PdfString(title)).Put(PdfName.Operation, operation);
     if (annotation != null)
     {
         action.Put(PdfName.Annotation, annotation.GetPdfObject());
     }
     return(action);
 }
Exemplo n.º 8
0
 public virtual bool ContainsAnnotation(PdfAnnotation annotation)
 {
     foreach (PdfAnnotation a in GetAnnotations())
     {
         if (a.GetPdfObject().Equals(annotation.GetPdfObject()))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 9
0
        private void CopyField(PdfPage toPage, IDictionary <String, PdfFormField> fieldsFrom, IDictionary <String, PdfFormField
                                                                                                           > fieldsTo, PdfAnnotation currentAnnot)
        {
            PdfDictionary parent = currentAnnot.GetPdfObject().GetAsDictionary(PdfName.Parent);

            if (parent != null)
            {
                PdfFormField parentField = GetParentField(parent, documentTo);
                PdfString    parentName  = parentField.GetFieldName();
                if (parentName == null)
                {
                    return;
                }
                CopyParentFormField(toPage, fieldsTo, currentAnnot, parentField);
            }
            else
            {
                PdfString annotName       = currentAnnot.GetPdfObject().GetAsString(PdfName.T);
                String    annotNameString = null;
                if (annotName != null)
                {
                    annotNameString = annotName.ToUnicodeString();
                }
                if (annotNameString != null && fieldsFrom.ContainsKey(annotNameString))
                {
                    PdfFormField field = fieldsTo.Get(annotNameString);
                    if (field == null)
                    {
                        formTo.AddField(PdfFormField.MakeFormField(currentAnnot.GetPdfObject(), documentTo), null);
                    }
                    else
                    {
                        CopyExistingField(toPage, currentAnnot);
                    }
                }
            }
        }
Exemplo n.º 10
0
 public virtual void GetAnnotationSetAsStringTest()
 {
     using (PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()))) {
         String    namedDestination     = "namedDestination";
         String    annotationIdentifier = "annotationIdentifier";
         PdfTarget target = PdfTarget.CreateChildTarget(namedDestination, annotationIdentifier);
         PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation(new Rectangle(0, 0, 20, 20));
         annotation.SetName(new PdfString(annotationIdentifier));
         document.AddNewPage();
         document.GetPage(1).AddAnnotation(annotation);
         document.GetCatalog().GetNameTree(PdfName.Dests).AddEntry(namedDestination, new PdfArray(new PdfNumber(1))
                                                                   );
         PdfAnnotation retrievedAnnotation = target.GetAnnotation(document);
         NUnit.Framework.Assert.AreEqual(annotation.GetPdfObject(), retrievedAnnotation.GetPdfObject());
     }
 }
Exemplo n.º 11
0
        // ********************************************************************
        // Fct:     GetNextPdfComment
        //
        // Descr:   -
        //
        // Owner:   erst
        // ********************************************************************
        private static bool GetNextPdfComment(PdfPage page, ref float x, ref float y, ref string content, ref int annotNo)
        {
            // Collects all comment annotations in the document
            Collection <PdfAnnotation> annotations = new Collection <PdfAnnotation>(page.GetAnnotations());

            while (annotNo <= annotations.Count)
            {
                PdfAnnotation annot = annotations[annotNo - 1];   // Array starts with index 0, annotNo starts with 1
                if (annot.GetType().Equals((typeof(iText.Kernel.Pdf.Annot.PdfTextAnnotation))))
                {
                    PdfDictionary annotDictionary = annot.GetPdfObject();

                    //Rect is the annotation rectangle defining the location of the annotation on the page
                    PdfArray Arr = annotDictionary.GetAsArray(PdfName.Rect);

                    //int x = Arr.GetAsNumber(0).IntValue();
                    //int y = Arr.GetAsNumber(1).IntValue();

                    Rectangle rect = annotDictionary.GetAsRectangle(PdfName.Rect);
                    x = rect.GetX();
                    y = rect.GetY();

                    //Read the content
                    content = annotDictionary.Get(PdfName.Contents).ToString();

                    annotNo++;
                    return(true);
                }
                else
                {
                    annotNo++;
                }
            }

            return(false);
        }
Exemplo n.º 12
0
 /// <summary>Creates a Rendition action (section 12.6.4.13 of ISO 32000-1).</summary>
 /// <param name="file">the name of the media clip, for use in the user interface.</param>
 /// <param name="fileSpec">a full file specification or form XObject that specifies the actual media data</param>
 /// <param name="mimeType">an ASCII string identifying the type of data</param>
 /// <param name="screenAnnotation">a screen annotation</param>
 /// <returns>created action</returns>
 public static iText.Kernel.Pdf.Action.PdfAction CreateRendition(String file, PdfFileSpec fileSpec, String
                                                                 mimeType, PdfAnnotation screenAnnotation)
 {
     return(new iText.Kernel.Pdf.Action.PdfAction().Put(PdfName.S, PdfName.Rendition).Put(PdfName.OP, new PdfNumber
                                                                                              (0)).Put(PdfName.AN, screenAnnotation.GetPdfObject()).Put(PdfName.R, new PdfRendition(file, fileSpec,
                                                                                                                                                                                    mimeType).GetPdfObject()));
 }
Exemplo n.º 13
0
 /// <summary>Creates a Hide action (section 12.6.4.10 of ISO 32000-1).</summary>
 /// <param name="annotation">the annotation to be hidden or shown</param>
 /// <param name="hidden">a flag indicating whether to hide the annotation (<c>true</c>) or show it (<c>false</c>)
 ///     </param>
 /// <returns>created action</returns>
 public static iText.Kernel.Pdf.Action.PdfAction CreateHide(PdfAnnotation annotation, bool hidden)
 {
     return(new iText.Kernel.Pdf.Action.PdfAction().Put(PdfName.S, PdfName.Hide).Put(PdfName.T, annotation.GetPdfObject
                                                                                         ()).Put(PdfName.H, PdfBoolean.ValueOf(hidden)));
 }
Exemplo n.º 14
0
        private void CopyExistingField(PdfPage toPage, PdfAnnotation currentAnnot)
        {
            PdfFormField field = MergeFieldsWithTheSameName(PdfFormField.MakeFormField(currentAnnot.GetPdfObject(), toPage
                                                                                       .GetDocument()));
            PdfArray kids = field.GetKids();

            if (kids != null)
            {
                field.GetPdfObject().Remove(PdfName.Kids);
                formTo.AddField(field, toPage);
                field.GetPdfObject().Put(PdfName.Kids, kids);
            }
            else
            {
                formTo.AddField(field, toPage);
            }
        }