The realization method of PDF transfer picture in Java _java

Source: Internet
Author: User

Java in the implementation of PDF transfer pictures can be provided by a third party package, here are several commonly used, can choose to use according to their own needs.

First, Icepdf. There are fee and open source version, the most recommended in several ways. The effect of the conversion is better, I can identify the Chinese in the file, that is, after the conversion may be the font of the relationship between the part of the word spacing a bit wide. Because font support is charged, the converted picture will have an official watermark. The way to watermark can be viewed in another article:icepdf watermark Method

1, download the Icepdf package, and import the project, here to use 4, as follows:

2, enclosed code examples:

String FilePath = "C:/test.pdf";
Document document = new document ();
Document.setfile (FilePath);
Float scale = 2.5f;//scaling
float rotation = 0f;//rotation angle for
    
(int i = 0; i < document.getnumberofpages (); i++) {
BufferedImage image = (bufferedimage)
document.getpageimage (i, Graphicsrenderinghints.screen, org.icepdf    . Core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendimage = image;
try {
  File File = new file ("C:/iecpdf_" + i + ". png");
  Imageio.write (Rendimage, "PNG", file); 
The catch (IOException e) {
  e.printstacktrace ();
}
  Image.flush ();
}
Document.dispose ();

In the example PDF to PNG format, you can also change the 12 or 13 rows to JPG, the JPG format, but from the conversion effect to see PNG clarity will be relatively high. One trick is to change the 12 lines to JPG, but the 13 lines use PNG, which is a picture converted to JPG but with PNG sharpness.

Second, PDFBox. The conversion effect can also, can identify most of the contents of my hands, some of the content is not recognized.

1, download the PDFBox package, and import the project, here to use 2, as follows:

2, enclosed code examples:

File File = new file ("C:\\test.pdf");
try {
PDDocument doc = pddocument.load (file);
Pdfrenderer renderer = new Pdfrenderer (DOC);
 5 int pagecount = Doc.getnumberofpages ();
for (int i=0;i<pagecount;i++) {
BufferedImage image = renderer.renderimagewithdpi (i, 296);     bufferedimage image = Renderer.renderimage (i, 2.5f);
Imageio.write (Image, "PNG", New File ("C:\\pdfbox_image.png"));
}
catch (IOException e) {
e.printstacktrace ();
}

The second parameter of rederimagewithdpi in the example is the DPI resolution unit, which adjusts the size according to the requirement, and the eighth line provides another way to turn the picture in the package, and the second parameter is the scaling ratio.

Third, Jpedal. The effect is not ideal, seemingly support for Chinese is not very good, the following LGPL version is open source version.

1, download the Jpedal package, and import the project, as follows:

2, enclosed code examples:

Pdfdecoder decode_pdf = new Pdfdecoder (true);
try {
decode_pdf.openpdffile ("c:\\test.pdf");//file
//Decode_pdf.openpdffile ("C:/jpedalpdf.pdf", " Password "); Encrypted file
//   Decode_pdf.openpdfarray (bytes);//bytes is byte[] array with PDF
//   Decode_ Pdf.openpdffilefromurl ("Http://www.mysite.com/jpedalPDF.pdf", false);   Decode_pdf.openpdffilefrominputstream (in, false); 

int start = 1, end = Decode_pdf.getpagecount ();
for (int i = start; i < end+1; i++) {
bufferedimage img=decode_pdf.getpageasimage (i);
try {
imageio.write (IMG, PNG, New File ("C:\\jpedal_image.png"));
catch (IOException e) {
e.printstacktrace ();
}
decode_pdf.closepdffile ();
catch (Pdfexception e) {
e.printstacktrace ();
}

The example's 3-7 lines also offer several different ways to open PDFs, which you can choose to use for your own needs.

The above Java PDF to the implementation of the image is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat 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.