|
| 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 | +} |
0 commit comments