Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to intercept the PDF specified page and convert the picture format

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use java to intercept the PDF specified page and convert the image format. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Introduce dependency

Org.apache.pdfbox pdfbox 2.0.16 org.apache.pdfbox fontbox 2.0.16

Download address of jar package:

Https://mvnrepository.com/artifact/org.apache.pdfbox/pdfboxhttps://mvnrepository.com/artifact/org.apache.pdfbox/fontbox

2. Implement DEMO

Package com.dddpeter.app;import org.apache.pdfbox.multipdf.Splitter;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.rendering.PDFRenderer;import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;import java.awt.image.BufferedImage;import java.io.*;import java.util.List;import java.util.ListIterator;public class PDFUtils {public static String splitPdf (int pageNum, String source, String dest) {File indexFile = new File (source); File outFile = new File (dest) PDDocument document = null; try {document = PDDocument.load (indexFile); / / document.getNumberOfPages (); Splitter splitter = new Splitter (); splitter.setStartPage (pageNum); splitter.setEndPage (pageNum); List pages = splitter.split (document); ListIterator iterator = pages.listIterator (); while (iterator.hasNext ()) {PDDocument pd = iterator.next () If (outFile.exists ()) {outFile.delete ();} pd.save (outFile); pd.close (); if (outFile.exists ()) {return outFile.getPath ();}} document.close ();} catch (IOException e) {e.printStackTrace () } catch (Exception e) {e.printStackTrace ();} return null;} public static void pdfFileToImage (File pdffile,String targetPath) {try {FileInputStream instream = new FileInputStream (pdffile); InputStream byteInputStream=null; try {PDDocument doc = PDDocument.load (instream); PDFRenderer renderer = new PDFRenderer (doc); int pageCount = doc.getNumberOfPages () If (pageCount > 0) {BufferedImage image = renderer.renderImage (0,4.0f); image.flush (); ByteArrayOutputStream bs = new ByteArrayOutputStream (); ImageOutputStream imOut; imOut = ImageIO.createImageOutputStream (bs); ImageIO.write (image, "png", imOut); byteInputStream = new ByteArrayInputStream (bs.toByteArray ()); byteInputStream.close () } doc.close ();} catch (IOException e) {e.printStackTrace ();} File uploadFile = new File (targetPath); FileOutputStream fops; fops = new FileOutputStream (uploadFile); fops.write (readInputStream (byteInputStream)); fops.flush (); fops.close ();} catch (Exception e) {e.printStackTrace () }} public static byte [] readInputStream (InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len = 0; while ((len = inStream.read (buffer))! =-1) {outStream.write (buffer, 0, len);} inStream.close (); return outStream.toByteArray () } public static void main (String [] args) {String path = splitPdf (4, "D:\\ data\\ 11.pdf", "D:\\ data\\ out11.pdf"); File file= new File (path); / / upload the image ending in png format String targetfile= "D:\\ data\\ out11.png"; pdfFileToImage (file,targetfile);}} Thank you for reading! On "how to use java to intercept PDF specified pages and image format conversion function" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report