|
| 1 | +package com.itextpdf.kernel.pdf; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.font.PdfFont; |
| 4 | +import com.itextpdf.kernel.font.PdfFontFactory; |
| 5 | +import com.itextpdf.kernel.geom.Rectangle; |
| 6 | +import com.itextpdf.kernel.pdf.action.PdfAction; |
| 7 | +import com.itextpdf.kernel.pdf.annot.PdfCircleAnnotation; |
| 8 | +import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation; |
| 9 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvas; |
| 10 | +import com.itextpdf.kernel.utils.CompareTool; |
| 11 | +import com.itextpdf.test.ExtendedITextTest; |
| 12 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 13 | +import java.io.ByteArrayInputStream; |
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Map; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.BeforeClass; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.experimental.categories.Category; |
| 21 | + |
| 22 | +@Category(IntegrationTest.class) |
| 23 | +public class SmartModeTest extends ExtendedITextTest { |
| 24 | + |
| 25 | + public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/SmartModeTest/"; |
| 26 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/SmartModeTest/"; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void beforeClass() { |
| 30 | + createOrClearDestinationFolder(destinationFolder); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void smartModeSameResourcesCopyingAndFlushing() throws IOException, InterruptedException { |
| 35 | + String outFile = destinationFolder + "smartModeSameResourcesCopyingAndFlushing.pdf"; |
| 36 | + String cmpFile = sourceFolder + "cmp_smartModeSameResourcesCopyingAndFlushing.pdf"; |
| 37 | + String[] srcFiles = new String[]{ |
| 38 | + sourceFolder + "indirectResourcesStructure.pdf", |
| 39 | + sourceFolder + "indirectResourcesStructure2.pdf" |
| 40 | + }; |
| 41 | + |
| 42 | + PdfDocument outputDoc = new PdfDocument(new PdfWriter(outFile, new WriterProperties().useSmartMode())); |
| 43 | + |
| 44 | + for (String srcFile : srcFiles) { |
| 45 | + PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile)); |
| 46 | + sourceDoc.copyPagesTo(1, sourceDoc.getNumberOfPages(), outputDoc); |
| 47 | + sourceDoc.close(); |
| 48 | + |
| 49 | + outputDoc.flushCopiedObjects(sourceDoc); |
| 50 | + } |
| 51 | + |
| 52 | + outputDoc.close(); |
| 53 | + |
| 54 | + PdfDocument assertDoc = new PdfDocument(new PdfReader(outFile)); |
| 55 | + PdfIndirectReference page1ResFontObj = assertDoc.getPage(1).getPdfObject().getAsDictionary(PdfName.Resources) |
| 56 | + .getAsDictionary(PdfName.Font).getIndirectReference(); |
| 57 | + PdfIndirectReference page2ResFontObj = assertDoc.getPage(2).getPdfObject().getAsDictionary(PdfName.Resources) |
| 58 | + .getAsDictionary(PdfName.Font).getIndirectReference(); |
| 59 | + PdfIndirectReference page3ResFontObj = assertDoc.getPage(3).getPdfObject().getAsDictionary(PdfName.Resources) |
| 60 | + .getAsDictionary(PdfName.Font).getIndirectReference(); |
| 61 | + |
| 62 | + Assert.assertTrue(page1ResFontObj.equals(page2ResFontObj)); |
| 63 | + Assert.assertTrue(page1ResFontObj.equals(page3ResFontObj)); |
| 64 | + assertDoc.close(); |
| 65 | + |
| 66 | + Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFile, destinationFolder)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void smartModeSameResourcesCopyingModifyingAndFlushing() throws IOException { |
| 71 | + String outFile = destinationFolder + "smartModeSameResourcesCopyingModifyingAndFlushing.pdf"; |
| 72 | + String[] srcFiles = new String[]{ |
| 73 | + sourceFolder + "indirectResourcesStructure.pdf", |
| 74 | + sourceFolder + "indirectResourcesStructure2.pdf" |
| 75 | + }; |
| 76 | + boolean exceptionCaught = false; |
| 77 | + |
| 78 | + PdfDocument outputDoc = new PdfDocument(new PdfWriter(outFile, new WriterProperties().useSmartMode())); |
| 79 | + |
| 80 | + int lastPageNum = 1; |
| 81 | + PdfFont font = PdfFontFactory.createFont(); |
| 82 | + for (String srcFile : srcFiles) { |
| 83 | + PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile)); |
| 84 | + sourceDoc.copyPagesTo(1, sourceDoc.getNumberOfPages(), outputDoc); |
| 85 | + sourceDoc.close(); |
| 86 | + |
| 87 | + int i; |
| 88 | + for (i = lastPageNum; i <= outputDoc.getNumberOfPages(); ++i) { |
| 89 | + PdfCanvas canvas; |
| 90 | + try { |
| 91 | + canvas = new PdfCanvas(outputDoc.getPage(i)); |
| 92 | + } catch (NullPointerException expected) { |
| 93 | + // Smart mode makes it possible to share objects coming from different source documents. |
| 94 | + // Flushing one object documents might make it impossible to modify further copied objects. |
| 95 | + Assert.assertEquals(2, i); |
| 96 | + exceptionCaught = true; |
| 97 | + break; |
| 98 | + } |
| 99 | + canvas.beginText().moveText(36, 36).setFontAndSize(font, 12).showText("Page " + i).endText(); |
| 100 | + } |
| 101 | + lastPageNum = i; |
| 102 | + |
| 103 | + if (exceptionCaught) { |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + outputDoc.flushCopiedObjects(sourceDoc); |
| 108 | + } |
| 109 | + |
| 110 | + if (!exceptionCaught) { |
| 111 | + Assert.fail(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh() throws IOException, InterruptedException { |
| 117 | + String outFile = destinationFolder + "smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf"; |
| 118 | + String cmpFile = sourceFolder + "cmp_smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf"; |
| 119 | + String[] srcFiles = new String[]{ |
| 120 | + sourceFolder + "indirectResourcesStructure.pdf", |
| 121 | + sourceFolder + "indirectResourcesStructure2.pdf" |
| 122 | + }; |
| 123 | + |
| 124 | + PdfDocument outputDoc = new PdfDocument(new PdfWriter(outFile, new WriterProperties().useSmartMode())); |
| 125 | + |
| 126 | + int lastPageNum = 1; |
| 127 | + PdfFont font = PdfFontFactory.createFont(); |
| 128 | + for (String srcFile : srcFiles) { |
| 129 | + PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile)); |
| 130 | + for (int i = 1; i <= sourceDoc.getNumberOfPages(); ++i) { |
| 131 | + PdfDictionary srcRes = sourceDoc.getPage(i).getPdfObject().getAsDictionary(PdfName.Resources); |
| 132 | + |
| 133 | + // Ensures that objects copied to the output document are fresh, |
| 134 | + // i.e. are not reused from already copied objects cache. |
| 135 | + boolean ensureObjectIsFresh = true; |
| 136 | + // it's crucial to copy first inner objects and then the container object! |
| 137 | + for (PdfObject v : srcRes.values()) { |
| 138 | + if (v.getIndirectReference() != null) { |
| 139 | + // We are not interested in returned copied objects instances, they will be picked up by |
| 140 | + // general copying mechanism from copied objects cache by default. |
| 141 | + v.copyTo(outputDoc, ensureObjectIsFresh); |
| 142 | + } |
| 143 | + } |
| 144 | + if (srcRes.getIndirectReference() != null) { |
| 145 | + srcRes.copyTo(outputDoc, ensureObjectIsFresh); |
| 146 | + } |
| 147 | + } |
| 148 | + sourceDoc.copyPagesTo(1, sourceDoc.getNumberOfPages(), outputDoc); |
| 149 | + sourceDoc.close(); |
| 150 | + |
| 151 | + int i; |
| 152 | + for (i = lastPageNum; i <= outputDoc.getNumberOfPages(); ++i) { |
| 153 | + PdfPage page = outputDoc.getPage(i); |
| 154 | + PdfCanvas canvas = new PdfCanvas(page); |
| 155 | + canvas.beginText().moveText(36, 36).setFontAndSize(font, 12).showText("Page " + i).endText(); |
| 156 | + } |
| 157 | + lastPageNum = i; |
| 158 | + |
| 159 | + outputDoc.flushCopiedObjects(sourceDoc); |
| 160 | + } |
| 161 | + |
| 162 | + outputDoc.close(); |
| 163 | + |
| 164 | + PdfDocument assertDoc = new PdfDocument(new PdfReader(outFile)); |
| 165 | + PdfIndirectReference page1ResFontObj = assertDoc.getPage(1).getPdfObject().getAsDictionary(PdfName.Resources) |
| 166 | + .getAsDictionary(PdfName.Font).getIndirectReference(); |
| 167 | + PdfIndirectReference page2ResFontObj = assertDoc.getPage(2).getPdfObject().getAsDictionary(PdfName.Resources) |
| 168 | + .getAsDictionary(PdfName.Font).getIndirectReference(); |
| 169 | + PdfIndirectReference page3ResFontObj = assertDoc.getPage(3).getPdfObject().getAsDictionary(PdfName.Resources) |
| 170 | + .getAsDictionary(PdfName.Font).getIndirectReference(); |
| 171 | + |
| 172 | + Assert.assertFalse(page1ResFontObj.equals(page2ResFontObj)); |
| 173 | + Assert.assertFalse(page1ResFontObj.equals(page3ResFontObj)); |
| 174 | + Assert.assertFalse(page2ResFontObj.equals(page3ResFontObj)); |
| 175 | + assertDoc.close(); |
| 176 | + |
| 177 | + Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFile, destinationFolder)); |
| 178 | + } |
| 179 | +} |
0 commit comments