|
16 | 16 | */ |
17 | 17 | package org.apache.pdfbox.pdfwriter; |
18 | 18 |
|
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 21 | + |
19 | 22 | import java.io.BufferedOutputStream; |
20 | 23 | import java.io.ByteArrayOutputStream; |
21 | 24 | import java.io.File; |
22 | 25 | import java.io.IOException; |
23 | 26 | import java.nio.file.Paths; |
24 | 27 |
|
25 | 28 | import org.apache.pdfbox.Loader; |
| 29 | +import org.apache.pdfbox.cos.COSDocument; |
| 30 | +import org.apache.pdfbox.cos.COSName; |
| 31 | +import org.apache.pdfbox.cos.COSObjectKey; |
26 | 32 | import org.apache.pdfbox.multipdf.PageExtractor; |
| 33 | +import org.apache.pdfbox.pdfwriter.compress.CompressParameters; |
27 | 34 | import org.apache.pdfbox.pdmodel.PDDocument; |
28 | 35 | import org.apache.pdfbox.pdmodel.PDPage; |
| 36 | +import org.apache.pdfbox.pdmodel.PDResources; |
| 37 | +import org.apache.pdfbox.pdmodel.common.PDRectangle; |
| 38 | +import org.apache.pdfbox.pdmodel.font.PDFont; |
| 39 | +import org.apache.pdfbox.pdmodel.font.PDType1Font; |
| 40 | +import org.apache.pdfbox.pdmodel.font.Standard14Fonts; |
| 41 | +import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; |
| 42 | +import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; |
| 43 | +import org.apache.pdfbox.pdmodel.interactive.form.PDTextField; |
29 | 44 | import org.junit.jupiter.api.Test; |
30 | 45 |
|
31 | 46 | class COSWriterTest |
@@ -68,4 +83,69 @@ void testPDFBox5485() throws Exception |
68 | 83 | } |
69 | 84 | } |
70 | 85 | } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testPDFBox5945() throws Exception |
| 89 | + { |
| 90 | + byte[] input = create(); |
| 91 | + checkTrailerSize(input); |
| 92 | + |
| 93 | + byte[] output = edit(input); |
| 94 | + checkTrailerSize(output); |
| 95 | + } |
| 96 | + |
| 97 | + private static void checkTrailerSize(byte[] docData) throws IOException |
| 98 | + { |
| 99 | + try (PDDocument pdDocument = Loader.loadPDF(docData)) |
| 100 | + { |
| 101 | + COSDocument cosDocument = pdDocument.getDocument(); |
| 102 | + long maxObjNumber = cosDocument.getXrefTable().keySet().stream() // |
| 103 | + .mapToLong(COSObjectKey::getNumber).max().getAsLong(); |
| 104 | + long sizeFromTrailer = cosDocument.getTrailer().getLong(COSName.SIZE); |
| 105 | + assertEquals(maxObjNumber + 1, sizeFromTrailer); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static byte[] create() throws IOException |
| 110 | + { |
| 111 | + try (PDDocument pdDocument = new PDDocument()) |
| 112 | + { |
| 113 | + PDAcroForm acroForm = new PDAcroForm(pdDocument); |
| 114 | + pdDocument.getDocumentCatalog().setAcroForm(acroForm); |
| 115 | + PDFont font1 = new PDType1Font(Standard14Fonts.FontName.HELVETICA); |
| 116 | + PDFont font2 = new PDType1Font(Standard14Fonts.FontName.ZAPF_DINGBATS); |
| 117 | + PDResources resources = new PDResources(); |
| 118 | + resources.put(COSName.getPDFName("Helv"), font1); |
| 119 | + resources.put(COSName.getPDFName("ZaDb"), font2); |
| 120 | + acroForm.setDefaultResources(resources); |
| 121 | + PDPage page = new PDPage(PDRectangle.A4); |
| 122 | + pdDocument.addPage(page); |
| 123 | + PDTextField textField = new PDTextField(acroForm); |
| 124 | + textField.setPartialName("textFieldName"); |
| 125 | + acroForm.getFields().add(textField); |
| 126 | + PDAnnotationWidget widget = textField.getWidgets().get(0); |
| 127 | + widget.setPage(page); |
| 128 | + page.getAnnotations().add(widget); |
| 129 | + PDRectangle rectangle = new PDRectangle(10, 200, 200, 15); |
| 130 | + widget.setRectangle(rectangle); |
| 131 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 132 | + pdDocument.save(out, CompressParameters.NO_COMPRESSION); |
| 133 | + return out.toByteArray(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static byte[] edit(byte[] input) throws IOException |
| 138 | + { |
| 139 | + try (PDDocument pdDocument = Loader.loadPDF(input)) |
| 140 | + { |
| 141 | + PDTextField textField = (PDTextField) pdDocument.getDocumentCatalog().getAcroForm() |
| 142 | + .getField("textFieldName"); |
| 143 | + assertNotNull(textField); |
| 144 | + textField.setMultiline(true); |
| 145 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 146 | + pdDocument.saveIncremental(out); |
| 147 | + return out.toByteArray(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
71 | 151 | } |
0 commit comments