Package Com.test;import Com.itextpdf.text.basecolor;import Com.itextpdf.text.font;import com.itextpdf.text.Image; Import Com.itextpdf.text.pdf.*;import org.junit.test;import java.io.fileoutputstream;/** * Edit PDF */public Class pdftest {@Test public void testpdf () {generatepdf ("d:/1.pdf", "D:/2.pdf", "d:1.png"); }/** * Generate a new PDF * @param pdfpath the PDF path to edit * @param newpdfpath generate a new PDF path * @param imagePath Insert Picture Path */public void generatepdf (String pdfpath,string newpdfpath,string imagePath) {try{//create a PDF read stream Pdfreader reader = new Pdfreader (Pdfpath); Create a pdfstamper based on a pdfreader. Used to generate a new PDF. Pdfstamper stamper = new Pdfstamper (reader,new FileOutputStream (Newpdfpath)); This font is self-contained in Itext-asian.jar so do not consider operating system environment issues. Basefont bf = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded); Basefont does not support font style settings. But font fonts require the operating system to support this font, which can lead to porting problems. Font font = new Font (bf,10); Font.setstyle (Font.Bold); Font.getbasefont (); Pdfcontentbyte over; The number of pages is 1-based for (int i=1; i<=reader.getnumberofpages (); i++) {//Get Pdfstamper print content on the top of the current page. Which means Content will be overwritten with the original PDF content. over = Stamper.getovercontent (i); Lower-level print content of the current page select//over = stamper.getundercontent (i) as per your requirement; Gets the current page Dictionary object with Pdfreader. Contains some data for the page. For example, the axis information for this page. Pdfdictionary p = Reader.getpagen (i); Get the size information for the page in the Mediabox. Pdfobject po = p.get (New Pdfname ("Mediabox")); The PO is an array object. It contains the axis range of the page's PDF. Pdfarray pa = (pdfarray) po; Start writing text Over.begintext (); Set the font and size over.setfontandsize (Font.getbasefont (), 90); Sets the font color Over.setcolorfill (new Basecolor (0,110,107,100)); Com.itextpdf.text.pdf.PdfGState gstate = nEW pdfgstate (); Gstate.setstrokeopacity (0.1f); Over.setgstate (gstate); To output the text alignment, write a word to set the font's output position if the font is rotated over.showtextaligned (0, "HELLO World", 0,100,1 00); Over.endtext (); Creates an Image object. Image image = Image.getinstance (ImagePath); Sets the output position of the Image object Pa.getasnumber (Pa.size ()-1). Floatvalue () is the maximum value of the y-axis of the page's PDF axis 0, 0, 841.92, 595.32 Image.setab Soluteposition (0,pa.getasnumber (Pa.size ()-1). Floatvalue ()); Sets the size of the inserted picture Image.scaletofit (50,50); Over.addimage (image); Draw a circle. Over.setrgbcolorstroke (0xFF, 0x00, 0x00); Over.setlinewidth (5f); Over.ellipse (250, 450, 350, 550); Over.stroke (); } stamper.close (); }catch (Exception e) {e.printstacktrace (); } }}
PDF Development Kit: http://pdfbox.apache.org/
Java Edit pdf Write text Insert Picture