Skip to content

Commit d045c19

Browse files
committed
Add test. DEVSIX-2007
1 parent d01bc5d commit d045c19

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfStampingTest.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.io.source.ByteUtils;
4646
import com.itextpdf.io.util.DateTimeUtil;
47+
import com.itextpdf.kernel.events.Event;
48+
import com.itextpdf.kernel.events.IEventHandler;
49+
import com.itextpdf.kernel.events.PdfDocumentEvent;
50+
import com.itextpdf.kernel.font.PdfFontFactory;
51+
import com.itextpdf.kernel.geom.Rectangle;
52+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
53+
import com.itextpdf.kernel.utils.CompareTool;
4754
import com.itextpdf.kernel.xmp.XMPException;
4855
import com.itextpdf.kernel.xmp.XMPMetaFactory;
4956
import com.itextpdf.test.ExtendedITextTest;
5057
import com.itextpdf.test.annotations.type.IntegrationTest;
58+
import org.junit.Assert;
5159
import org.junit.BeforeClass;
5260
import org.junit.Ignore;
5361
import org.junit.Test;
@@ -673,7 +681,7 @@ public void stampingStreamsCompression01() throws IOException {
673681

674682
assertEquals(5731884, lengthBefore);
675683
float expected = 5731884;
676-
float coef = Math.abs((expected - lengthAfter)/expected);
684+
float coef = Math.abs((expected - lengthAfter) / expected);
677685
assertTrue(coef < 0.01);
678686
}
679687

@@ -698,7 +706,7 @@ public void stampingStreamsCompression02() throws IOException {
698706

699707
assertEquals(5731884, lengthBefore);
700708
float expected = 11321910;
701-
float coef = Math.abs((expected - lengthAfter)/expected);
709+
float coef = Math.abs((expected - lengthAfter) / expected);
702710
assertTrue(coef < 0.01);
703711
}
704712

@@ -721,7 +729,7 @@ public void stampingStreamsCompression03() throws IOException {
721729

722730
assertEquals(5731884, lengthBefore);
723731
float expected = 5729270;
724-
float coef = Math.abs((expected - lengthAfter)/expected);
732+
float coef = Math.abs((expected - lengthAfter) / expected);
725733
assertTrue(coef < 0.01);
726734
}
727735

@@ -796,7 +804,7 @@ public void stampingXmp2() throws IOException, XMPException {
796804
for (int i = 0; i < pdfDoc3.getNumberOfPages(); i++) {
797805
pdfDoc3.getPage(i + 1);
798806
}
799-
assertNotNull("XmpMetadata not found", XMPMetaFactory.parseFromBuffer(pdfDoc3.getXmpMetadata()));
807+
assertNotNull("XmpMetadata not found", XMPMetaFactory.parseFromBuffer(pdfDoc3.getXmpMetadata()));
800808
assertEquals("Number of pages", pageCount, pdfDoc3.getNumberOfPages());
801809
assertEquals("Rebuilt", false, reader3.hasRebuiltXref());
802810
assertEquals("Fixed", false, reader3.hasFixedXref());
@@ -1250,19 +1258,19 @@ public void stampingTestWithFullCompression01() throws IOException, InterruptedE
12501258
pdfDoc.close();
12511259
float result = new File(destinationFolder + "stampingTestWithFullCompression01.pdf").length();
12521260
float expected = new File(sourceFolder + "cmp_stampingTestWithFullCompression01.pdf").length();
1253-
float coef = Math.abs((expected - result)/expected);
1261+
float coef = Math.abs((expected - result) / expected);
12541262
assertTrue(coef < 0.01);
12551263
}
12561264

12571265
@Test
12581266
public void stampingTestWithFullCompression02() throws IOException, InterruptedException {
12591267
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "fullCompressedDocument.pdf"),
12601268
new PdfWriter(destinationFolder + "stampingTestWithFullCompression02.pdf",
1261-
new WriterProperties().setFullCompressionMode(false)));
1269+
new WriterProperties().setFullCompressionMode(false)));
12621270
pdfDoc.close();
12631271
float result = new File(destinationFolder + "stampingTestWithFullCompression02.pdf").length();
12641272
float expected = new File(sourceFolder + "cmp_stampingTestWithFullCompression02.pdf").length();
1265-
float coef = Math.abs((expected - result)/expected);
1273+
float coef = Math.abs((expected - result) / expected);
12661274
assertTrue(coef < 0.01);
12671275
}
12681276

@@ -1284,4 +1292,42 @@ static void verifyPdfPagesCount(PdfObject root) {
12841292
verifyPdfPagesCount(kids);
12851293
}
12861294
}
1295+
1296+
@Test
1297+
//TODO: DEVSIX-2007
1298+
public void stampingStreamNoEndingWhitespace01() throws IOException, InterruptedException {
1299+
System.out.println(destinationFolder);
1300+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "stampingStreamNoEndingWhitespace01.pdf"),
1301+
new PdfWriter(destinationFolder + "stampingStreamNoEndingWhitespace01.pdf",
1302+
new WriterProperties().setCompressionLevel(0)));
1303+
1304+
PdfDocument pdfDocInput = new PdfDocument(new PdfReader(sourceFolder + "stampingStreamNoEndingWhitespace01.pdf"));
1305+
PdfDocument pdfDocOutput = new PdfDocument(new PdfWriter(destinationFolder + "stampingStreamNoEndingWhitespace01.pdf", new WriterProperties().setCompressionLevel(0)));
1306+
1307+
class WatermarkEventHandler implements IEventHandler {
1308+
1309+
@Override
1310+
public void handleEvent(Event event) {
1311+
PdfDocumentEvent pdfEvent = (PdfDocumentEvent) event;
1312+
PdfPage page = pdfEvent.getPage();
1313+
PdfCanvas pdfCanvas = new PdfCanvas(page);
1314+
try {
1315+
pdfCanvas.beginText()
1316+
.setFontAndSize(PdfFontFactory.createFont(), 12.0f)
1317+
.showText("Text")
1318+
.endText();
1319+
} catch (IOException e) {
1320+
}
1321+
}
1322+
}
1323+
;
1324+
pdfDocOutput.addEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkEventHandler());
1325+
1326+
pdfDocInput.copyPagesTo(1, pdfDocInput.getNumberOfPages(), pdfDocOutput);
1327+
1328+
pdfDocInput.close();
1329+
pdfDocOutput.close();
1330+
1331+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "stampingStreamNoEndingWhitespace01.pdf", sourceFolder + "cmp_stampingStreamNoEndingWhitespace01.pdf", destinationFolder, "diff_"));
1332+
}
12871333
}

0 commit comments

Comments
 (0)