Java PDF to pictures

Source: Internet
Author: User

The need for a PDF to picture has been used in recent projects and is documented here.

1. Based on Ghostscript

Use this method to require the running environment to install Ghostscript. The command used by the conversion is: Gs-sdevice=pngalpha-o%03d.png-sdevice=pngalpha-r144 test.pdf

 Public Staticlist<byte[]> pdf2image (String pdffilepath)throwsexception{File tempdir=NULL; Try{tempdir=Files.createtempdir (); Process proc=NewProcessbuilder ("GS", "-sdevice=pngalpha", "-o", TempDir + file.separator + "%03d.png", "-sdevice=pngalpha", "-r144", Pdffilepath). Redirecterrorstream (true). Start (); ArrayList<String> output =NewArraylist<string>(); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Proc.getinputstream ())); String Line=NULL;  while(line = Br.readline ())! =NULL) Output.add (line); Logger.info ("Output of the Execute GS command:" +stringutils.join (output, System.lineseparator ())); if(0! =proc.waitfor ())Throw NewException ("Conversion failed"); file[] Files=Tempdir.listfiles (); Arrays.sort (Files,NewComparator<file>() {                 Public intCompare (file f1, file F2) {returnf1.getname (). CompareTo (F2.getname ());                        }            }); List<byte[]> images =NewArraylist<>();  for(File file:files) Images.add (Ioutils.tobytearray (Newfileinputstream (file)); returnimages; }finally{            if(TempDir! =NULL) fileutils.deletedirectory (tempdir); }    }

Ghostscript also has a lot of common commands, interested can go to see: https://www.ghostscript.com/doc/current/Use.htm

2. Based on ImageMagick

But my project is to have a multi-page PDF into a picture, Ghostscript always turn it into multiple images (I have been looking for a long time online, did not find a picture of the order, if there are small partners know, but also want to share), so I found on the internet ImageMagick , the main thing was to find the command to convert the entire PDF to a picture,

The specific execution command is: Convert Test.pdf-append-flatten test.png

Of course you need to install ImageMagick,

The installation command is: Yum install ImageMagick Imagemagick-devel

Java PDF to pictures

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.