Generate PDF full guide: add content on existing PDF, pdf full Guide
The project is changing, the demand is changing, and what remains unchanged is the programmer who clicks the keyboard .....
After a PDF file is generated, you sometimes need to add other content on the PDF file, such as text, images ....
After several failed attempts, I finally got the correct code writing method.
Record the summary here, so that you can continue to renew it again. For the required jar files, go to: generate a PDF full guide.
PdfReader reader = new PdfReader("E:\\A.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("E:\\B.pdf")); PdfContentByte overContent = stamper.getOverContent(1);
The above code is the core code for adding content to the original PDF. The specific process is as follows:
- If you are careful enough, the Code reads the original A.pdf, writes it to B .pdf, and then operates B .pdf.
- Some readers may say that reading A and then writing it to A will definitely not work. At the time of reading, A has been loaded and cannot be modified.
- I don't like this method because the original PDF information is already stored in the database, including the server path, old name, new name, type of the PDF ......
- In this way, there will be an additional Database Change operation, because the PDF name needs to be changed here, and the ghost knows how the subsequent requirements will change.
- Here, we urgently need to add content only in PDF. Everything else remains the same and the code is slightly adjusted.
FileUtil.fileChannelCopy(A.pdf,A + "tmp".pdf)); PdfReader reader = new PdfReader(A + "tmp".pdf); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(A.pdf)); PdfContentByte overContent = stamper.getOverContent(1);
The code process is like this.
- Here we introduce the pipeline copy file, copy A, read the copy, and write it back to the original pdf a. At last, we need to delete the copy file.
- By now, no matter how the demand changes, other attributes of the pdf file remain unchanged, so you can easily face it.
The pipeline copy code is as follows:
Public static void fileChannelCopy (File sources, File dest) {try {FileInputStream inputStream = new FileInputStream (sources); FileOutputStream outputStream = new FileOutputStream (dest); FileChannel fileChannelin = inputStream. getChannel (); // obtain the corresponding file channel FileChannel fileChannelout = outputStream. getChannel (); // obtain the corresponding file channel fileChannelin. transferTo (0, fileChannelin. size (), fileChannelout); // connects two channels, reads data from the in channel, and writes data to the out channel inputStream. close (); fileChannelin. close (); outputStream. close (); fileChannelout. close ();} catch (Exception e) {e. printStackTrace ();}}
The complete code for adding other content to an existing PDF file is as follows:
FileUtil. fileChannelCopy (new File ("E: \ A.pdf"), new File ("E: \ A +" tmp ". pdf "); PdfReader reader = new PdfReader (" E: \ A + "tmp ". pdf "); PdfStamper stamper = new PdfStamper (reader, new FileOutputStream (" E: \ A.pdf "); your contentbyte overContent = stamper. getOverContent (1); // Add the text BaseFont font = BaseFont. createFont ("STSong-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED); overContent. beginText (); overContent. setFontAndSize (font, 10); overContent. setTextMatrix (200,200); overContent. showTextAligned (Element. ALIGN_CENTER, "words to be added", 580,530, 0); overContent. endText (); // Add the image PdfDictionary pdfDictionary = reader. getPageN (1); PdfObject pdfObject = pdfDictionary. get (new queue name ("MediaBox"); PdfArray pdfArray = (PdfArray) pdfObject; Image image = Image. getInstance ("D: \ 1.jpg"); image. setAbsolutePosition (100,100); overContent. addImage (image); // Add a red circle overContent. setRGBColorStroke (0xFF, 0x00, 0x00); overContent. setLineWidth (5f); overContent. ellipse (250,450,350,550); overContent. stroke (); stamper. close ();