Java generates text and pictures in existing PDF files-the Basics

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.