|
| 1 | +package com.itextpdf.kernel.pdf; |
| 2 | + |
| 3 | +import com.itextpdf.io.LogMessageConstant; |
| 4 | +import com.itextpdf.kernel.PdfException; |
| 5 | +import com.itextpdf.kernel.geom.Rectangle; |
| 6 | +import com.itextpdf.kernel.pdf.annot.PdfAnnotation; |
| 7 | +import com.itextpdf.kernel.utils.CompareTool; |
| 8 | +import com.itextpdf.test.ExtendedITextTest; |
| 9 | +import com.itextpdf.test.annotations.LogMessage; |
| 10 | +import com.itextpdf.test.annotations.LogMessages; |
| 11 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Rule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.experimental.categories.Category; |
| 19 | +import org.junit.rules.ExpectedException; |
| 20 | + |
| 21 | +@Category(IntegrationTest.class) |
| 22 | +public class PdfObjectReleaseTest extends ExtendedITextTest { |
| 23 | + |
| 24 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/PdfObjectReleaseTest/"; |
| 25 | + public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/PdfObjectReleaseTest/"; |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void beforeClass() { |
| 29 | + createOrClearDestinationFolder(destinationFolder); |
| 30 | + } |
| 31 | + |
| 32 | + @Rule |
| 33 | + public ExpectedException junitExpectedException = ExpectedException.none(); |
| 34 | + |
| 35 | + @Test |
| 36 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FORBID_RELEASE_IS_SET, count = 108)) |
| 37 | + public void releaseObjectsInDocWithStructTreeRootTest() throws IOException, InterruptedException { |
| 38 | + singlePdfObjectReleaseTest("releaseObjectsInDocWithStructTreeRoot.pdf", |
| 39 | + "releaseObjectsInDocWithStructTreeRoot_stamping.pdf", |
| 40 | + "releaseObjectsInDocWithStructTreeRoot_stamping_release.pdf"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FORBID_RELEASE_IS_SET, count = 5)) |
| 45 | + public void releaseObjectsInDocWithXfaTest() throws IOException, InterruptedException { |
| 46 | + singlePdfObjectReleaseTest("releaseObjectsInDocWithXfa.pdf", |
| 47 | + "releaseObjectsInDocWithXfa_stamping.pdf", |
| 48 | + "releaseObjectsInDocWithXfa_stamping_release.pdf"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FORBID_RELEASE_IS_SET, count = 3)) |
| 53 | + public void releaseObjectsInSimpleDocTest() throws IOException, InterruptedException { |
| 54 | + singlePdfObjectReleaseTest("releaseObjectsInSimpleDoc.pdf", |
| 55 | + "releaseObjectsInSimpleDoc_stamping.pdf", |
| 56 | + "releaseObjectsInSimpleDoc_stamping_release.pdf"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FORBID_RELEASE_IS_SET)) |
| 61 | + public void releaseCatalogTest() throws IOException, InterruptedException { |
| 62 | + String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf"; |
| 63 | + String release = destinationFolder + "outReleaseObjectsInSimpleDoc.pdf"; |
| 64 | + |
| 65 | + try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) { |
| 66 | + doc.getCatalog().getPdfObject().release(); |
| 67 | + } |
| 68 | + |
| 69 | + Assert.assertNull(new CompareTool().compareByContent(release, srcFile, destinationFolder)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FORBID_RELEASE_IS_SET)) |
| 74 | + public void releasePagesTest() throws IOException, InterruptedException { |
| 75 | + String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf"; |
| 76 | + String release = destinationFolder + "outReleaseObjectsInSimpleDoc.pdf"; |
| 77 | + |
| 78 | + try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) { |
| 79 | + doc.getCatalog().getPdfObject().getAsDictionary(PdfName.Pages).release(); |
| 80 | + } |
| 81 | + |
| 82 | + Assert.assertNull(new CompareTool().compareByContent(release, srcFile, destinationFolder)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.FORBID_RELEASE_IS_SET)) |
| 87 | + public void releaseStructTreeRootTest() throws IOException, InterruptedException { |
| 88 | + String srcFile = sourceFolder + "releaseObjectsInDocWithStructTreeRoot.pdf"; |
| 89 | + String release = destinationFolder + "outReleaseObjectsInDocWithStructTreeRoot.pdf"; |
| 90 | + |
| 91 | + try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) { |
| 92 | + doc.getStructTreeRoot().getPdfObject().release(); |
| 93 | + } |
| 94 | + |
| 95 | + Assert.assertNull(new CompareTool().compareByContent(release, srcFile, destinationFolder)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void noForbidReleaseObjectsModifyingTest() throws IOException, InterruptedException { |
| 100 | + String srcFile = sourceFolder + "noForbidReleaseObjectsModifying.pdf"; |
| 101 | + String stampReleased = sourceFolder + "noForbidReleaseObjectsModified.pdf"; |
| 102 | + |
| 103 | + try (PdfDocument doc = new PdfDocument( |
| 104 | + new PdfReader(srcFile), |
| 105 | + new PdfWriter(destinationFolder + "noForbidReleaseObjectsModifying.pdf"), |
| 106 | + new StampingProperties().useAppendMode())) { |
| 107 | + |
| 108 | + PdfAnnotation annots = doc.getPage(1).getAnnotations().get(0); |
| 109 | + |
| 110 | + annots.setRectangle(new PdfArray(new Rectangle(100, 100, 80, 50))); |
| 111 | + annots.getRectangle().release(); |
| 112 | + } |
| 113 | + |
| 114 | + try (PdfDocument openPrev = new PdfDocument(new PdfReader(stampReleased))) { |
| 115 | + Assert.assertTrue(new Rectangle(100, 100, 80, 50).equalsWithEpsilon( |
| 116 | + openPrev.getPage(1).getAnnotations().get(0).getRectangle().toRectangle())); |
| 117 | + } |
| 118 | + |
| 119 | + Assert.assertNotNull(new CompareTool().compareByContent(srcFile, stampReleased, destinationFolder)); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void addingReleasedObjectToDocumentTest() throws IOException { |
| 124 | + String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf"; |
| 125 | + |
| 126 | + PdfDocument doc = new PdfDocument(new PdfReader(srcFile), |
| 127 | + new PdfWriter(sourceFolder + "addingReleasedObjectToDocument.pdf")); |
| 128 | + try { |
| 129 | + PdfObject releasedObj = doc.getPdfObject(1); |
| 130 | + releasedObj.release(); |
| 131 | + |
| 132 | + doc.getCatalog().put(PdfName.Outlines, releasedObj); |
| 133 | + } finally { |
| 134 | + junitExpectedException.expect(PdfException.class); |
| 135 | + junitExpectedException.expectMessage("Cannot write object after it was released." |
| 136 | + + " In normal situation the object must be read once again before being written."); |
| 137 | + doc.close(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void singlePdfObjectReleaseTest(String inputFilename, String outStampingFilename, String outStampingReleaseFilename) throws IOException, InterruptedException { |
| 142 | + String srcFile = sourceFolder + inputFilename; |
| 143 | + String outPureStamping = destinationFolder + outStampingFilename; |
| 144 | + String outStampingRelease = destinationFolder + outStampingReleaseFilename; |
| 145 | + |
| 146 | + PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outPureStamping)); |
| 147 | + // We open/close document to make sure that the results of release logic and simple overwriting coincide. |
| 148 | + doc.close(); |
| 149 | + |
| 150 | + PdfDocument stamperRelease = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outStampingRelease)); |
| 151 | + |
| 152 | + for (int i = 0; i < stamperRelease.getNumberOfPdfObjects(); i++) { |
| 153 | + PdfObject pdfObject = stamperRelease.getPdfObject(i); |
| 154 | + if (pdfObject != null) { |
| 155 | + stamperRelease.getPdfObject(i).release(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + stamperRelease.close(); |
| 160 | + |
| 161 | + Assert.assertNull(new CompareTool().compareByContent(outStampingRelease, outPureStamping, destinationFolder)); |
| 162 | + } |
| 163 | +} |
0 commit comments