Output PDF and TIFF with Java 8

Overview

Use Apache PDFBox for PDF output. It seems that TIFF output is supported as standard in Java 9, but Java8 is not supported, so use JAI (Java Advanced Imaging API).

Preparation

Download the library to use. Apache PDFBox Download the required files from here Apache PDFBox ・ Pdfbox-2.0.16.jar ・ Fontbox-2.0.16.jar ・ Pdfbox-tools-2.0.16.jar (The above version is the latest at the time of writing)

With Maven

pom.xml


<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.16</version>
</dependency>

JAI I wanted only the jar file, so I downloaded it from java2s.com. ・ Jai_imageio-1.1.jar

Implementation

PDF output

Japanese fonts can also be handled by using fontbox.

PDFSample.java


/**
Exception handling is appropriate
**/

PDDocument doc = new PDDocument();
PDPage page = new PDPage();
TrueTypeCollection ttc = new TrueTypeCollection(new File("c:/windows/fonts/msgothic.ttc"));
PDFont font = PDType0Font.load(doc, ttc.getFontByName("MS-Gothic"), true);
doc.addPage(page);
PDPageContentStream cs = new PDPageContentStream(doc, page);
cs.beginText();
cs.setFont(font, 12);
cs.newLineAtOffset(10f, 10f);
cs.showText("Hello PDFBox!");
cs.endText();
cs.close();
doc.close();

TIFF output

TIFFSample.java


/**
Exception handling is appropriate
**/
PDDocument doc = new PDDocument()

/**
Omitted: PDF creation process
**/

PDFRenderer pdfRenderer = new PDFRenderer(doc);

for (int p = 0; p < doc.getNumberOfPages(); ++p) {
    BufferedImage bi = pdfRenderer.renderImageWithDPI(p, 300, ImageType.RGB);
    ImageIOUtil.writeImage(bi, "TIF", this.outputStream, 300);
}
doc.close();

Recommended Posts

Output PDF and TIFF with Java 8
Java encryption and decryption PDF
Paging PDF with Java + PDFBox.jar
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Use JDBC with Java and Scala.
Encrypt with Java and decrypt with C #
Output vector graphics images (PDF, SVG, PPT, EPS, SWF) with Java Graphics 2D and various libraries
Link Java and C ++ code with SWIG
Let's try WebSocket with Java and javascript!
[Java] How to output and write files!
Program PDF headers and footers in Java
[Java] output, variables
Java and JavaScript
XXE and Java
Let's write Java file input / output with NIO
Just input and output images with Spring MVC
Create barcodes and QR codes in Java PDF
How to output Excel and PDF using Excella
Read a string in a PDF file with Java
[Java] How to get and output standard input
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
Build Java environment and output hello world [Beginner]
Text extraction in Java from PDF with pdfbox-2.0.8
Get detailed statistics and edit output with Locust
Prepare a scraping environment with Docker and Java
KMS) Envelope encryption with openssl and java decryption
Encrypt / decrypt with AES256 in PHP and Java
Compare PDF output in Java for snapshot testing
Java enables extraction of PDF text and images
[Java] Convert and import file values with OpenCSV
[Review] Reading and writing files with java (JDK6)
About Java log output
[Java] Align characters even with mixed half-width and full-width characters
Install java with Homebrew
Use fast Mapping library MapStruct with Lombok and Java 11
[Java] Convert PDF version
Java formatted output [Notes]
Getters and setters (Java)
I tried to read and output CSV with Outsystems
Change seats with java
Solving with Ruby and Java AtCoder ABC129 D 2D array
[Java] Thread and Runnable
Summary of ToString behavior with Java and Groovy annotations
Java compressed PDF document
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Java true and false
[Java] String comparison and && and ||
Export pdf with a single program (Java / Perl / VBA)
Comfortable download with JAVA
[Java] Refer to and set private variables with reflection
Switch java with direnv
[Java] PDF viewing settings
Java --Serialization and Deserialization
I tried OCR processing a PDF file with Java
[Java] Arguments and parameters
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
Download Java with Ansible