Skip to content

Commit b20b866

Browse files
committed
Added sample Image.getInstance() openpdf
1 parent 58e39ae commit b20b866

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

code-samples-itext2/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>code-samples-itext2</artifactId>
6+
7+
<parent>
8+
<groupId>org.fugerit.java</groupId>
9+
<artifactId>code-samples</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<name>Fugerit Code Samples IText 2</name>
14+
<description>My code samples.</description>
15+
16+
<licenses>
17+
<license>
18+
<name>Apache License, Version 2.0</name>
19+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
20+
<distribution>repo</distribution>
21+
</license>
22+
</licenses>
23+
24+
<properties>
25+
<itext-version>2.1.7</itext-version>
26+
</properties>
27+
28+
<scm>
29+
<connection>scm:git:git://github.com/fugerit-org/code-samples.git</connection>
30+
<developerConnection>scm:git:ssh://github.com/fugerit-org/code-samples.git</developerConnection>
31+
<url>https://github.com/fugerit-org/code-samples.git</url>
32+
<tag>HEAD</tag>
33+
</scm>
34+
35+
<dependencies>
36+
37+
<dependency>
38+
<groupId>com.lowagie</groupId>
39+
<artifactId>itext</artifactId>
40+
<version>${itext-version}</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.lowagie</groupId>
45+
<artifactId>itext-rtf</artifactId>
46+
<version>${itext-version}</version>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
<organization>
52+
<url>https://www.fugerit.org</url>
53+
<name>Fugerit</name>
54+
</organization>
55+
56+
<url>https://www.fugerit.org/</url>
57+
58+
</project>

code-samples-openpdf/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>code-samples-openpdf</artifactId>
6+
7+
<parent>
8+
<groupId>org.fugerit.java</groupId>
9+
<artifactId>code-samples</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<name>Fugerit Code Samples OpenPDF</name>
14+
<description>My code samples.</description>
15+
16+
<licenses>
17+
<license>
18+
<name>Apache License, Version 2.0</name>
19+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
20+
<distribution>repo</distribution>
21+
</license>
22+
</licenses>
23+
24+
<properties>
25+
<openpdf-version-version>1.3.36</openpdf-version-version>
26+
<openrtf-version-version>1.2.1</openrtf-version-version>
27+
</properties>
28+
29+
<scm>
30+
<connection>scm:git:git://github.com/fugerit-org/code-samples.git</connection>
31+
<developerConnection>scm:git:ssh://github.com/fugerit-org/code-samples.git</developerConnection>
32+
<url>https://github.com/fugerit-org/code-samples.git</url>
33+
<tag>HEAD</tag>
34+
</scm>
35+
36+
<dependencies>
37+
38+
<dependency>
39+
<groupId>com.github.librepdf</groupId>
40+
<artifactId>openpdf</artifactId>
41+
<version>${openpdf-version-version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>com.github.librepdf</groupId>
46+
<artifactId>openrtf</artifactId>
47+
<version>${openrtf-version-version}</version>
48+
</dependency>
49+
50+
</dependencies>
51+
52+
<organization>
53+
<url>https://www.fugerit.org</url>
54+
<name>Fugerit</name>
55+
</organization>
56+
57+
<url>https://www.fugerit.org/</url>
58+
59+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.fugerit.java.codesamples.openpdf;
2+
3+
import com.lowagie.text.Element;
4+
import com.lowagie.text.Image;
5+
import org.fugerit.java.core.function.SafeFunction;
6+
7+
public class LoadImage {
8+
9+
private LoadImage() {}
10+
11+
public static Image fromBytes( byte[] data ) {
12+
return SafeFunction.get( () -> {
13+
Image image = Image.getInstance( data );
14+
image.scaleAbsoluteHeight(70);
15+
image.scaleAbsoluteWidth(70);
16+
image.setAlignment(Element.ALIGN_CENTER);
17+
return image;
18+
} );
19+
}
20+
21+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package test.org.fugerit.java.codesamples;
2+
3+
import com.lowagie.text.Image;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.fugerit.java.codesamples.openpdf.LoadImage;
6+
import org.fugerit.java.core.io.StreamIO;
7+
import org.fugerit.java.core.lang.helpers.ClassHelper;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import javax.imageio.ImageIO;
12+
import java.awt.image.BufferedImage;
13+
import java.io.InputStream;
14+
15+
@Slf4j
16+
class TestLoadImage {
17+
18+
private static final String[] IMAGES = { "logo1.png", "logo2.png" };
19+
20+
@Test
21+
void testFromBytes() {
22+
for ( String current : IMAGES ) {
23+
String currentTest = "test_image/"+current;
24+
try (InputStream is = ClassHelper.loadFromDefaultClassLoader( currentTest ) ) {
25+
log.info( "currentTest {} - {}", LoadImage.class.getName(), currentTest );
26+
Image img = LoadImage.fromBytes( StreamIO.readBytes( is ) );
27+
Assertions.assertNotNull( img );
28+
} catch (Exception e) {
29+
log.warn( "Errore "+e, e );
30+
}
31+
}
32+
}
33+
34+
@Test
35+
void testImageIO() {
36+
for ( String current : IMAGES ) {
37+
String currentTest = "test_image/"+current;
38+
try (InputStream is = ClassHelper.loadFromDefaultClassLoader( currentTest ) ) {
39+
log.info( "currentTest ImageLoader.getPngImage() {}", currentTest );
40+
BufferedImage bufferedImage = ImageIO.read(is);
41+
Image img = Image.getInstance(bufferedImage, null, false);
42+
Assertions.assertNotNull( img );
43+
} catch (Exception e) {
44+
log.warn( "Errore ImageLoader.getPngImage() "+e, e );
45+
}
46+
}
47+
}
48+
49+
}
Loading
Loading

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<module>code-samples-base</module>
3333
<module>code-samples-pdfbox2</module>
3434
<module>code-samples-fj-doc</module>
35+
<module>code-samples-openpdf</module>
36+
<module>code-samples-itext2</module>
3537
</modules>
3638

3739
<scm>

0 commit comments

Comments
 (0)