Skip to content

Commit ecf1d35

Browse files
ar3emiText-CI
authored andcommitted
Add check that PdfAPage can be flushed
Add PdfAPage class. Use page factories to create pages. Add check that PdfAPage object is ready to be flushed and if no don't call flushing. DEVSIX-2645 Autoported commit. Original commit hash: [44050f2b4]
1 parent b468bb7 commit ecf1d35

File tree

16 files changed

+611
-49
lines changed

16 files changed

+611
-49
lines changed

itext.tests/itext.pdfa.tests/itext/pdfa/PdfAPageEndEventTest.cs renamed to itext.tests/itext.pdfa.tests/itext/pdfa/PdfAAppendModeTest.cs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,70 +42,61 @@ source product.
4242
*/
4343
using System;
4444
using System.IO;
45-
using iText.Kernel.Events;
4645
using iText.Kernel.Font;
47-
using iText.Kernel.Geom;
4846
using iText.Kernel.Pdf;
4947
using iText.Kernel.Pdf.Canvas;
5048
using iText.Kernel.Utils;
51-
using iText.Layout;
52-
using iText.Layout.Element;
5349
using iText.Test;
50+
using iText.Test.Pdfa;
5451

5552
namespace iText.Pdfa {
56-
public class PdfAPageEndEventTest : ExtendedITextTest {
53+
public class PdfAAppendModeTest : ExtendedITextTest {
5754
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
5855
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/";
5956

60-
public static readonly String cmpFolder = sourceFolder + "cmp/PdfAPageEndEventTest/";
57+
public const String testDirName = "PdfAAppendModeTest/";
6158

62-
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
63-
+ "/test/itext/pdfa/PdfAPageEndEventTest/";
59+
public static readonly String cmpFolder = sourceFolder + "cmp/" + testDirName;
60+
61+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
62+
+ "/test/itext/pdfa/" + testDirName;
6463

6564
[NUnit.Framework.OneTimeSetUp]
6665
public static void BeforeClass() {
6766
CreateOrClearDestinationFolder(destinationFolder);
6867
}
6968

7069
[NUnit.Framework.Test]
71-
public virtual void CheckPageEndEvent() {
72-
// TODO DEVSIX-2645
73-
String outPdf = destinationFolder + "checkPageEndEvent.pdf";
74-
String cmpPdf = sourceFolder + "cmp_checkPageEndEvent.pdf";
75-
PdfWriter writer = new PdfWriter(outPdf);
70+
public virtual void AddPageInAppendModeTest() {
71+
String inputFile = destinationFolder + "in_addPageInAppendModeTest.pdf";
72+
String outputFile = destinationFolder + "out_addPageInAppendModeTest.pdf";
73+
String cmpFile = cmpFolder + "cmp_addPageInAppendModeTest.pdf";
74+
CreateInputPdfADocument(inputFile);
75+
PdfDocument pdfADocument = new PdfADocument(new PdfReader(inputFile), new PdfWriter(outputFile), new StampingProperties
76+
().UseAppendMode());
77+
PdfCanvas canvas = new PdfCanvas(pdfADocument.AddNewPage());
78+
canvas.SaveState().BeginText().MoveText(36, 750).SetFontAndSize(PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf"
79+
, true), 16).ShowText("This page 2").EndText().RestoreState();
80+
canvas.Release();
81+
pdfADocument.Close();
82+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(inputFile));
83+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outputFile));
84+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outputFile, cmpFile, destinationFolder, "diff_"
85+
));
86+
}
87+
88+
private static void CreateInputPdfADocument(String docName) {
89+
PdfWriter writer = new PdfWriter(docName);
7690
PdfADocument pdfDoc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom"
7791
, "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(sourceFolder + "sRGB Color Space Profile.icm"
7892
, FileMode.Open, FileAccess.Read)));
7993
pdfDoc.SetTagged();
8094
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
81-
PdfFont freesans = PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf", true);
82-
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new PdfAPageEndEventTest.HeaderEventHandler(freesans));
83-
Document document = new Document(pdfDoc, PageSize.A4);
84-
// TODO fix header duplication on the first page
85-
document.Add(new Paragraph("Hello World on page 1").SetFont(freesans));
86-
document.Add(new AreaBreak());
87-
document.Add(new Paragraph("Hello World on page 2").SetFont(freesans));
88-
document.Add(new AreaBreak());
89-
document.Close();
90-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diff_"
91-
));
92-
}
93-
94-
internal class HeaderEventHandler : IEventHandler {
95-
internal PdfFont font;
96-
97-
internal static int counter = 1;
98-
99-
public HeaderEventHandler(PdfFont font) {
100-
this.font = font;
101-
}
102-
103-
public virtual void HandleEvent(Event @event) {
104-
PdfDocumentEvent pdfEvent = (PdfDocumentEvent)@event;
105-
PdfPage page = pdfEvent.GetPage();
106-
new PdfCanvas(page).BeginText().MoveText(10, page.GetPageSize().GetHeight() - 20).SetFontAndSize(font, 12.0f
107-
).ShowText("Footer " + counter++).EndText();
108-
}
95+
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
96+
canvas.SaveState().BeginText().MoveText(36, 750).SetFontAndSize(PdfFontFactory.CreateFont(sourceFolder + "FreeSans.ttf"
97+
, true), 16).ShowText("This page 1").EndText().RestoreState();
98+
canvas.Release();
99+
pdfDoc.Close();
109100
}
110101
}
111102
}

itext.tests/itext.pdfa.tests/itext/pdfa/PdfAFlushingTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ source product.
4949
using iText.Kernel.Pdf.Xobject;
5050
using iText.Kernel.Utils;
5151
using iText.Test;
52+
using iText.Test.Attributes;
5253

5354
namespace iText.Pdfa {
5455
public class PdfAFlushingTest : ExtendedITextTest {
@@ -64,6 +65,7 @@ public static void BeforeClass() {
6465
}
6566

6667
[NUnit.Framework.Test]
68+
[LogMessage(PdfALogMessageConstant.PDFA_OBJECT_FLUSHING_WAS_NOT_PERFORMED)]
6769
public virtual void FlushingTest01() {
6870
String outPdf = destinationFolder + "pdfA1b_flushingTest01.pdf";
6971
String cmpPdf = sourceFolder + "cmp/PdfAFlushingTest/cmp_pdfA1b_flushingTest01.pdf";
@@ -84,6 +86,7 @@ public virtual void FlushingTest01() {
8486
}
8587

8688
[NUnit.Framework.Test]
89+
[LogMessage(PdfALogMessageConstant.PDFA_PAGE_FLUSHING_WAS_NOT_PERFORMED)]
8790
public virtual void FlushingTest02() {
8891
String outPdf = destinationFolder + "pdfA2b_flushingTest02.pdf";
8992
String cmpPdf = sourceFolder + "cmp/PdfAFlushingTest/cmp_pdfA2b_flushingTest02.pdf";
@@ -126,6 +129,7 @@ public virtual void FlushingTest03() {
126129
}
127130

128131
[NUnit.Framework.Test]
132+
[LogMessage(PdfALogMessageConstant.PDFA_OBJECT_FLUSHING_WAS_NOT_PERFORMED)]
129133
public virtual void AddUnusedStreamObjectsTest() {
130134
String outPdf = destinationFolder + "pdfA1b_docWithUnusedObjects_3.pdf";
131135
String cmpPdf = sourceFolder + "cmp/PdfAFlushingTest/cmp_pdfA1b_docWithUnusedObjects_3.pdf";
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 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.Kernel.Events;
46+
using iText.Kernel.Geom;
47+
using iText.Kernel.Pdf;
48+
using iText.Layout;
49+
using iText.Layout.Element;
50+
using iText.Test;
51+
using iText.Test.Attributes;
52+
using iText.Test.Pdfa;
53+
54+
namespace iText.Pdfa {
55+
public class PdfAPageTest : ExtendedITextTest {
56+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
57+
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/";
58+
59+
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
60+
+ "/test/itext/pdfa/PdfAPageTest/";
61+
62+
[NUnit.Framework.OneTimeSetUp]
63+
public static void BeforeClass() {
64+
CreateOrClearDestinationFolder(destinationFolder);
65+
}
66+
67+
[NUnit.Framework.Test]
68+
[LogMessage(PdfALogMessageConstant.PDFA_PAGE_FLUSHING_WAS_NOT_PERFORMED)]
69+
public virtual void CheckThatFlushingPreventedWhenAddingElementToDocument() {
70+
// Expected log message that page flushing was not performed
71+
String outPdf = destinationFolder + "checkThatFlushingPreventedWhenAddingElementToDocument.pdf";
72+
PdfWriter writer = new PdfWriter(outPdf);
73+
PdfADocument pdfDoc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom"
74+
, "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(sourceFolder + "sRGB Color Space Profile.icm"
75+
, FileMode.Open, FileAccess.Read)));
76+
pdfDoc.SetTagged();
77+
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
78+
PdfAPageTest.EndPageEventHandler eventHandler = new PdfAPageTest.EndPageEventHandler();
79+
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
80+
int pageCount = 3;
81+
Document document = new Document(pdfDoc, PageSize.A4);
82+
for (int i = 1; i < pageCount; i++) {
83+
// Adding a area break causes a new page to be added and an attempt to flush the page will occur,
84+
// but flushing these pages will be prevented due to a condition added to the PdfAPage#flush method
85+
document.Add(new AreaBreak());
86+
}
87+
// Before closing document have 3 pages, but no one call of end page event
88+
NUnit.Framework.Assert.AreEqual(pageCount, document.GetPdfDocument().GetNumberOfPages());
89+
NUnit.Framework.Assert.AreEqual(0, eventHandler.GetCounter());
90+
document.Close();
91+
// During the closing event was called on each document page
92+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
93+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
94+
}
95+
96+
[NUnit.Framework.Test]
97+
[LogMessage(PdfALogMessageConstant.PDFA_PAGE_FLUSHING_WAS_NOT_PERFORMED)]
98+
public virtual void CheckThatFlushingPreventedWithFalseFlushResourcesContentStreams() {
99+
// Expected log message that page flushing was not performed
100+
String outPdf = destinationFolder + "checkThatFlushingPreventedWithFalseFlushResourcesContentStreams.pdf";
101+
PdfWriter writer = new PdfWriter(outPdf);
102+
PdfADocument pdfDoc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom"
103+
, "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(sourceFolder + "sRGB Color Space Profile.icm"
104+
, FileMode.Open, FileAccess.Read)));
105+
pdfDoc.SetTagged();
106+
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
107+
PdfAPageTest.EndPageEventHandler eventHandler = new PdfAPageTest.EndPageEventHandler();
108+
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
109+
int pageCount = 3;
110+
for (int i = 0; i < pageCount; i++) {
111+
pdfDoc.AddNewPage().Flush(false);
112+
}
113+
NUnit.Framework.Assert.AreEqual(pageCount, pdfDoc.GetNumberOfPages());
114+
NUnit.Framework.Assert.AreEqual(0, eventHandler.GetCounter());
115+
pdfDoc.Close();
116+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
117+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
118+
}
119+
120+
[NUnit.Framework.Test]
121+
public virtual void CheckFlushingWhenPdfDocumentIsClosing() {
122+
String outPdf = destinationFolder + "checkFlushingWhenPdfDocumentIsClosing.pdf";
123+
PdfWriter writer = new PdfWriter(outPdf);
124+
PdfADocument pdfDoc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom"
125+
, "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(sourceFolder + "sRGB Color Space Profile.icm"
126+
, FileMode.Open, FileAccess.Read)));
127+
pdfDoc.SetTagged();
128+
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
129+
PdfAPageTest.EndPageEventHandler eventHandler = new PdfAPageTest.EndPageEventHandler();
130+
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
131+
int pageCount = 3;
132+
for (int i = 0; i < pageCount; i++) {
133+
pdfDoc.AddNewPage();
134+
}
135+
NUnit.Framework.Assert.AreEqual(pageCount, pdfDoc.GetNumberOfPages());
136+
NUnit.Framework.Assert.AreEqual(0, eventHandler.GetCounter());
137+
pdfDoc.Close();
138+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
139+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
140+
}
141+
142+
[NUnit.Framework.Test]
143+
public virtual void CheckFlushingWithTrueFlushResourcesContentStreams() {
144+
String outPdf = destinationFolder + "checkFlushingWithTrueFlushResourcesContentStreams.pdf";
145+
PdfWriter writer = new PdfWriter(outPdf);
146+
PdfADocument pdfDoc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom"
147+
, "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(sourceFolder + "sRGB Color Space Profile.icm"
148+
, FileMode.Open, FileAccess.Read)));
149+
pdfDoc.SetTagged();
150+
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
151+
PdfAPageTest.EndPageEventHandler eventHandler = new PdfAPageTest.EndPageEventHandler();
152+
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
153+
int pageCount = 3;
154+
for (int i = 0; i < pageCount; i++) {
155+
pdfDoc.AddNewPage().Flush(true);
156+
}
157+
NUnit.Framework.Assert.AreEqual(pageCount, pdfDoc.GetNumberOfPages());
158+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
159+
pdfDoc.Close();
160+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
161+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
162+
}
163+
164+
[NUnit.Framework.Test]
165+
public virtual void CheckFlushingOfCheckedPage() {
166+
String outPdf = destinationFolder + "checkFlushingOfCheckedPage.pdf";
167+
PdfWriter writer = new PdfWriter(outPdf);
168+
PdfADocument pdfDoc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom"
169+
, "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(sourceFolder + "sRGB Color Space Profile.icm"
170+
, FileMode.Open, FileAccess.Read)));
171+
pdfDoc.SetTagged();
172+
pdfDoc.GetCatalog().SetLang(new PdfString("en-US"));
173+
PdfAPageTest.EndPageEventHandler eventHandler = new PdfAPageTest.EndPageEventHandler();
174+
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
175+
int pageCount = 3;
176+
for (int i = 0; i < pageCount; i++) {
177+
PdfPage page = pdfDoc.AddNewPage();
178+
pdfDoc.checker.CheckSinglePage(page);
179+
page.Flush(false);
180+
}
181+
NUnit.Framework.Assert.AreEqual(pageCount, pdfDoc.GetNumberOfPages());
182+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
183+
pdfDoc.Close();
184+
NUnit.Framework.Assert.AreEqual(pageCount, eventHandler.GetCounter());
185+
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf));
186+
}
187+
188+
internal class EndPageEventHandler : IEventHandler {
189+
private int counter = 0;
190+
191+
internal EndPageEventHandler() {
192+
}
193+
194+
public virtual int GetCounter() {
195+
return counter;
196+
}
197+
198+
public virtual void HandleEvent(Event @event) {
199+
counter++;
200+
}
201+
}
202+
}
203+
}
Binary file not shown.

0 commit comments

Comments
 (0)