Skip to content

Commit f8f2188

Browse files
Yahor MartsynkovskyiText-CI
authored andcommitted
Fix constructor of PdfReader to close() the PdfTokenizer when exception thrown
Add PdfWriter/PdfReader/PdfDocument constructors to try-with-resources Add tests to compareLinkAnnotation method and encrypt method DEVSIX-3504 Autoported commit. Original commit hash: [0178a9fec]
1 parent 0f01e0b commit f8f2188

File tree

9 files changed

+377
-206
lines changed

9 files changed

+377
-206
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using System;
44+
using System.IO;
45+
using iText.Test;
46+
47+
namespace iText.Kernel.Pdf {
48+
public class PdfEncryptorTest : ExtendedITextTest {
49+
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
50+
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/PdfEncryptorTest/";
51+
52+
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
53+
+ "/test/itext/kernel/pdf/PdfEncryptorTest/";
54+
55+
[NUnit.Framework.OneTimeSetUp]
56+
public static void BeforeClass() {
57+
CreateDestinationFolder(DESTINATION_FOLDER);
58+
}
59+
60+
[NUnit.Framework.Test]
61+
public virtual void EncryptFileTest() {
62+
String outFileName = DESTINATION_FOLDER + "encryptFileTest.pdf";
63+
String initialFileName = SOURCE_FOLDER + "initial.pdf";
64+
PdfEncryptor encryptor = new PdfEncryptor();
65+
EncryptionProperties encryptionProperties = new EncryptionProperties();
66+
encryptionProperties.SetStandardEncryption(new byte[16], new byte[16], 0, 0);
67+
encryptor.SetEncryptionProperties(encryptionProperties);
68+
using (PdfReader initialFile = new PdfReader(initialFileName)) {
69+
using (FileStream outputStream = new FileStream(outFileName, FileMode.Create)) {
70+
encryptor.Encrypt(initialFile, outputStream);
71+
}
72+
}
73+
ReaderProperties readerProperties = new ReaderProperties();
74+
readerProperties.SetPassword(new byte[16]);
75+
PdfReader outFile = new PdfReader(outFileName, readerProperties);
76+
PdfDocument doc = new PdfDocument(outFile);
77+
doc.Close();
78+
NUnit.Framework.Assert.IsTrue(outFile.IsEncrypted());
79+
}
80+
}
81+
}

itext.tests/itext.kernel.tests/itext/kernel/utils/CompareToolTest.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ source product.
4343
using System;
4444
using System.IO;
4545
using iText.IO;
46+
using iText.IO.Font.Constants;
4647
using iText.IO.Util;
48+
using iText.Kernel.Font;
49+
using iText.Kernel.Geom;
50+
using iText.Kernel.Pdf;
51+
using iText.Kernel.Pdf.Annot;
52+
using iText.Kernel.Pdf.Canvas;
53+
using iText.Kernel.Pdf.Navigation;
4754
using iText.Test;
4855
using iText.Test.Attributes;
4956

@@ -196,5 +203,61 @@ public virtual void CompareVisuallyDiffTestTest() {
196203
NUnit.Framework.Assert.IsTrue(new FileInfo(destinationFolder + "diff_1.png").Exists);
197204
NUnit.Framework.Assert.IsTrue(new FileInfo(destinationFolder + "diff_2.png").Exists);
198205
}
206+
207+
[NUnit.Framework.Test]
208+
public virtual void CompareDiffFilesWithSameLinkAnnotationTest() {
209+
String firstPdf = destinationFolder + "firstPdf.pdf";
210+
String secondPdf = destinationFolder + "secondPdf.pdf";
211+
PdfDocument firstDocument = new PdfDocument(new PdfWriter(firstPdf));
212+
PdfPage page1FirstDocument = firstDocument.AddNewPage();
213+
PdfCanvas canvas = new PdfCanvas(page1FirstDocument);
214+
canvas.BeginText();
215+
canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD), 14);
216+
canvas.MoveText(100, 600);
217+
canvas.ShowText("Page 1");
218+
canvas.MoveText(0, -30);
219+
canvas.ShowText("Link to page 1. Click here!");
220+
canvas.EndText();
221+
canvas.Release();
222+
page1FirstDocument.AddAnnotation(new PdfLinkAnnotation(new Rectangle(100, 560, 260, 25)).SetDestination(PdfExplicitDestination
223+
.CreateFit(page1FirstDocument)).SetBorder(new PdfArray(new float[] { 0, 0, 1 })));
224+
page1FirstDocument.Flush();
225+
firstDocument.Close();
226+
PdfDocument secondDocument = new PdfDocument(new PdfWriter(secondPdf));
227+
PdfPage page1secondDocument = secondDocument.AddNewPage();
228+
canvas = new PdfCanvas(page1secondDocument);
229+
canvas.BeginText();
230+
canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD), 14);
231+
canvas.MoveText(100, 600);
232+
canvas.ShowText("Page 1 wit different Text");
233+
canvas.MoveText(0, -30);
234+
canvas.ShowText("Link to page 1. Click here!");
235+
canvas.EndText();
236+
canvas.Release();
237+
page1secondDocument.AddAnnotation(new PdfLinkAnnotation(new Rectangle(100, 560, 260, 25)).SetDestination(PdfExplicitDestination
238+
.CreateFit(page1secondDocument)).SetBorder(new PdfArray(new float[] { 0, 0, 1 })));
239+
page1secondDocument.Flush();
240+
secondDocument.Close();
241+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareLinkAnnotations(firstPdf, secondPdf));
242+
}
243+
244+
[NUnit.Framework.Test]
245+
public virtual void CompareFilesWithDiffLinkAnnotationTest() {
246+
String firstPdf = destinationFolder + "outPdf.pdf";
247+
String secondPdf = destinationFolder + "secondPdf.pdf";
248+
PdfDocument firstDocument = new PdfDocument(new PdfWriter(firstPdf));
249+
PdfDocument secondDocument = new PdfDocument(new PdfWriter(secondPdf));
250+
PdfPage page1FirstDocument = firstDocument.AddNewPage();
251+
page1FirstDocument.AddAnnotation(new PdfLinkAnnotation(new Rectangle(100, 560, 400, 50)).SetDestination(PdfExplicitDestination
252+
.CreateFit(page1FirstDocument)).SetBorder(new PdfArray(new float[] { 0, 0, 1 })));
253+
page1FirstDocument.Flush();
254+
firstDocument.Close();
255+
PdfPage page1SecondDocument = secondDocument.AddNewPage();
256+
page1SecondDocument.AddAnnotation(new PdfLinkAnnotation(new Rectangle(100, 560, 260, 25)).SetDestination(PdfExplicitDestination
257+
.CreateFit(page1SecondDocument)).SetBorder(new PdfArray(new float[] { 0, 0, 1 })));
258+
page1SecondDocument.Flush();
259+
secondDocument.Close();
260+
NUnit.Framework.Assert.IsNotNull(new CompareTool().CompareLinkAnnotations(firstPdf, secondPdf));
261+
}
199262
}
200263
}

itext/itext.kernel/itext/kernel/pdf/PdfEncryptor.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,21 @@ public iText.Kernel.Pdf.PdfEncryptor SetEncryptionProperties(EncryptionPropertie
234234
public void Encrypt(PdfReader reader, Stream os, IDictionary<String, String> newInfo) {
235235
WriterProperties writerProperties = new WriterProperties();
236236
writerProperties.encryptionProperties = properties;
237-
PdfWriter writer = new PdfWriter(os, writerProperties);
238237
StampingProperties stampingProperties = new StampingProperties();
239238
stampingProperties.SetEventCountingMetaInfo(metaInfo);
240-
PdfDocument document = new PdfDocument(reader, writer, stampingProperties);
241-
document.GetDocumentInfo().SetMoreInfo(newInfo);
242-
document.Close();
239+
try {
240+
using (PdfWriter writer = new PdfWriter(os, writerProperties)) {
241+
using (PdfDocument document = new PdfDocument(reader, writer, stampingProperties)) {
242+
document.GetDocumentInfo().SetMoreInfo(newInfo);
243+
}
244+
}
245+
}
246+
catch (System.IO.IOException) {
247+
}
243248
}
244249

250+
//The close() method of OutputStream throws an exception, but we don't need to do anything in this case,
251+
// because OutputStream#close() does nothing.
245252
/// <summary>Entry point to encrypt a PDF document.</summary>
246253
/// <param name="reader">the read PDF</param>
247254
/// <param name="os">the output destination</param>

itext/itext.kernel/itext/kernel/pdf/PdfReader.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,8 +1444,21 @@ private void ReadDecryptObj() {
14441444
/// <param name="byteSource">the source to check</param>
14451445
/// <returns>a tokeniser that is guaranteed to start at the PDF header</returns>
14461446
private static PdfTokenizer GetOffsetTokeniser(IRandomAccessSource byteSource) {
1447+
iText.IO.IOException possibleException = null;
14471448
PdfTokenizer tok = new PdfTokenizer(new RandomAccessFileOrArray(byteSource));
1448-
int offset = tok.GetHeaderOffset();
1449+
int offset;
1450+
try {
1451+
offset = tok.GetHeaderOffset();
1452+
}
1453+
catch (iText.IO.IOException ex) {
1454+
possibleException = ex;
1455+
throw possibleException;
1456+
}
1457+
finally {
1458+
if (possibleException != null) {
1459+
tok.Close();
1460+
}
1461+
}
14491462
if (offset != 0) {
14501463
IRandomAccessSource offsetSource = new WindowRandomAccessSource(byteSource, offset);
14511464
tok = new PdfTokenizer(new RandomAccessFileOrArray(offsetSource));

0 commit comments

Comments
 (0)