public virtual void RemoveKidTest() { // Creating input document String inputFile = "in_removeKidTest.pdf"; PdfDocument inDoc = new PdfDocument(new PdfWriter(destinationFolder + inputFile)); inDoc.AddNewPage(); PdfFormField root = PdfFormField.CreateEmptyField(inDoc); root.SetFieldName("root"); PdfFormField child = PdfFormField.CreateEmptyField(inDoc); child.SetFieldName("child"); root.AddKid(child); PdfAcroForm.GetAcroForm(inDoc, true).AddField(root); inDoc.Close(); // Creating stamping document String outputFile = "removeKidTest.pdf"; PdfReader reader = new PdfReader(destinationFolder + inputFile); PdfWriter writer = new PdfWriter(destinationFolder + outputFile); PdfDocument outputDoc = new PdfDocument(reader, writer, new StampingProperties().UseAppendMode()); PdfAcroForm.GetAcroForm(outputDoc, true).RemoveField("root.child"); outputDoc.Close(); CompareWithCmp(outputFile); }
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); }
public virtual void RemoveKidFromIndirectKidsArrayTest() { String inputFile = "in_removeKidFromIndirectKidsArrayTest.pdf"; String outputFile = "removeKidFromIndirectKidsArrayTest.pdf"; // Creating input document PdfDocument inDoc = new PdfDocument(new PdfWriter(DESTINATION_DIR + inputFile)); inDoc.AddNewPage(); PdfFormField root = PdfFormField.CreateEmptyField(inDoc); root.SetFieldName("root"); PdfFormField child = PdfFormField.CreateEmptyField(inDoc); child.SetFieldName("child"); root.AddKid(child); PdfAcroForm.GetAcroForm(inDoc, true).AddField(root); // make kids array indirect PdfAcroForm.GetAcroForm(inDoc, true).GetField("root").GetKids().MakeIndirect(inDoc); inDoc.Close(); // Creating stamping document PdfReader reader = new PdfReader(DESTINATION_DIR + inputFile); PdfWriter writer = new PdfWriter(DESTINATION_DIR + outputFile); PdfDocument outputDoc = new PdfDocument(reader, writer, new StampingProperties().UseAppendMode()); PdfAcroForm.GetAcroForm(outputDoc, true).RemoveField("root.child"); outputDoc.Close(); CompareWithCmp(outputFile); }
private PdfFormField MergeFieldsWithTheSameName(PdfFormField existingField, PdfFormField newField) { String fullFieldName = newField.GetFieldName().ToUnicodeString(); PdfString fieldName = newField.GetPdfObject().GetAsString(PdfName.T); newField.GetPdfObject().Remove(PdfName.T); newField.GetPdfObject().Remove(PdfName.P); existingField = formTo.GetField(fullFieldName); PdfArray kids = existingField.GetKids(); if (kids != null && !kids.IsEmpty()) { existingField.AddKid(newField); return(existingField); } existingField.GetPdfObject().Remove(PdfName.T); existingField.GetPdfObject().Remove(PdfName.P); formTo.GetFields().Remove(existingField.GetPdfObject()); PdfFormField mergedField = PdfFormField.CreateEmptyField(documentTo); mergedField.Put(PdfName.FT, existingField.GetFormType()).Put(PdfName.T, fieldName); PdfDictionary parent = existingField.GetParent(); if (parent != null) { mergedField.Put(PdfName.Parent, parent); PdfArray parentKids = parent.GetAsArray(PdfName.Kids); for (int i = 0; i < parentKids.Size(); i++) { PdfObject obj = parentKids.Get(i); if (obj == existingField.GetPdfObject()) { parentKids.Set(i, mergedField.GetPdfObject()); break; } } } kids = existingField.GetKids(); if (kids != null) { mergedField.Put(PdfName.Kids, kids); } mergedField.AddKid(existingField).AddKid(newField); PdfObject value = existingField.GetValue(); if (value != null) { mergedField.Put(PdfName.V, existingField.GetPdfObject().Get(PdfName.V)); } return(mergedField); }
public virtual void AddFieldWithKidsTest() { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); PdfFormField root = PdfFormField.CreateEmptyField(pdfDoc); root.SetFieldName("root"); PdfFormField child = PdfFormField.CreateEmptyField(pdfDoc); child.SetFieldName("child"); root.AddKid(child); PdfTextFormField text1 = PdfFormField.CreateText(pdfDoc, new Rectangle(100, 700, 200, 20), "text1", "test" ); child.AddKid(text1); form.AddField(root); NUnit.Framework.Assert.AreEqual(3, form.GetFormFields().Count); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfFormField personal = PdfFormField.CreateEmptyField(pdfDoc); personal.SetFieldName("personal"); PdfTextFormField name = PdfFormField.CreateText(pdfDoc, new Rectangle(36, 760, 108, 30), "name", ""); personal.AddKid(name); PdfTextFormField password = PdfFormField.CreateText(pdfDoc, new Rectangle(150, 760, 300, 30), "password", ""); personal.AddKid(password); PdfAcroForm.GetAcroForm(pdfDoc, true).AddField(personal, pdfDoc.GetFirstPage()); pdfDoc.Close(); }
public virtual void PdfA1DocWithPdfA1ButtonFieldTest() { String name = "pdfA1DocWithPdfA1ButtonField"; String fileName = destinationFolder + name + ".pdf"; String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1ButtonField.pdf"; Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read); PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B; PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel, new PdfOutputIntent("Custom" , "", "http://www.color.org", "sRGB IEC61966-2.1", @is)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); PdfFormField emptyField = PdfFormField.CreateEmptyField(pdfDoc, conformanceLevel).SetFieldName("empty"); emptyField.AddKid(PdfFormField.CreateButton(pdfDoc, new Rectangle(36, 756, 20, 20), PdfAnnotation.PRINT, conformanceLevel ).SetFieldName("button").SetValue("hello")); form.AddField(emptyField); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(fileName, cmp, destinationFolder)); NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(fileName)); }
private PdfFormField MergeFieldsWithTheSameName(PdfFormField newField) { String fullFieldName = newField.GetFieldName().ToUnicodeString(); PdfString fieldName = newField.GetPdfObject().GetAsString(PdfName.T); logger.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, fullFieldName )); PdfFormField existingField = formTo.GetField(fullFieldName); if (existingField.IsFlushed()) { int index = 0; do { index++; newField.SetFieldName(fieldName.ToUnicodeString() + "_#" + index); fullFieldName = newField.GetFieldName().ToUnicodeString(); }while (formTo.GetField(fullFieldName) != null); return(newField); } newField.GetPdfObject().Remove(PdfName.T); newField.GetPdfObject().Remove(PdfName.P); formTo.GetFields().Remove(existingField.GetPdfObject()); PdfArray kids = existingField.GetKids(); if (kids != null && !kids.IsEmpty()) { existingField.AddKid(newField); return(existingField); } existingField.GetPdfObject().Remove(PdfName.T); existingField.GetPdfObject().Remove(PdfName.P); PdfFormField mergedField = PdfFormField.CreateEmptyField(documentTo); mergedField.Put(PdfName.FT, existingField.GetFormType()).Put(PdfName.T, fieldName); PdfDictionary parent = existingField.GetParent(); if (parent != null) { mergedField.Put(PdfName.Parent, parent); PdfArray parentKids = parent.GetAsArray(PdfName.Kids); for (int i = 0; i < parentKids.Size(); i++) { PdfObject obj = parentKids.Get(i); if (obj == existingField.GetPdfObject()) { parentKids.Set(i, mergedField.GetPdfObject()); break; } } } kids = existingField.GetKids(); if (kids != null) { mergedField.Put(PdfName.Kids, kids); } mergedField.AddKid(existingField).AddKid(newField); PdfObject value = existingField.GetPdfObject().Get(PdfName.V); if (value != null) { mergedField.Put(PdfName.V, existingField.GetPdfObject().Get(PdfName.V)); } return(mergedField); }