1
+ /*
2
+ This file is part of the iText (R) project.
3
+ Copyright (c) 1998-2019 iText Group NV
4
+ Authors: iText Software.
5
+
6
+ This program is offered under a commercial and under the AGPL license.
7
+ For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8
+
9
+ AGPL licensing:
10
+ This program is free software: you can redistribute it and/or modify
11
+ it under the terms of the GNU Affero General Public License as published by
12
+ the Free Software Foundation, either version 3 of the License, or
13
+ (at your option) any later version.
14
+
15
+ This program is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ GNU Affero General Public License for more details.
19
+
20
+ You should have received a copy of the GNU Affero General Public License
21
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
22
+ */
23
+ package com .itextpdf .kernel .pdf .canvas ;
24
+
25
+ import com .itextpdf .io .LogMessageConstant ;
26
+ import com .itextpdf .io .image .ImageDataFactory ;
27
+ import com .itextpdf .io .source .ByteArrayOutputStream ;
28
+ import com .itextpdf .io .util .StreamUtil ;
29
+ import com .itextpdf .io .util .UrlUtil ;
30
+ import com .itextpdf .kernel .pdf .CompressionConstants ;
31
+ import com .itextpdf .kernel .pdf .PdfDocument ;
32
+ import com .itextpdf .kernel .pdf .PdfPage ;
33
+ import com .itextpdf .kernel .pdf .PdfVersion ;
34
+ import com .itextpdf .kernel .pdf .PdfWriter ;
35
+ import com .itextpdf .kernel .pdf .WriterProperties ;
36
+ import com .itextpdf .kernel .utils .CompareTool ;
37
+ import com .itextpdf .test .ExtendedITextTest ;
38
+ import com .itextpdf .test .annotations .LogMessage ;
39
+ import com .itextpdf .test .annotations .LogMessages ;
40
+ import com .itextpdf .test .annotations .type .IntegrationTest ;
41
+
42
+ import java .io .IOException ;
43
+ import java .io .InputStream ;
44
+ import org .junit .Assert ;
45
+ import org .junit .BeforeClass ;
46
+ import org .junit .Test ;
47
+ import org .junit .experimental .categories .Category ;
48
+
49
+ @ Category (IntegrationTest .class )
50
+ public class PdfCanvasInlineImagesTest extends ExtendedITextTest {
51
+ public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/canvas/PdfCanvasInlineImagesTest/" ;
52
+ public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/canvas/PdfCanvasInlineImagesTest/" ;
53
+
54
+ @ BeforeClass
55
+ public static void beforeClass () {
56
+ createOrClearDestinationFolder (destinationFolder );
57
+ }
58
+
59
+ @ Test
60
+ @ LogMessages (messages = {
61
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_HAS_JBIG2DECODE_FILTER ),
62
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_HAS_JPXDECODE_FILTER ),
63
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_HAS_MASK ),
64
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_SIZE_CANNOT_BE_MORE_4KB )
65
+ })
66
+ public void inlineImagesTest01 () throws IOException , InterruptedException {
67
+ String filename = "inlineImages01.pdf" ;
68
+ PdfDocument document = new PdfDocument (new PdfWriter (destinationFolder + filename ));
69
+
70
+ PdfPage page = document .addNewPage ();
71
+ PdfCanvas canvas = new PdfCanvas (page );
72
+
73
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "Desert.jpg" ), 36 , 700 , 100 , true );
74
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "bulb.gif" ), 36 , 600 , 100 , true );
75
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "smpl.bmp" ), 36 , 500 , 100 , true );
76
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "itext.png" ), 36 , 460 , 100 , true );
77
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "0047478.jpg" ), 36 , 300 , 100 , true );
78
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "map.jp2" ), 36 , 200 , 100 , true );
79
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "amb.jb2" ), 36 , 30 , 100 , true );
80
+
81
+ document .close ();
82
+
83
+ Assert .assertNull (new CompareTool ()
84
+ .compareByContent (destinationFolder + filename , sourceFolder + "cmp_" + filename , destinationFolder ));
85
+ }
86
+
87
+ @ Test
88
+ @ LogMessages (messages = {
89
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_HAS_JBIG2DECODE_FILTER ),
90
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_HAS_JPXDECODE_FILTER ),
91
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_HAS_MASK ),
92
+ @ LogMessage (messageTemplate = LogMessageConstant .IMAGE_SIZE_CANNOT_BE_MORE_4KB )
93
+ })
94
+ public void inlineImagesTest02 () throws IOException , InterruptedException {
95
+ String filename = "inlineImages02.pdf" ;
96
+ PdfDocument document = new PdfDocument (new PdfWriter (destinationFolder + filename ));
97
+
98
+ PdfPage page = document .addNewPage ();
99
+ PdfCanvas canvas = new PdfCanvas (page );
100
+
101
+ InputStream stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "Desert.jpg" ));
102
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
103
+ StreamUtil .transferBytes (stream , baos );
104
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 700 , 100 , true );
105
+ stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "bulb.gif" ));
106
+
107
+ baos = new ByteArrayOutputStream ();
108
+ StreamUtil .transferBytes (stream , baos );
109
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 600 , 100 , true );
110
+ stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "smpl.bmp" ));
111
+
112
+ baos = new ByteArrayOutputStream ();
113
+ StreamUtil .transferBytes (stream , baos );
114
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 500 , 100 , true );
115
+ stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "itext.png" ));
116
+
117
+ baos = new ByteArrayOutputStream ();
118
+ StreamUtil .transferBytes (stream , baos );
119
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 460 , 100 , true );
120
+ stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "0047478.jpg" ));
121
+
122
+ baos = new ByteArrayOutputStream ();
123
+ StreamUtil .transferBytes (stream , baos );
124
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 300 , 100 , true );
125
+ stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "map.jp2" ));
126
+
127
+ baos = new ByteArrayOutputStream ();
128
+ StreamUtil .transferBytes (stream , baos );
129
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 200 , 100 , true );
130
+ stream = UrlUtil .openStream (UrlUtil .toURL (sourceFolder + "amb.jb2" ));
131
+
132
+ baos = new ByteArrayOutputStream ();
133
+ StreamUtil .transferBytes (stream , baos );
134
+ canvas .addImage (ImageDataFactory .create (baos .toByteArray ()), 36 , 30 , 100 , true );
135
+
136
+ document .close ();
137
+
138
+ Assert .assertNull (new CompareTool ()
139
+ .compareByContent (destinationFolder + filename , sourceFolder + "cmp_" + filename , destinationFolder ));
140
+ }
141
+
142
+ @ Test
143
+ public void inlineImagesTest03 () throws IOException , InterruptedException {
144
+ String filename = "inlineImages03.pdf" ;
145
+ PdfDocument document = new PdfDocument (new PdfWriter (destinationFolder + filename ,
146
+ new WriterProperties ().setPdfVersion (PdfVersion .PDF_2_0 ))
147
+ .setCompressionLevel (CompressionConstants .NO_COMPRESSION ));
148
+
149
+ PdfPage page = document .addNewPage ();
150
+ PdfCanvas canvas = new PdfCanvas (page );
151
+
152
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "bulb.gif" ), 36 , 600 , 100 , true );
153
+
154
+ document .close ();
155
+
156
+ Assert .assertNull (new CompareTool ()
157
+ .compareByContent (destinationFolder + filename , sourceFolder + "cmp_" + filename , destinationFolder ));
158
+ }
159
+
160
+ @ Test
161
+ //TODO update cmp-files after DEVSIX-3564 will be fixed
162
+ public void inlineImagesPngTest () throws IOException , InterruptedException {
163
+ String filename = "inlineImagePng.pdf" ;
164
+ PdfDocument document = new PdfDocument (new PdfWriter (destinationFolder + filename ));
165
+
166
+ PdfPage page = document .addNewPage ();
167
+ PdfCanvas canvas = new PdfCanvas (page );
168
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "5.png" ), 36 , 700 , 100 , true );
169
+
170
+ document .close ();
171
+
172
+ Assert .assertNull (new CompareTool ()
173
+ .compareByContent (destinationFolder + filename , sourceFolder + "cmp_" + filename , destinationFolder ));
174
+ }
175
+
176
+ @ Test
177
+ //TODO update cmp-files after DEVSIX-3564 will be fixed
178
+ public void inlineImagesPngErrorWhileOpenTest () throws IOException , InterruptedException {
179
+ String filename = "inlineImagePngErrorWhileOpen.pdf" ;
180
+ PdfDocument document = new PdfDocument (new PdfWriter (destinationFolder + filename ));
181
+
182
+ PdfPage page = document .addNewPage ();
183
+ PdfCanvas canvas = new PdfCanvas (page );
184
+ canvas .addImage (ImageDataFactory .create (sourceFolder + "3.png" ), 36 , 700 , 100 , true );
185
+
186
+ document .close ();
187
+
188
+ Assert .assertNull (new CompareTool ()
189
+ .compareByContent (destinationFolder + filename , sourceFolder + "cmp_" + filename , destinationFolder ));
190
+ }
191
+ }
0 commit comments