Java PDF Transfer Picture method __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:

Icepdf-core.jar, Icepdf-viewer.jar, Icepdf-pro.jar (seemingly to Watermark), Icepdf-pro-intl.jar (looks like to watermark)

Icepdf-core.jar, Icepdf-viewer.jar download Address: http://download.csdn.net/detail/romantic_pk/9672082

2, enclosed code examples:

Import Java.awt.image.BufferedImage;
Import Java.awt.image.RenderedImage;
Import Java.io.File;

Import java.io.IOException;

Import Javax.imageio.ImageIO;
Import org.icepdf.core.exceptions.PDFException;
Import org.icepdf.core.exceptions.PDFSecurityException;
Import org.icepdf.core.pobjects.Document;

Import org.icepdf.core.util.GraphicsRenderingHints; /** * PDF File Transfer picture (Icepdf technology) * @author Songjinzhou * @day 2016-11-03/public class Pdf2imgtest {public static void Mai
		N (string[] args) {String FilePath = "D:/fileupload/ftpdownload/xxx.pdf";

		Document document = new document ();
			try {document.setfile (FilePath); Float scale = 1.1f;//scaling (Large)//float scale = 0.2f;//scaling (small figure) Float rotation = 90f;//rotation angle for (int i = 0; I & Lt Document.getnumberofpages (); i++) {BufferedImage image = (bufferedimage) document.getpageimage (i, Graphicsrenderinghints.screen, org.i
				Cepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale); RenderedImage RendiMage = image;
					try {File File = new file ("d:/fileupload/ftpdownload/icepdf_a" + i + ". jpg");
				Here PNG function is: The format is JPG but has PNG definition imageio.write (rendimage, "PNG", file);
				catch (IOException e) {e.printstacktrace ();
			} image.flush ();
		} document.dispose ();
		catch (Pdfexception E1) {e1.printstacktrace ();
		catch (Pdfsecurityexception E1) {e1.printstacktrace ();
		catch (IOException E1) {e1.printstacktrace ();
	} System.out.println ("====================== complete ============================"); }
}

From the conversion effect, PNG's sharpness will be relatively higher than JPG. Here's a little trick. See the following two lines of code, which is converted to JPG format but PNG definition of the picture.

File File = new file ("d:/fileupload/ftpdownload/icepdf_a" + i + ". jpg");
Imageio.write (Rendimage, "PNG", file);

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, where the use of 1 jar packages, as follows:

Pdfbox-app-2.0.2.jar Jar Pack Free download address: http://download.csdn.net/detail/romantic_pk/9679121

2, enclosed code examples:

Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;

Import Javax.imageio.ImageIO;

Import org.apache.pdfbox.pdmodel.PDDocument;
Import Org.apache.pdfbox.rendering.PDFRenderer;

public class Pdf2imgtest {public

	static void Main (string[] args) {
		file File = new File ("C:\\test.pdf");
		try {
			PDDocument doc = pddocument.load (file);
			Pdfrenderer renderer = new Pdfrenderer (DOC);
			int pagecount = Doc.getnumberofpages ();
			for (int i = 0; i < PageCount i++) {
				//mode 1, the second parameter is to set the scaling ratio (that is, pixels)
				bufferedimage image = Renderer.renderimagewithd PI (i, 296);
				Mode 2, the second parameter is to set the scaling ratio (that is, pixels)
				//BufferedImage image = Renderer.renderimage (i, 2.5f);
				Imageio.write (Image, "PNG", New File ("C:\\pdfbox_image.png"));
			}
		catch (IOException e) {
			e.printstacktrace ();}}}

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:

1 Pdfdecoder decode_pdf = new Pdfdecoder (true);
 2 try {
 3 decode_pdf.openpdffile ("C:\\test.pdf");//file
 4//Decode_pdf.openpdffile ("C:/jpedalpdf.pdf", " Password "); Encrypted file
 5//      Decode_pdf.openpdfarray (bytes);//bytes is byte[] array with PDF
 6//      Decode_ Pdf.openpdffilefromurl ("Http://www.mysite.com/jpedalPDF.pdf", false);
 7//      Decode_pdf.openpdffilefrominputstream (in, false); 
 8 
 9 int start = 1, end = Decode_pdf.getpagecount ();
for (int i = start; I < end+1 i++) {One
bufferedimage img=decode_pdf.getpageasimage (i);
try {
imageio.write (IMG, PNG, New File ("C:\\jpedal_image.png"));
The catch (IOException e) {
e.printstacktrace ();
decode_pdf.closepdffile ();
The catch (Pdfexception e) {
e.printstacktrace ();
21}

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

Reprint Address: http://www.cnblogs.com/pcheng/p/5704470.html

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.