Java image processing and PDF transfer pictures __ Images processing

Source: Internet
Author: User
Java image processing and PDF transfer pictures 1. Demand

before the project used to display a PDF module, the need to display the PDF processing, but also combined a number of plug-ins, Pdf.js is a Firefox browser launched a set of H5 rendering PDF front-end Plug-ins, supporting the mobile end of the PC, but the display effect is not very good, Sometimes need to nest into the mobile webview inside, the display will have a problem, the PC side, because the direct use of IFRAME support, but individual browsers still do not support, so in order to solve the compatibility of one-time, all the PDF into a picture on the line, so there will be no compatibility issues
But in the actual operation, found that there are still some problems, some PDF fonts, Linux Server font Library does not, when the use of the Icetext class library, found that when the PDF font Icetext unrecognized will appear garbled, and generate pictures, is based on a PDF page of a picture generated, the display is not very friendly. need for further clarity 1. A PDF file corresponds to a picture 2. Cannot appear garbled

Finally found Apache, PDFBox support to transfer out of the picture effect is better, but also not garbled, but pdf corresponding to multiple pictures
Demand 2 solved, how to solve a PDF file corresponding to a picture of the problem of Nepal?
If you can merge multiple images into one image in memory, then how do you merge pictures?
It's not difficult, actually. Before school, the book once said, picture file is a pixel composition, only we can get and save every picture of all the pixels, you can restore this picture at any time, of course, in order to ensure that the picture is not distorted, but also need to save the original width and height. PDFBox Code Implementation

Code in the use of the latest pdfbox2.0 version, you can go to the official website download

Package com.taoyuanx.utils;
Import Java.awt.image.BufferedImage;

Import Java.io.File;

Import Javax.imageio.ImageIO;
Import org.apache.pdfbox.pdmodel.PDDocument;
Import Org.apache.pdfbox.rendering.ImageType;


Import Org.apache.pdfbox.rendering.PDFRenderer;
    public class Pdftoimage {public static void main (string[] args) {pdftoimage ("d://test.pdf"); //Tested, DPI 96,100,105,120,150,200, 105 display more clear, volume is stable, the dpi higher picture volume larger//general computer display resolution for the public static final float DEF
    ault_dpi=105; /**pdf picture * @param pdfpath/public static void Pdftoimage (String pdfpath) {try{if (pdf path==null| | "". Equals (Pdfpath) | |!
                Pdfpath.endswith (". pdf")) return; Image merge Using parameters int width = 0; Total width int[] Singleimgrgb;
                Save the RGB data in a picture int shiftheight = 0; BufferedImage Imageresult = null;//Save pixel value per picture//use PDFBox to generate image pddocument Pddocument = pddocument.load (new File (Pdfpath));
               Pdfrenderer renderer = new Pdfrenderer (pddocument); Loops each page number for (int i=0,len=pddocument.getnumberofpages (); i<len; i++) {bufferedimage imag
                  E=RENDERER.RENDERIMAGEWITHDPI (i, DEFAULT_DPI,IMAGETYPE.RGB);
                 int Imageheight=image.getheight ();
                 int Imagewidth=image.getwidth (); 
                     if (i==0) {//Calculate height and offset width=imagewidth;//Use the first picture width;
                 Saves the pixel value of each page picture imageresult= new BufferedImage (width, imageheight*len, Bufferedimage.type_int_rgb); }else{Shiftheight + = imageheight//Compute offset Height}
                 Imgrgb= Image.getrgb (0, 0, width, imageheight, null, 0, width); Imageresult.setrgb (0, Shiftheight, width, imageheight, Singleimgrgb, 0, width);
                In the Write stream} pddocument.close (); File outfile = new File (Pdfpath.replace (". pdf", "_" +default_dpi+ ". jpg")); Imageio.write (imageresult, "JPG", outfile);//write Picture}catch (Exception e) {e.printstacktrace ()
            ;
 }

    }
}

Here only write the code of the PDF transfer picture, in fact, PDFBox still have a lot of operations, such as watermarks, drawings and so on, if we want to do some of the generated pictures to cut, draw text and so on, can be bufferimage this Java self-contained class implementation, may be some distortion of the picture. PostScript

Because possible business follow-up also involves the operation of the picture, so to understand the next, the Picture class library, found that Alibaba's simpleimage is easier to use, the image of the crop, flip will not be distorted, but there are some bugs, using simpleimage found when drawing custom fonts, Subclasses of Drawtextitem must be instantiated using a full parameter class.

SimpleImage also relies on Oracle's Jai Picture class library, you may also need to use to download the network, simple operation of the picture can also be.

Related Article

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.