Projects are changing, requirements are changing, and the unchanging programmers are always typing on keyboards...
After the PDF is generated, sometimes you need to add some other content to the PDF, such as text, pictures...
After several failed attempts, I finally got the correct way to write the code.
This record and summarize, so that the next time you will be unchanged and adapt to changes, please move 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 considered as the core code that adds content to the original PDF. The specific process is as follows
•If the reader is careful enough, the code is to read the original A.pdf, then write it to B.pdf, and then operate B.pdf.
•Some readers may say that reading A and then writing it into A is definitely not possible. A has been loaded and cannot be modified when reading.
•I don't like this because the information of the original PDF is already stored in the database, including the PDF's server path, old name, new name, type...
•This will cause an extra database change operation, because the PDF name needs to be changed here, and people know how the subsequent requirements will change.
• It is urgent to add content to the PDF here, and nothing else changes, so I slightly adjusted the code.
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 flow becomes like this
Here we introduce the pipeline copy file, copy A, read the copy, and then write back to the original PDF A, and of course, you need to delete the copy file.
At this point, no matter how the subsequent requirements change, the other properties of pdf remain unchanged, and you can face them calmly.
The pipeline copy code is as follows:
pubpc static void fileChannelCopy(File sources, File dest) { try { FileInputStream inputStream = new FileInputStream(sources); FileOutputStream outputStream = new FileOutputStream(dest); FileChannel fileChannepn = inputStream.getChannel();//Get the corresponding file channel FileChannel fileChannelout = outputStream.getChannel();//Get the corresponding file channel fileChannepn.transferTo(0, fileChannepn.size(), fileChannelout);//Connect two channels, read from the in channel, and then write to the out channel inputStream.close(); fileChannepn.close(); outputStream.close(); fileChannelout.close(); } catch (Exception e) { e.printStackTrace(); } }The complete PDF other content code 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")); PdfContentByte overContent = stamper.getOverContent(1); //Add text BaseFont font = BaseFont.createFont("STSong-pght", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); overContent.beginText(); overContent.setFontAndSize(font, 10); overContent.setTextMatrix(200, 200); overContent.showTextApgned(Element.ApGN_CENTER,"Text to be added",580,530,0); overContent.endText(); //Add image PdfDictionary pdfDictionary = reader.getPageN(1); PdfObject pdfObject = pdfDictionary.get(new PdfName("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.setpneWidth(5f); overContent.elppse(250, 450, 350, 550); overContent.stroke(); stamper.close();The above full guide to generate PDFs is the implementation method of adding content to existing PDFs. The editor shares all the content with you. I hope you can give you a reference and I hope you can support Wulin.com more.