Skip to content

Commit

Permalink
Merge branch 'release_branch_DEVSIX-5942' into master-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
iText-CI authored and EvgenyB1001 committed Oct 20, 2021
2 parents 87fffa6 + 6d5b066 commit a274601
Show file tree
Hide file tree
Showing 559 changed files with 3,839 additions and 859 deletions.
2 changes: 1 addition & 1 deletion barcodes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.itextpdf</groupId>
<artifactId>root</artifactId>
<version>7.1.16</version>
<version>7.1.17</version>
</parent>
<artifactId>barcodes</artifactId>
<name>iText 7 - barcodes</name>
Expand Down
2 changes: 1 addition & 1 deletion font-asian/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.itextpdf</groupId>
<artifactId>root</artifactId>
<version>7.1.16</version>
<version>7.1.17</version>
</parent>
<artifactId>font-asian</artifactId>
<name>iText 7 - Asian fonts</name>
Expand Down
2 changes: 1 addition & 1 deletion forms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.itextpdf</groupId>
<artifactId>root</artifactId>
<version>7.1.16</version>
<version>7.1.17</version>
</parent>
<artifactId>forms</artifactId>
<name>iText 7 - forms</name>
Expand Down
24 changes: 22 additions & 2 deletions forms/src/main/java/com/itextpdf/forms/PdfPageFormCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,14 @@ private void copyParentFormField(PdfPage toPage, Map<String, PdfFormField> field
PdfAnnotation annot, PdfFormField parentField) {
PdfString parentName = parentField.getFieldName();
if (!fieldsTo.containsKey(parentName.toUnicodeString())) {
// no such field, hence we should simply add it
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 {
// it is either a field (field name will not be null) or a widget (field name is not null)
PdfFormField field = makeFormField(annot.getPdfObject());
if (field == null) {
return;
Expand All @@ -190,18 +192,36 @@ private void copyParentFormField(PdfPage toPage, Map<String, PdfFormField> field
fieldsTo);
}
} else {
if (!parentField.getKids().contains(field.getPdfObject())) {
if (!parentField.getKids().contains(field.getPdfObject())
&& formTo.getFields().contains(parentField.getPdfObject())) {
// its parent is already a field of the resultant document,
// hence we only need to update its children
HashSet<String> existingFields = new HashSet<>();
getAllFieldNames(formTo.getFields(), existingFields);
addChildToExistingParent(annot.getPdfObject(), existingFields);
} else {
// its parent is not a field of the resultant document, but the latter contains
// a field of the same name, therefore we should merge them (note that merging in this context
// differs from merging a widget and an annotation into a single entity)
PdfFormField mergedField = mergeFieldsWithTheSameName(field);
// we need to add the field not to its representation (#getFormFields()), but to
// /Fields entry of the acro form
formTo.addField(mergedField, toPage);
}
}
}
}

private PdfFormField mergeFieldsWithTheSameName(PdfFormField newField) {
String fullFieldName = newField.getFieldName().toUnicodeString();
PdfString fieldName = newField.getPdfObject().getAsString(PdfName.T);
if (null == fieldName) {
fieldName = newField.getParent().getAsString(PdfName.T);
}

String fullFieldName = fieldName.toUnicodeString();
if (null != newField.getFieldName()) {
fullFieldName = newField.getFieldName().toUnicodeString();
}

logger.warn(MessageFormatUtil.format(LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, fullFieldName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private static void fillTextFieldsThenFlattenThenCompare(String testName) throws
}

@Test
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 3)})
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 5)})
//Logging is expected since there are duplicate field names
//isReadOnly should be true after DEVSIX-2156
public void flattenReadOnly() throws IOException {
Expand Down
124 changes: 112 additions & 12 deletions forms/src/test/java/com/itextpdf/forms/PdfFormCopyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ This file is part of the iText (R) project.
import com.itextpdf.test.annotations.LogMessage;
import com.itextpdf.test.annotations.LogMessages;
import com.itextpdf.test.annotations.type.IntegrationTest;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -63,8 +64,8 @@ This file is part of the iText (R) project.
@Category(IntegrationTest.class)
public class PdfFormCopyTest extends ExtendedITextTest {

public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfFormFieldsCopyTest/";
public static final String destinationFolder = "./target/test/com/itextpdf/forms/PdfFormFieldsCopyTest/";
public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfFormCopyTest/";
public static final String destinationFolder = "./target/test/com/itextpdf/forms/PdfFormCopyTest/";

@BeforeClass
public static void beforeClass() {
Expand All @@ -73,7 +74,7 @@ public static void beforeClass() {

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 13)
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 32)
})
public void copyFieldsTest01() throws IOException, InterruptedException {
String srcFilename1 = sourceFolder + "appearances1.pdf";
Expand Down Expand Up @@ -328,7 +329,7 @@ public void copyFieldsTest06() throws IOException, InterruptedException {

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 13)
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 32)
})
public void copyFieldsTest07() throws IOException, InterruptedException {
String srcFilename = sourceFolder + "datasheet.pdf";
Expand All @@ -350,7 +351,7 @@ public void copyFieldsTest07() throws IOException, InterruptedException {

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 13)
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 32)
})
public void copyFieldsTest08() throws IOException, InterruptedException {
String srcFilename1 = sourceFolder + "appearances1.pdf";
Expand Down Expand Up @@ -378,7 +379,7 @@ public void copyFieldsTest08() throws IOException, InterruptedException {

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 26)
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 64)
})
public void copyFieldsTest09() throws IOException, InterruptedException {
String srcFilename = sourceFolder + "datasheet.pdf";
Expand Down Expand Up @@ -516,9 +517,9 @@ public void unnamedFieldsHierarchyTest() throws IOException, InterruptedExceptio
})
public void copyAndEditTextFields() throws IOException, InterruptedException {
//TODO: update after DEVSIX-2354
String srcFileName = sourceFolder + "/checkPdfFormCopy_Source.pdf";
String srcFileName = sourceFolder + "checkPdfFormCopy_Source.pdf";
String destFilename = destinationFolder + "copyAndEditTextFields.pdf";
String cmpFileName = sourceFolder + "/cmp_copyAndEditTextFields.pdf";
String cmpFileName = sourceFolder + "cmp_copyAndEditTextFields.pdf";

PdfDocument srcDoc = new PdfDocument(new PdfReader(srcFileName));
PdfDocument destDoc = new PdfDocument(new PdfWriter(destFilename));
Expand Down Expand Up @@ -547,9 +548,9 @@ public void copyAndEditTextFields() throws IOException, InterruptedException {
})
public void copyAndEditCheckboxes() throws IOException, InterruptedException {
//TODO: update after DEVSIX-2354
String srcFileName = sourceFolder + "/checkPdfFormCopy_Source.pdf";
String srcFileName = sourceFolder + "checkPdfFormCopy_Source.pdf";
String destFilename = destinationFolder + "copyAndEditCheckboxes.pdf";
String cmpFileName = sourceFolder + "/cmp_copyAndEditCheckboxes.pdf";
String cmpFileName = sourceFolder + "cmp_copyAndEditCheckboxes.pdf";

PdfDocument srcDoc = new PdfDocument(new PdfReader(srcFileName));
PdfDocument destDoc = new PdfDocument(new PdfWriter(destFilename));
Expand All @@ -576,9 +577,9 @@ public void copyAndEditCheckboxes() throws IOException, InterruptedException {
})
public void copyAndEditRadioButtons() throws IOException, InterruptedException {
//TODO: update after DEVSIX-2354
String srcFileName = sourceFolder + "/checkPdfFormCopy_Source.pdf";
String srcFileName = sourceFolder + "checkPdfFormCopy_Source.pdf";
String destFilename = destinationFolder + "copyAndEditRadioButtons.pdf";
String cmpFileName = sourceFolder + "/cmp_copyAndEditRadioButtons.pdf";
String cmpFileName = sourceFolder + "cmp_copyAndEditRadioButtons.pdf";

PdfDocument srcDoc = new PdfDocument(new PdfReader(srcFileName));
PdfDocument destDoc = new PdfDocument(new PdfWriter(destFilename));
Expand All @@ -596,4 +597,103 @@ public void copyAndEditRadioButtons() throws IOException, InterruptedException {

Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFileName, destinationFolder, "diff_"));
}

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD)
})
public void mergeMergedFieldAndMergedFieldTest() throws IOException, InterruptedException {
String srcFileName1 = sourceFolder + "fieldMergedWithWidget.pdf";
String destFilename = destinationFolder + "mergeMergedFieldAndMergedFieldTest.pdf";
String cmpFileName = sourceFolder + "cmp_mergeMergedFieldAndMergedFieldTest.pdf";

try (
PdfWriter writer = new PdfWriter(destFilename);
PdfDocument resultPdfDocument = new PdfDocument(writer);
PdfReader reader1 = new PdfReader(srcFileName1);
PdfDocument sourceDoc1 = new PdfDocument(reader1);) {
PdfAcroForm.getAcroForm(resultPdfDocument, true);
PdfPageFormCopier formCopier = new PdfPageFormCopier();

sourceDoc1.copyPagesTo(1, sourceDoc1.getNumberOfPages(), resultPdfDocument, formCopier);
sourceDoc1.copyPagesTo(1, sourceDoc1.getNumberOfPages(), resultPdfDocument, formCopier);
}

Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFileName, destinationFolder, "diff_"));
}

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD, count = 2)
})
public void mergeMergedFieldAndTwoWidgetsTest() throws IOException, InterruptedException {
String srcFileName1 = sourceFolder + "fieldMergedWithWidget.pdf";
String srcFileName2 = sourceFolder + "fieldTwoWidgets.pdf";
String destFilename = destinationFolder + "mergeMergedFieldAndTwoWidgetsTest.pdf";
String cmpFileName = sourceFolder + "cmp_mergeMergedFieldAndTwoWidgetsTest.pdf";

try (
PdfWriter writer = new PdfWriter(destFilename);
PdfDocument resultPdfDocument = new PdfDocument(writer);
PdfReader reader1 = new PdfReader(srcFileName1);
PdfDocument sourceDoc1 = new PdfDocument(reader1);
PdfReader reader2 = new PdfReader(srcFileName2);
PdfDocument sourceDoc2 = new PdfDocument(reader2);) {
PdfAcroForm.getAcroForm(resultPdfDocument, true);
PdfPageFormCopier formCopier = new PdfPageFormCopier();

sourceDoc1.copyPagesTo(1, sourceDoc1.getNumberOfPages(), resultPdfDocument, formCopier);
sourceDoc2.copyPagesTo(1, sourceDoc2.getNumberOfPages(), resultPdfDocument, formCopier);
}

Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFileName, destinationFolder, "diff_"));
}

@Test
@LogMessages(messages = {
@LogMessage(messageTemplate = LogMessageConstant.DOCUMENT_ALREADY_HAS_FIELD)
})
public void mergeTwoWidgetsAndMergedFieldTest() throws IOException, InterruptedException {
String srcFileName1 = sourceFolder + "fieldMergedWithWidget.pdf";
String srcFileName2 = sourceFolder + "fieldTwoWidgets.pdf";
String destFilename = destinationFolder + "mergeTwoWidgetsAndMergedFieldTest.pdf";
String cmpFileName = sourceFolder + "cmp_mergeTwoWidgetsAndMergedFieldTest.pdf";

try (
PdfWriter writer = new PdfWriter(destFilename);
PdfDocument resultPdfDocument = new PdfDocument(writer);
PdfReader reader1 = new PdfReader(srcFileName1);
PdfDocument sourceDoc1 = new PdfDocument(reader1);
PdfReader reader2 = new PdfReader(srcFileName2);
PdfDocument sourceDoc2 = new PdfDocument(reader2);) {
PdfAcroForm.getAcroForm(resultPdfDocument, true);
PdfPageFormCopier formCopier = new PdfPageFormCopier();

sourceDoc2.copyPagesTo(1, sourceDoc2.getNumberOfPages(), resultPdfDocument, formCopier);
sourceDoc1.copyPagesTo(1, sourceDoc1.getNumberOfPages(), resultPdfDocument, formCopier);
}

Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFileName, destinationFolder, "diff_"));
}

@Test
public void mergeTwoWidgetsAndTwoWidgetsTest() throws IOException, InterruptedException {
String srcFileName2 = sourceFolder + "fieldTwoWidgets.pdf";
String destFilename = destinationFolder + "mergeTwoWidgetsAndTwoWidgetsTest.pdf";
String cmpFileName = sourceFolder + "cmp_mergeTwoWidgetsAndTwoWidgetsTest.pdf";

try (
PdfWriter writer = new PdfWriter(destFilename);
PdfDocument resultPdfDocument = new PdfDocument(writer);
PdfReader reader2 = new PdfReader(srcFileName2);
PdfDocument sourceDoc2 = new PdfDocument(reader2);) {
PdfAcroForm.getAcroForm(resultPdfDocument, true);
PdfPageFormCopier formCopier = new PdfPageFormCopier();

sourceDoc2.copyPagesTo(1, sourceDoc2.getNumberOfPages(), resultPdfDocument, formCopier);
sourceDoc2.copyPagesTo(1, sourceDoc2.getNumberOfPages(), resultPdfDocument, formCopier);
}

Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFileName, destinationFolder, "diff_"));
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion hyph/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.itextpdf</groupId>
<artifactId>root</artifactId>
<version>7.1.16</version>
<version>7.1.17</version>
</parent>
<artifactId>hyph</artifactId>
<name>iText 7 - hyph</name>
Expand Down
2 changes: 1 addition & 1 deletion io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.itextpdf</groupId>
<artifactId>root</artifactId>
<version>7.1.16</version>
<version>7.1.17</version>
</parent>
<artifactId>io</artifactId>
<name>iText 7 - io</name>
Expand Down
Loading

0 comments on commit a274601

Please sign in to comment.