I created a PDF in Java.

Introduction

Form output has been seen in every system, but recently it has become paperless, so I feel that the number of paper output itself is decreasing. However, form-like business practices are still necessary. Let's easily create PDF data.

Library

pdfbox-2.0.8

Download it here. Official

First try to create a file

package pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class PdfTest {

    public static void main(String args[]) {
	    try {
	    	
	        //Creating a document object
	        PDDocument document = new PDDocument();

	        //Creating a page object
	        PDPage page = new PDPage();
	        document.addPage(page);

	        //Save the document
	        document.save("test.pdf");
	        document.close();
	    }
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}

Execution result

image.png

image.png

You now have a one-page file.

Output characters

Next is the output of characters.

package pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class PdfTest {

    public static void main(String args[]) {
	    try {
	    	
	        //Creating a document object
	        PDDocument document = new PDDocument();

	        //Creating a page object
	        PDPage page = new PDPage();
	        document.addPage(page);

	        //Character output processing
	        PDPageContentStream contentStream = new PDPageContentStream(document, page);
	        contentStream.beginText();
	        //Font specification
	        PDFont font = PDType1Font.TIMES_ITALIC;
	        contentStream.setFont(font, 12);
	        //Output position specification
	        contentStream.newLineAtOffset(0f, 0f);
	        //Output string
	        contentStream.showText( "HelloWorld" );
	        contentStream.endText();
	        contentStream.close();
	        
	        //Save the document
	        document.save("test2.pdf");
	        document.close();
	    }
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}

Execution result

image.png

image.png

Since the output position is contentStream.newLineAtOffset (0f, 0f), it will be output at the bottom left. There are various types of output fonts.

Output Japanese

package pdf;

import java.io.File;

import org.apache.fontbox.ttf.TrueTypeCollection;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

public class PdfTest {

    public static void main(String args[]) {
	    try {
	    	
	        //Creating a document object
	        PDDocument document = new PDDocument();

	        //Creating a page object
	        PDPage page = new PDPage();
	        document.addPage(page);

	        //Character output processing
	        PDPageContentStream contentStream = new PDPageContentStream(document, page);
	        contentStream.beginText();
	        //Font specification
	        File file = new File("C:/Windows/Fonts/msmincho.ttc");
	        TrueTypeCollection collection = new TrueTypeCollection(file);
	        PDFont font = PDType0Font.load(document, collection.getFontByName("MS-Mincho"), true);

	        contentStream.setFont(font, 12);
	        //Output position specification
	        contentStream.newLineAtOffset(0f, 755f);
	        //Output string
	        contentStream.showText( "Hello World" );
	        contentStream.endText();
	        contentStream.close();
	        
	        //Save the document
	        document.save("test3.pdf");
	        document.close();
	    }
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}

Execution result

image.png

image.png

Font files are handled slightly differently between .ttf and .ttc. This time I'm using .ttc.

Keep only PDF data without creating a file

Depending on the business case, there may be a requirement that you want to save only the data without creating the actual file. This can be achieved without any problems.

package pdf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.fontbox.ttf.TrueTypeCollection;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

public class PdfTest {

    public static void main(String args[]) {
	    try {
	    	
	        //Creating a document object
	        PDDocument document = new PDDocument();

	        //Creating a page object
	        PDPage page = new PDPage();
	        document.addPage(page);

	        //Character output processing
	        PDPageContentStream contentStream = new PDPageContentStream(document, page);
	        contentStream.beginText();
	        //Font specification
	        File file = new File("C:/Windows/Fonts/msmincho.ttc");
	        TrueTypeCollection collection = new TrueTypeCollection(file);
	        PDFont font = PDType0Font.load(document, collection.getFontByName("MS-Mincho"), true);

	        contentStream.setFont(font, 12);
	        //Output position specification
	        contentStream.newLineAtOffset(0f, 755f);
	        //Output string
	        contentStream.showText( "Hello World. Save only data." );
	        contentStream.endText();
	        contentStream.close();

	        //Save document data
	        ByteArrayOutputStream out = new ByteArrayOutputStream();
	        document.save(out);
	        document.close();
	        
	        InputStream streamData =new ByteArrayInputStream(out.toByteArray());
	        
	        System.out.println("Verification------------");
	        System.out.println("streamData" + streamData);
	    }
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}

You can save it in a blob type column as it is.

Extract the character string from the created PDF.

package pdf;

import java.io.File;
import java.io.StringWriter;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class GetPdfText {

    public static void main(String args[]) {
	    try {
			
	    	String pdfFile = "test3.pdf";
	    	PDDocument document = PDDocument.load(new File(pdfFile));
	    	StringWriter output = new StringWriter();
	        PDFTextStripper stripper = new PDFTextStripper();
	    	stripper.setStartPage(1);
	    	stripper.setEndPage(1);
	    	stripper.setSortByPosition(false);
	    	stripper.setShouldSeparateByBeads(true);
	    	stripper.writeText(document, output);
	    	String content = output.toString();
	    	System.out.println("---------Output start-------------");
	    	System.out.println(content);

		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}

Execution result

--------- Start output -------------- Hello World

There are various other functions such as image embedding and ruled lines, so I will take the opportunity to add them.

Recommended Posts

I created a PDF in Java.
I made a primality test program in Java
Read a string in a PDF file with Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
I made a rock-paper-scissors game in Java (CLI)
I wrote a prime factorization program in Java
Save Java PDF in Excel
I made roulette in Java.
Find a subset in Java
I tried metaprogramming in Java
I made a simple calculation problem game in Java
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried OCR processing a PDF file with Java
What I learned when building a server in Java
I sent an email in Java
3 Implement a simple interpreter in Java
I made a shopify app @java
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
I wrote Goldbach's theorem in java
A simple sample callback in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I made an annotation in Java.
I tried using JWT in Java
Get stuck in a Java primer
I tried to make a client of RESAS-API in Java
I tried OCR processing a PDF file with Java part2
Java draws shapes in PDF documents
I can't create a Java class with a specific name in IntelliJ
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
How to automatically operate a screen created in Java on Windows
I wrote a Stalin sort that feels like a mess in Java
[Note] What I learned in half a year from inexperienced (Java) (3)
About returning a reference in a Java Getter
[Java] I participated in ABC-188 of Atcorder.
What is a class in Java language (3 /?)
I tried using Elasticsearch API in Java
I tried a calendar problem in Ruby
When seeking multiple in a Java array
[Creating] A memorandum about coding in Java
I tried the new era in Java
Java creates a table in a Word document
Java creates a pie chart in Excel
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
I did OpenCV camera calibration in Java
Implement something like a stack in Java
Split a string with ". (Dot)" in Java
Creating a matrix class in Java Part 1
I made a new Java deployment tool
Program PDF headers and footers in Java
[* Java *] I participated in JJUG CCC 2019 Spring
Java application development environment created in VM environment
I tried embedding a formula in Javadoc
I created a Selenium sample app that supports multiple browsers (Chrome, IE, Firefox) that runs in Java.