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 FormFieldTest01() { PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "formFieldFile.pdf")); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); IDictionary <String, PdfFormField> fields = form.GetFormFields(); PdfFormField field = fields.Get("Text1"); NUnit.Framework.Assert.IsTrue(fields.Count == 6); NUnit.Framework.Assert.IsTrue(field.GetFieldName().ToUnicodeString().Equals("Text1")); NUnit.Framework.Assert.IsTrue(field.GetValue().ToString().Equals("TestField")); }
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); }
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(); } } }
private void AddChildToExistingParent(PdfDictionary fieldDic, ICollection <String> existingFields, IDictionary <String, PdfFormField> fieldsTo) { PdfDictionary parent = fieldDic.GetAsDictionary(PdfName.Parent); if (parent == null) { return; } PdfString parentName = parent.GetAsString(PdfName.T); if (parentName != null) { String name = parentName.ToUnicodeString(); if (existingFields.Contains(name)) { PdfArray kids = parent.GetAsArray(PdfName.Kids); foreach (PdfObject kid in kids) { if (((PdfDictionary)kid).Get(PdfName.T).Equals(fieldDic.Get(PdfName.T))) { PdfFormField kidField = MakeFormField(kid); PdfFormField field = MakeFormField(fieldDic); if (kidField == null || field == null) { continue; } fieldsTo.Put(kidField.GetFieldName().ToUnicodeString(), kidField); PdfFormField mergedField = MergeFieldsWithTheSameName(field); formTo.GetFormFields().Put(mergedField.GetFieldName().ToUnicodeString(), mergedField); return; } } kids.Add(fieldDic); } else { parent.Put(PdfName.Kids, new PdfArray(fieldDic)); AddChildToExistingParent(parent, 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); 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); } } } }
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); } } } } } } } }
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); }
private void VerticalJustificationInAcroForm <T>(PdfFormField field, T detail, string propertyName, PdfFont pdfFont, PdfFontAttribute pdfFontAttribute) { var widgets = field.GetWidgets(); if (widgets == null || widgets.Count == 0) { throw new ArgumentNullException($"no widgets to the field"); } PdfArray position = widgets.First().GetRectangle(); PdfPage page = field.GetWidgets().First().GetPage(); if (page == null) { throw new ArgumentNullException( $"field widget annotation is not associated with any page"); } int pageNum = _pdfDoc.GetPageNumber(page); float width = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue()); float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue()); float llx = (float)position.GetAsNumber(0).GetValue(); float lly = (float)position.GetAsNumber(1).GetValue(); float urx = (float)position.GetAsNumber(2).GetValue(); float ury = (float)position.GetAsNumber(3).GetValue(); Rectangle rect = new Rectangle(llx, lly, width, height); PdfCanvas canvas = new PdfCanvas(_pdfDoc, pageNum); canvas.SetLineWidth(0f).Rectangle(rect); string textValue = null; Type type = detail.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { if (property.Name == propertyName) { textValue = property.GetValue(detail, null).ToString(); } } if (string.IsNullOrWhiteSpace(textValue)) { return; } float verticalAdjustment = 0; var text = new Text(textValue); if (pdfFont != null) { text.SetFont(pdfFont); text.SetFontColor(pdfFontAttribute.Color); if (pdfFontAttribute.Size != 0) { text.SetFontSize(pdfFontAttribute.Size); } verticalAdjustment = (pdfFont.GetWidth(textValue, pdfFontAttribute.Size) / (urx - llx)) * pdfFont.GetAscent(textValue, pdfFontAttribute.Size); } Paragraph p = new Paragraph(text); if (pdfFontAttribute.Justification == PdfTextAlignment.CenterLeft || pdfFontAttribute.Justification == PdfTextAlignment.BottomLeft) { p.SetTextAlignment(iText.Layout.Properties.TextAlignment.LEFT); } if (pdfFontAttribute.Justification == PdfTextAlignment.CenterMiddle || pdfFontAttribute.Justification == PdfTextAlignment.BottomMiddle) { p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER); } if (pdfFontAttribute.Justification == PdfTextAlignment.CenterRight || pdfFontAttribute.Justification == PdfTextAlignment.BottomRight) { p.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT); } // set line space p.SetMultipliedLeading(1); if (pdfFontAttribute.Justification == PdfTextAlignment.CenterLeft || pdfFontAttribute.Justification == PdfTextAlignment.CenterMiddle || pdfFontAttribute.Justification == PdfTextAlignment.CenterRight) { new Canvas(canvas, _pdfDoc, rect) .Add(p.SetFixedPosition(llx, (ury + lly) / 2 - verticalAdjustment, urx - llx)); } if (pdfFontAttribute.Justification == PdfTextAlignment.BottomLeft || pdfFontAttribute.Justification == PdfTextAlignment.BottomMiddle || pdfFontAttribute.Justification == PdfTextAlignment.BottomRight) { new Canvas(canvas, _pdfDoc, rect).Add(p); } _form.RemoveField(field.GetFieldName().ToString()); }
public void FillU3DImageInAcroForm <T>(T detail) { IDictionary <String, PdfFormField> fields = _form.GetFormFields(); Type type = detail.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { PdfFormField toSet; PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>(); if (fields.TryGetValue(property.Name, out toSet)) { var value = property.GetValue(detail, null)?.ToString(); PdfFormField field = fields[property.Name]; if (!string.IsNullOrWhiteSpace(value)) { var widgets = field.GetWidgets(); if (widgets == null || widgets.Count == 0) { throw new ArgumentNullException($"no widgets to the field"); } PdfArray position = widgets.First().GetRectangle(); PdfPage page = field.GetWidgets().First().GetPage(); if (page == null) { throw new ArgumentNullException( $"field widget annotation is not associated with any page"); } int pageNum = _pdfDoc.GetPageNumber(page); float width = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue()); float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue()); var u3DInfo = new U3DInfo { Height = height, Width = width, X = (float)position.GetAsNumber(0).GetValue(), Y = (float)position.GetAsNumber(1).GetValue(), Page = pageNum, U3DFile = value }; _u3DInfos.Add(u3DInfo); _form.RemoveField(field.GetFieldName().ToString()); } else { if (required != null) { if (required.Action.Equals(ActionFlag.Required)) { throw new ArgumentNullException($"No {property.Name} image found"); } if (required.Action.Equals(ActionFlag.Optional)) { _form.RemoveField(field.GetFieldName().ToString()); continue; } if (required.Action.Equals(ActionFlag.NotAvailable)) { toSet.SetValue("N/A").SetFontSizeAutoScale().SetReadOnly(true); } } } } } // close the target pdf first: _pdfDoc?.Close(); using (var helper = new U3DImageHelper(TargetFile)) { foreach (var u3DInfo in _u3DInfos) { helper.AddU3DImage(u3DInfo); } } // reopen the target pdf: _temporaryFile = Path.Combine(Path.GetDirectoryName(TargetFile), $"temp_{DateTime.Now.Ticks}.pdf"); File.Move(TargetFile, _temporaryFile); Init(TargetFile, _temporaryFile); }
public void FillImageInAcroForm <T>(T detail) { IDictionary <String, PdfFormField> fields = _form.GetFormFields(); Type type = detail.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { PdfFormField toSet; PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>(); if (required != null && (required.Action.Equals(ActionFlag.Ignore) || required.Action.Equals(ActionFlag.MultiLines))) { continue; } if (fields.TryGetValue(property.Name, out toSet)) { var value = property.GetValue(detail, null); PdfFormField field = fields[property.Name]; if (!string.IsNullOrWhiteSpace(value?.ToString())) { var widgets = field.GetWidgets(); if (widgets == null || widgets.Count == 0) { throw new ArgumentNullException($"no widgets to the field"); } PdfArray position = widgets.First().GetRectangle(); PdfPage page = field.GetWidgets().First().GetPage(); if (page == null) { throw new ArgumentNullException( $"field widget annotation is not associated with any page"); } int pageNum = _pdfDoc.GetPageNumber(page); float width = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue()); float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue()); Image image = new Image(ImageDataFactory.Create(File.ReadAllBytes(value.ToString()))); image.ScaleToFit(width, height); float startX = (float)position.GetAsNumber(0).GetValue(); float startY = (float)position.GetAsNumber(1).GetValue(); PdfFontAttribute pdfFont = property.GetCustomAttribute <PdfFontAttribute>(); if (pdfFont != null) { ImagePositionAdjustification(pdfFont.Justification, width, height, image.GetImageScaledWidth(), image.GetImageScaledHeight(), (float)position.GetAsNumber(0).GetValue(), (float)position.GetAsNumber(1).GetValue(), out startX, out startY); } image.SetFixedPosition(pageNum, startX, startY); _form.RemoveField(field.GetFieldName().ToString()); _doc.Add(image); } else { if (required != null) { if (required.Action.Equals(ActionFlag.Required)) { throw new ArgumentNullException($"No {property.Name} image found"); } if (required.Action.Equals(ActionFlag.Optional)) { _form.RemoveField(field.GetFieldName().ToString()); continue; } if (required.Action.Equals(ActionFlag.NotAvailable)) { toSet.SetValue("N/A").SetFontSizeAutoScale().SetReadOnly(true); } } } } } }