|
| 1 | +using System; |
| 2 | +using iText.Kernel; |
| 3 | +using iText.Kernel.Geom; |
| 4 | +using iText.Kernel.Pdf.Annot; |
| 5 | +using iText.Kernel.Utils; |
| 6 | +using iText.Test; |
| 7 | +using iText.Test.Attributes; |
| 8 | + |
| 9 | +namespace iText.Kernel.Pdf { |
| 10 | + public class PdfObjectReleaseTest : ExtendedITextTest { |
| 11 | + public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 12 | + .CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/PdfObjectReleaseTest/"; |
| 13 | + |
| 14 | + public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory |
| 15 | + + "/test/itext/kernel/pdf/PdfObjectReleaseTest/"; |
| 16 | + |
| 17 | + [NUnit.Framework.OneTimeSetUp] |
| 18 | + public static void BeforeClass() { |
| 19 | + CreateOrClearDestinationFolder(destinationFolder); |
| 20 | + } |
| 21 | + |
| 22 | + [NUnit.Framework.Test] |
| 23 | + [LogMessage(iText.IO.LogMessageConstant.FORBID_RELEASE_IS_SET, Count = 108)] |
| 24 | + public virtual void ReleaseObjectsInDocWithStructTreeRootTest() { |
| 25 | + SinglePdfObjectReleaseTest("releaseObjectsInDocWithStructTreeRoot.pdf", "releaseObjectsInDocWithStructTreeRoot_stamping.pdf" |
| 26 | + , "releaseObjectsInDocWithStructTreeRoot_stamping_release.pdf"); |
| 27 | + } |
| 28 | + |
| 29 | + [NUnit.Framework.Test] |
| 30 | + [LogMessage(iText.IO.LogMessageConstant.FORBID_RELEASE_IS_SET, Count = 5)] |
| 31 | + public virtual void ReleaseObjectsInDocWithXfaTest() { |
| 32 | + SinglePdfObjectReleaseTest("releaseObjectsInDocWithXfa.pdf", "releaseObjectsInDocWithXfa_stamping.pdf", "releaseObjectsInDocWithXfa_stamping_release.pdf" |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + [NUnit.Framework.Test] |
| 37 | + [LogMessage(iText.IO.LogMessageConstant.FORBID_RELEASE_IS_SET, Count = 3)] |
| 38 | + public virtual void ReleaseObjectsInSimpleDocTest() { |
| 39 | + SinglePdfObjectReleaseTest("releaseObjectsInSimpleDoc.pdf", "releaseObjectsInSimpleDoc_stamping.pdf", "releaseObjectsInSimpleDoc_stamping_release.pdf" |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + [NUnit.Framework.Test] |
| 44 | + [LogMessage(iText.IO.LogMessageConstant.FORBID_RELEASE_IS_SET)] |
| 45 | + public virtual void ReleaseCatalogTest() { |
| 46 | + String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf"; |
| 47 | + String release = destinationFolder + "outReleaseObjectsInSimpleDoc.pdf"; |
| 48 | + using (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) { |
| 49 | + doc.GetCatalog().GetPdfObject().Release(); |
| 50 | + } |
| 51 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(release, srcFile, destinationFolder)); |
| 52 | + } |
| 53 | + |
| 54 | + [NUnit.Framework.Test] |
| 55 | + [LogMessage(iText.IO.LogMessageConstant.FORBID_RELEASE_IS_SET)] |
| 56 | + public virtual void ReleasePagesTest() { |
| 57 | + String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf"; |
| 58 | + String release = destinationFolder + "outReleaseObjectsInSimpleDoc.pdf"; |
| 59 | + using (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) { |
| 60 | + doc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.Pages).Release(); |
| 61 | + } |
| 62 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(release, srcFile, destinationFolder)); |
| 63 | + } |
| 64 | + |
| 65 | + [NUnit.Framework.Test] |
| 66 | + [LogMessage(iText.IO.LogMessageConstant.FORBID_RELEASE_IS_SET)] |
| 67 | + public virtual void ReleaseStructTreeRootTest() { |
| 68 | + String srcFile = sourceFolder + "releaseObjectsInDocWithStructTreeRoot.pdf"; |
| 69 | + String release = destinationFolder + "outReleaseObjectsInDocWithStructTreeRoot.pdf"; |
| 70 | + using (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) { |
| 71 | + doc.GetStructTreeRoot().GetPdfObject().Release(); |
| 72 | + } |
| 73 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(release, srcFile, destinationFolder)); |
| 74 | + } |
| 75 | + |
| 76 | + [NUnit.Framework.Test] |
| 77 | + public virtual void NoForbidReleaseObjectsModifyingTest() { |
| 78 | + String srcFile = sourceFolder + "noForbidReleaseObjectsModifying.pdf"; |
| 79 | + String stampReleased = sourceFolder + "noForbidReleaseObjectsModified.pdf"; |
| 80 | + using (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(destinationFolder + "noForbidReleaseObjectsModifying.pdf" |
| 81 | + ), new StampingProperties().UseAppendMode())) { |
| 82 | + PdfAnnotation annots = doc.GetPage(1).GetAnnotations()[0]; |
| 83 | + annots.SetRectangle(new PdfArray(new Rectangle(100, 100, 80, 50))); |
| 84 | + annots.GetRectangle().Release(); |
| 85 | + } |
| 86 | + using (PdfDocument openPrev = new PdfDocument(new PdfReader(stampReleased))) { |
| 87 | + NUnit.Framework.Assert.IsTrue(new Rectangle(100, 100, 80, 50).EqualsWithEpsilon(openPrev.GetPage(1).GetAnnotations |
| 88 | + ()[0].GetRectangle().ToRectangle())); |
| 89 | + } |
| 90 | + NUnit.Framework.Assert.IsNotNull(new CompareTool().CompareByContent(srcFile, stampReleased, destinationFolder |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + [NUnit.Framework.Test] |
| 95 | + public virtual void AddingReleasedObjectToDocumentTest() { |
| 96 | + String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf"; |
| 97 | + PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(sourceFolder + "addingReleasedObjectToDocument.pdf" |
| 98 | + )); |
| 99 | + try { |
| 100 | + PdfObject releasedObj = doc.GetPdfObject(1); |
| 101 | + releasedObj.Release(); |
| 102 | + doc.GetCatalog().Put(PdfName.Outlines, releasedObj); |
| 103 | + } |
| 104 | + finally { |
| 105 | + NUnit.Framework.Assert.That(() => { |
| 106 | + doc.Close(); |
| 107 | + } |
| 108 | + , NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo("Cannot write object after it was released." + " In normal situation the object must be read once again before being written.")) |
| 109 | +; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void SinglePdfObjectReleaseTest(String inputFilename, String outStampingFilename, String outStampingReleaseFilename |
| 114 | + ) { |
| 115 | + String srcFile = sourceFolder + inputFilename; |
| 116 | + String outPureStamping = destinationFolder + outStampingFilename; |
| 117 | + String outStampingRelease = destinationFolder + outStampingReleaseFilename; |
| 118 | + PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outPureStamping)); |
| 119 | + // We open/close document to make sure that the results of release logic and simple overwriting coincide. |
| 120 | + doc.Close(); |
| 121 | + PdfDocument stamperRelease = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outStampingRelease)); |
| 122 | + for (int i = 0; i < stamperRelease.GetNumberOfPdfObjects(); i++) { |
| 123 | + PdfObject pdfObject = stamperRelease.GetPdfObject(i); |
| 124 | + if (pdfObject != null) { |
| 125 | + stamperRelease.GetPdfObject(i).Release(); |
| 126 | + } |
| 127 | + } |
| 128 | + stamperRelease.Close(); |
| 129 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outStampingRelease, outPureStamping, destinationFolder |
| 130 | + )); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments