private PdfFormField MergeFieldsWithTheSameName(PdfFormField existingField, PdfFormField newField) { String fieldName = newField.GetFieldName().ToUnicodeString(); existingField.GetPdfObject().Remove(PdfName.T); PdfFormField mergedField = formTo.GetField(fieldName); PdfArray kids = mergedField.GetKids(); if (kids != null && !kids.IsEmpty()) { mergedField.AddKid(existingField); return(mergedField); } newField.GetPdfObject().Remove(PdfName.T); mergedField = PdfFormField.CreateEmptyField(documentTo); formTo.GetFields().Remove(newField.GetPdfObject()); mergedField.Put(PdfName.FT, existingField.GetFormType()).Put(PdfName.T, new PdfString(fieldName)); PdfDictionary parent = existingField.GetParent(); if (parent != null) { mergedField.Put(PdfName.Parent, parent); } kids = existingField.GetKids(); if (kids != null) { mergedField.Put(PdfName.Kids, kids); } mergedField.AddKid(existingField).AddKid(newField); return(mergedField); }
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 static void CreateInputFileWithIndirectFieldsArray() { PdfDocument document = new PdfDocument(new PdfWriter(inputFileWithIndirectFieldsArray)); document.AddNewPage(); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, true); acroForm.GetFields().MakeIndirect(document); acroForm.AddField(PdfFormField.CreateText(document, new Rectangle(20, 160, 100, 20), "textfield1", "text1" )); acroForm.AddField(PdfFormField.CreateText(document, new Rectangle(20, 130, 100, 20), "textfield2", "text2" )); document.Close(); }
public virtual void CopyFieldsTest04() { String srcFilename = sourceFolder + "srcFile1.pdf"; PdfDocument srcDoc = new PdfDocument(new PdfReader(srcFilename)); PdfDocument destDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfPageFormCopier formCopier = new PdfPageFormCopier(); srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), destDoc, formCopier); srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), destDoc, formCopier); PdfAcroForm form = PdfAcroForm.GetAcroForm(destDoc, false); NUnit.Framework.Assert.AreEqual(1, form.GetFields().Size()); NUnit.Framework.Assert.IsNotNull(form.GetField("Name1")); NUnit.Framework.Assert.IsNotNull(form.GetField("Name1.1")); destDoc.Close(); }
public virtual void Copy(PdfPage fromPage, PdfPage toPage) { if (documentFrom != fromPage.GetDocument()) { documentFrom = fromPage.GetDocument(); formFrom = PdfAcroForm.GetAcroForm(documentFrom, false); } if (documentTo != toPage.GetDocument()) { documentTo = toPage.GetDocument(); formTo = PdfAcroForm.GetAcroForm(documentTo, true); } if (formFrom != null) { //duplicate AcroForm dictionary IList <PdfName> excludedKeys = new List <PdfName>(); excludedKeys.Add(PdfName.Fields); excludedKeys.Add(PdfName.DR); PdfDictionary dict = formFrom.GetPdfObject().CopyTo(documentTo, excludedKeys, false); formTo.GetPdfObject().MergeDifferent(dict); } if (formFrom != null) { IDictionary <String, PdfFormField> fieldsFrom = formFrom.GetFormFields(); if (fieldsFrom.Count > 0) { IDictionary <String, PdfFormField> fieldsTo = formTo.GetFormFields(); IList <PdfAnnotation> annots = toPage.GetAnnotations(); foreach (PdfAnnotation annot in annots) { if (annot.GetSubtype().Equals(PdfName.Widget)) { PdfDictionary parent = annot.GetPdfObject().GetAsDictionary(PdfName.Parent); if (parent != null) { PdfFormField parentField = GetParentField(parent, documentTo); PdfString parentName = parentField.GetFieldName(); if (parentName == null) { continue; } 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 = PdfFormField.MakeFormField(annot.GetPdfObject(), documentTo); PdfString fieldName = field.GetFieldName(); if (fieldName != null) { PdfFormField existingField = fieldsTo.Get(fieldName.ToUnicodeString()); if (existingField != null) { PdfFormField clonedField = PdfFormField.MakeFormField(field.GetPdfObject().Clone().MakeIndirect(documentTo ), documentTo); toPage.GetPdfObject().GetAsArray(PdfName.Annots).Add(clonedField.GetPdfObject()); toPage.RemoveAnnotation(annot); MergeFieldsWithTheSameName(clonedField); } else { HashSet <String> existingFields = new HashSet <String>(); GetAllFieldNames(formTo.GetFields(), existingFields); AddChildToExistingParent(annot.GetPdfObject(), existingFields); } } else { if (!parentField.GetKids().Contains(field.GetPdfObject())) { HashSet <String> existingFields = new HashSet <String>(); GetAllFieldNames(formTo.GetFields(), existingFields); AddChildToExistingParent(annot.GetPdfObject(), existingFields); } } } } else { PdfString annotName = annot.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) { PdfDictionary clonedAnnot = (PdfDictionary)annot.GetPdfObject().Clone().MakeIndirect(documentTo); toPage.GetPdfObject().GetAsArray(PdfName.Annots).Add(clonedAnnot); toPage.RemoveAnnotation(annot); field = MergeFieldsWithTheSameName(PdfFormField.MakeFormField(clonedAnnot, toPage.GetDocument())); logger.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, annotNameString )); 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); } } else { formTo.AddField(PdfFormField.MakeFormField(annot.GetPdfObject(), documentTo), null); } } } } } } } }