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.
pdfbox-2.0.8
Download it here. Official
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();
	    }
	}
}


You now have a one-page file.
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();
	    }
	}
}


Since the output position is contentStream.newLineAtOffset (0f, 0f), it will be output at the bottom left. There are various types of output fonts.
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();
	    }
	}
}


Font files are handled slightly differently between .ttf and .ttc. This time I'm using .ttc.
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.
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();
		}
    }
}
--------- 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