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).
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
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();
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