Skip to content

Commit c580f61

Browse files
committed
Wrap old content when flattening fields
DEVSIX-2741
1 parent 01ebddc commit c580f61

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

forms/src/main/java/com/itextpdf/forms/PdfAcroForm.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ public void flattenFields() {
635635
initialPageResourceClones.put(i, resources == null ? null : resources.clone());
636636
}
637637

638+
Set<PdfPage> wrappedPages = new LinkedHashSet<PdfPage>();
638639
PdfPage page;
639640
for (PdfFormField field : fields) {
640641
PdfDictionary fieldObject = field.getPdfObject();
@@ -683,7 +684,8 @@ public void flattenFields() {
683684
if (page.isFlushed()) {
684685
throw new PdfException(PdfException.PageAlreadyFlushedUseAddFieldAppearanceToPageMethodBeforePageFlushing);
685686
}
686-
PdfCanvas canvas = new PdfCanvas(page);
687+
PdfCanvas canvas = new PdfCanvas(page, !wrappedPages.contains(page));
688+
wrappedPages.add(page);
687689

688690
// Here we avoid circular reference which might occur when page resources and the appearance xObject's
689691
// resources are the same object

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ This file is part of the iText (R) project.
6464
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
6565
import com.itextpdf.kernel.utils.CompareTool;
6666
import com.itextpdf.layout.Canvas;
67+
import com.itextpdf.layout.Document;
68+
import com.itextpdf.layout.element.AreaBreak;
6769
import com.itextpdf.layout.element.Paragraph;
6870
import com.itextpdf.test.ExtendedITextTest;
6971
import com.itextpdf.test.annotations.LogMessage;
@@ -887,4 +889,34 @@ public void regenerateMaxLenCombTest() throws IOException, InterruptedException
887889
Assert.fail(errorMessage);
888890
}
889891
}
892+
893+
@Test
894+
public void wrapPrecedingContentOnFlattenTest() throws IOException, InterruptedException {
895+
String filename = destinationFolder + "wrapPrecedingContentOnFlattenTest.pdf";
896+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename));
897+
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
898+
canvas.setFillColor(ColorConstants.MAGENTA);
899+
900+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
901+
PdfTextFormField[] fields = new PdfTextFormField[4];
902+
for (int i = 0; i < 4; i++)
903+
fields[i] = PdfFormField.createText(pdfDoc, new Rectangle(90, 700 - i * 100, 150, 22), "black" + i, "black");
904+
form.addField(fields[0]);
905+
form.addField(fields[1]);
906+
Document doc = new Document(pdfDoc);
907+
doc.add(new AreaBreak());
908+
canvas = new PdfCanvas(pdfDoc.getPage(2));
909+
canvas.setFillColor(ColorConstants.CYAN);
910+
form.addField(fields[2]);
911+
form.addField(fields[3], pdfDoc.getFirstPage());
912+
form.flattenFields();
913+
914+
pdfDoc.close();
915+
916+
CompareTool compareTool = new CompareTool();
917+
String errorMessage = compareTool.compareByContent(filename, sourceFolder + "cmp_wrapPrecedingContentOnFlattenTest.pdf", destinationFolder, "diff_");
918+
if (errorMessage != null) {
919+
Assert.fail(errorMessage);
920+
}
921+
}
890922
}

0 commit comments

Comments
 (0)