Generate PDF Full Introduction to How to add content to existing PDFs _java

Source: Internet
Author: User
Tags pdfobject

The project is changing, the demand is changing, the invariable is always the programmer tapping the keyboard ...

After the PDF is generated, it is sometimes necessary to add some other content to the PDF, such as text, pictures ....

After several failed attempts, I finally got the right way to write code.

Summary of this record to facilitate the next status quo, need the jar please go: Build PDF Full Raiders

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 in the original PDF to add content of the core code, the specific process is as follows

• If reader is careful, the code reads the original a.pdf, writes it to the B.pdf, and then operates B.pdf.

• Maybe some reader will say, read a and then write a, which is definitely not going to work, and a has been loaded and cannot be modified at the time of reading.

I do not like this way, because the original PDF information has been stored in the database, including the PDF server path, old name, new name, type ...

• This will result in one more database change operation, because the PDF name needs to be changed, and the ghost knows how the subsequent requirements will change.

• There is an urgent need to add content to the PDF only, the rest of the same, the code 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 flow does the following.

This introduces a pipeline copy file, a copy of a, read a copy, and then write back to the original PDF A, and finally, of course, the need to delete the copy file.

Here, no matter how the subsequent requirements change, to ensure that the other attributes of the PDF unchanged, you can calmly face.

The pipe copy code is as follows:

Pubpc static void Filechannelcopy (file sources, file dest) {
  try {
 FileInputStream inputstream = new Fileinputstre AM (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); Two channels are connected and read from the in channel and then written to the Out channel

 Inputstream.close ();
 Filechannepn.close ();
 Outputstream.close ();
 Filechannelout.close ();
  } catch (Exception e) {
 e.printstacktrace ();
  }
  }

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 picture 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 generated PDF in the existing PDF to add content to the implementation method is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.