/// <summary>Sets the name indicating the field to be signed.</summary> /// <remarks> /// Sets the name indicating the field to be signed. The field can already be presented in the /// document but shall not be signed. If the field is not presented in the document, it will be created. /// </remarks> /// <param name="fieldName">The name indicating the field to be signed.</param> public virtual void SetFieldName(String fieldName) { if (fieldName != null) { if (fieldName.IndexOf('.') >= 0) { throw new ArgumentException(PdfException.FieldNamesCannotContainADot); } PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, true); if (acroForm.GetField(fieldName) != null) { PdfFormField field = acroForm.GetField(fieldName); if (!PdfName.Sig.Equals(field.GetFormType())) { throw new ArgumentException(PdfException.FieldTypeIsNotASignatureFieldType); } if (field.GetValue() != null) { throw new ArgumentException(PdfException.FieldAlreadySigned); } appearance.SetFieldName(fieldName); IList <PdfWidgetAnnotation> widgets = field.GetWidgets(); if (widgets.Count > 0) { PdfWidgetAnnotation widget = widgets[0]; appearance.SetPageRect(GetWidgetRectangle(widget)); appearance.SetPageNumber(GetWidgetPageNumber(widget)); } } this.fieldName = fieldName; } }
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 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 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); }