Skip to content

Commit fe26d63

Browse files
committed
Test for pdfbox special characters : U+2002 ('enspace')
1 parent 12f06de commit fe26d63

File tree

9 files changed

+109
-52
lines changed

9 files changed

+109
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Test for pdfbox special characters : U+2002 ('enspace')
13+
- Module pdfbox
1214
- AgeCheck with tests
1315

code-samples-base/src/main/java/org/fugerit/java/code/samples/time/AgeCheck.java renamed to code-samples-base/src/main/java/org/fugerit/java/code/samples/pdfbox2/AgeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.fugerit.java.code.samples.time;
1+
package org.fugerit.java.code.samples.pdfbox2;
22

33
import java.time.LocalDate;
44
import java.time.temporal.ChronoUnit;

code-samples-base/src/test/java/test/org/fugerit/java/code/samples/time/TestAgeCheck.java renamed to code-samples-base/src/test/java/test/org/fugerit/java/code/samples/pdfbox2/TestAgeCheck.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package test.org.fugerit.java.code.samples.time;
1+
package test.org.fugerit.java.code.samples.pdfbox2;
22

33
import lombok.extern.slf4j.Slf4j;
4-
import org.fugerit.java.code.samples.time.AgeCheck;
4+
import org.fugerit.java.code.samples.pdfbox2.AgeCheck;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.fugerit.java.code.samples.pdfbox2;
2+
3+
import org.apache.pdfbox.pdmodel.PDDocument;
4+
import org.apache.pdfbox.pdmodel.PDPage;
5+
import org.apache.pdfbox.pdmodel.PDPageContentStream;
6+
import org.apache.pdfbox.pdmodel.font.PDFont;
7+
import org.apache.pdfbox.pdmodel.font.PDType0Font;
8+
9+
import java.io.*;
10+
11+
/**
12+
* Handling of special characters
13+
*
14+
* In this example we have : U+2002 ('enspace')
15+
*
16+
* See the Unit test for usage
17+
*/
18+
public class SpecialCharacters {
19+
20+
private void generatePDFWorker(OutputStream os, PDDocument document, PDFont font, String line) throws IOException {
21+
PDPage currentPage = new PDPage();
22+
PDPageContentStream cs = new PDPageContentStream( document, currentPage );
23+
cs.setFont( font, 14 );
24+
cs.setLeading(12);
25+
cs.beginText();
26+
cs.newLineAtOffset(25, 550);
27+
cs.showText( line );
28+
cs.endText();
29+
cs.stroke();
30+
cs.close();
31+
document.addPage( currentPage );
32+
document.save( os );
33+
}
34+
35+
public void generatePDF(OutputStream os, InputStream fontIS, String line) throws IOException {
36+
try (PDDocument document = new PDDocument()) {
37+
PDType0Font font = PDType0Font.load( document, fontIS );
38+
this.generatePDFWorker( os, document, font, line );
39+
}
40+
}
41+
42+
public void generatePDF(OutputStream os, PDFont font, String line) throws IOException {
43+
try (PDDocument document = new PDDocument()) {
44+
this.generatePDFWorker( os, document, font, line );
45+
}
46+
}
47+
48+
}

code-samples-pdfbox2/src/main/java/org/fugerit/java/code/samples/time/AgeCheck.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package test.org.fugerit.java.code.samples.pdfbox2;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.pdfbox.pdmodel.font.PDType1Font;
5+
import org.fugerit.java.code.samples.pdfbox2.SpecialCharacters;
6+
import org.fugerit.java.core.lang.helpers.ClassHelper;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.File;
11+
import java.io.FileOutputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
15+
/*
16+
* Proof of Concept to handle special characters like U+2002 ('enspace')
17+
*
18+
* PdfBox bundled font, PDType1Font.HELVETICA will cause an exception :
19+
* java.lang.IllegalArgumentException: U+2002 ('enspace') is not available in the font Helvetica, encoding: WinAnsiEncoding
20+
* (see method testWithPdfBoxFontHelvetica)
21+
*
22+
* Whereas some externally loaded font with the PDType0Font API will not.
23+
* (see method testWithExternalHelvetica)
24+
*
25+
*/
26+
@Slf4j
27+
class TestSpecialCharacters {
28+
29+
private static final String TEST_LINE = "Row with special character :  ."; // test line with U+2002 ('enspace')
30+
31+
@Test
32+
void testWithPdfBoxFontHelvetica() throws IOException {
33+
try (FileOutputStream fos = new FileOutputStream( "target/test_font_from_pdfbox.pdf" )) {
34+
SpecialCharacters sp = new SpecialCharacters();
35+
Assertions.assertThrows( IllegalArgumentException.class, () -> {
36+
sp.generatePDF( fos, PDType1Font.HELVETICA, TEST_LINE );
37+
});
38+
}
39+
40+
}
41+
42+
@Test
43+
void testWithExternalHelvetica() throws IOException {
44+
File pdfFile = new File( "target/test_font_external.pdf" );
45+
log.info( "pdfFile: {}, delete: {}", pdfFile, pdfFile.delete() );
46+
Assertions.assertFalse( pdfFile.exists() );
47+
try ( InputStream fontIS = ClassHelper.loadFromClassLoader( "font/Helvetica.ttf" );
48+
FileOutputStream fos = new FileOutputStream( pdfFile ) ) {
49+
SpecialCharacters sp = new SpecialCharacters();
50+
sp.generatePDF( fos, fontIS, TEST_LINE );
51+
}
52+
Assertions.assertTrue( pdfFile.exists() );
53+
}
54+
55+
}

code-samples-pdfbox2/src/test/java/test/org/fugerit/java/code/samples/time/TestAgeCheck.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
311 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<relativePath/>
1212
</parent>
1313

14-
<name>Fugerit Code Samples</name>
14+
<name>Fugerit Code Samples PDFBOX 2</name>
1515
<description>My code samples.</description>
1616
<version>1.0.0-SNAPSHOT</version>
1717
<packaging>pom</packaging>

0 commit comments

Comments
 (0)