Skip to content

Commit 8e7ce0d

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add PdfObjectReleaseTest class
DEVSIX-4482 Autoported commit. Original commit hash: [81b15d4e8]
1 parent 77f17ca commit 8e7ce0d

File tree

9 files changed

+151
-1
lines changed

9 files changed

+151
-1
lines changed

itext.tests/itext.forms.tests/itext/forms/PdfFormFieldTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,5 +1134,22 @@ public virtual void BorderWidthIndentSingleLineTest() {
11341134
NUnit.Framework.Assert.Fail(errorMessage);
11351135
}
11361136
}
1137+
1138+
[NUnit.Framework.Test]
1139+
public virtual void ReleaseAcroformTest() {
1140+
String srcFile = sourceFolder + "formFieldFile.pdf";
1141+
String outPureStamping = destinationFolder + "formFieldFileStamping.pdf";
1142+
String outStampingRelease = destinationFolder + "formFieldFileStampingRelease.pdf";
1143+
PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outPureStamping));
1144+
// We open/close document to make sure that the results of release logic and simple overwriting coincide.
1145+
doc.Close();
1146+
using (PdfDocument stamperRelease = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outStampingRelease
1147+
))) {
1148+
PdfAcroForm form = PdfAcroForm.GetAcroForm(stamperRelease, false);
1149+
form.Release();
1150+
}
1151+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outStampingRelease, outPureStamping, destinationFolder
1152+
));
1153+
}
11371154
}
11381155
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bba6f3848485162379a0fb31cc3d1d6a60e07724
1+
81b15d4e8d15aa59254c01638ce9fec1d2f5c4e2

0 commit comments

Comments
 (0)