|
1 | 1 | package com.itextpdf.forms;
|
2 | 2 |
|
3 | 3 | import com.itextpdf.io.LogMessageConstant;
|
4 |
| -import com.itextpdf.kernel.pdf.IPdfPageExtraCopier; |
5 |
| -import com.itextpdf.kernel.pdf.PdfDictionary; |
6 |
| -import com.itextpdf.kernel.pdf.PdfDocument; |
7 |
| -import com.itextpdf.kernel.pdf.PdfName; |
8 |
| -import com.itextpdf.kernel.pdf.PdfPage; |
9 |
| -import com.itextpdf.kernel.pdf.PdfString; |
| 4 | +import com.itextpdf.kernel.pdf.*; |
10 | 5 | import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
|
11 | 6 | import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation;
|
12 | 7 | import com.itextpdf.forms.fields.PdfFormField;
|
@@ -95,19 +90,26 @@ public void copy(PdfPage fromPage, PdfPage toPage) {
|
95 | 90 | }
|
96 | 91 |
|
97 | 92 | private PdfFormField mergeFieldsWithTheSameName(PdfFormField existingField, PdfFormField newField) {
|
98 |
| - PdfFormField mergedField = PdfFormField.createEmptyField(documentTo); |
| 93 | + String fieldName = newField.getFieldName().toUnicodeString(); |
| 94 | + existingField.remove(PdfName.T); |
| 95 | + PdfFormField mergedField = formTo.getField(fieldName); |
| 96 | + PdfArray kids = mergedField.getKids(); |
| 97 | + if (kids != null && !kids.isEmpty()) { |
| 98 | + mergedField.addKid(existingField); |
| 99 | + return mergedField; |
| 100 | + } |
| 101 | + newField.remove(PdfName.T); |
| 102 | + mergedField = PdfFormField.createEmptyField(documentTo); |
99 | 103 | formTo.getFields().remove(newField.getPdfObject());
|
| 104 | + //formTo.getFormFields().remove(fieldName); |
100 | 105 | mergedField.
|
101 | 106 | put(PdfName.FT, existingField.getFormType()).
|
102 |
| - put(PdfName.T, existingField.getFieldName()). |
| 107 | + put(PdfName.T, new PdfString(fieldName)). |
103 | 108 | put(PdfName.Parent, existingField.getParent()).
|
104 | 109 | put(PdfName.Kids, existingField.getKids());
|
105 | 110 |
|
106 |
| - existingField.remove(PdfName.T); |
107 |
| - newField.remove(PdfName.T); |
108 | 111 | mergedField.addKid(existingField).addKid(newField);
|
109 | 112 |
|
110 | 113 | return mergedField;
|
111 | 114 | }
|
112 |
| - |
113 | 115 | }
|
0 commit comments