Self-summary, what are the shortcomings please tell, thank you! The next summary of the PDF template map generates a report (responding to changing PDF reporting requirements, data availability, and report generation decoupling).
Purpose: To generate a report on a given PDF template, you need to know the most basic actions: Add text, add images.
Cause: In PDF generation, the most common operation is to generate text, generate pictures, so this time and everyone to learn about the text and image generation.
Preparatory work:
1. Introduce the jar: introduce the Itextpdf package statement in Pom.xml, choose a version of the link randomly (my is 5.20): Http://www.mvnrepository.com/artifact/com.itextpdf/itextpdf
2. Create a blank PDF file.
3. Prepare a picture.
4. Need to understand the concept: in the PDF page coordinates origin (0,0) in the lower left corner
Code:test class
Packagecom.core.pdf;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportCom.itextpdf.text.BaseColor;Importcom.itextpdf.text.DocumentException;ImportCom.itextpdf.text.Image;ImportCom.itextpdf.text.pdf.BaseFont;ImportCom.itextpdf.text.pdf.PdfContentByte;ImportCom.itextpdf.text.pdf.PdfReader;ImportCom.itextpdf.text.pdf.PdfStamper; Public classTest { Public Static voidMain (string[] args)throwsdocumentexception, IOException {basefont Basefont= Basefont.createfont ("Stsong-light", "Unigb-ucs2-h", basefont.not_embedded); InputStream input=NewFileInputStream (NewFile ("E:/pdf_test/test.pdf")); Pdfreader Reader=NewPdfreader (input); OutputStream Output=NewFileOutputStream (NewFile ("E:/pdf_test/test1.pdf")); Pdfstamper Stamper=Newpdfstamper (reader, output); Pdfcontentbyte page= Stamper.getovercontent (1); //paste text into a PDFPage.begintext (); Page.setfontandsize (Basefont,12); Basecolor Coler=NewBasecolor (0, 0, 0); Page.setcolorfill (Coler); Page.settextmatrix (100,500);//set the coordinates of text in a pagePage.showtext ("Add text message"); Page.endtext (); //paste a picture into a PDFImage image = Image.getinstance ("E:/pdf_test/8.png"); Image.setabsoluteposition (100,200);//set the coordinates of a picture in a pagepage.addimage (image); Stamper.close (); Reader.close (); Input.close (); }}
All right, this concludes the summary.
Java generates text and pictures in existing PDF files-the Basics