java实现截取PDF指定页并进行图片格式转换功能
程序员文章站
2022-08-03 20:47:53
1、引入依赖
org.apache.pdfbox ...
1、引入依赖
<dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>pdfbox</artifactid> <version>2.0.16</version> </dependency> <dependency> <groupid>org.apache.pdfbox</groupid> <artifactid>fontbox</artifactid> <version>2.0.16</version> </dependency>
jar包下载地址:
2、实现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<pddocument> pages = splitter.split(document); listiterator<pddocument> 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); //上传的是png格式的图片结尾 string targetfile="d:\\data\\out11.png"; pdffiletoimage(file,targetfile); } }
总结
以上所述是小编给大家介绍的java实现截取pdf指定页并进行图片格式转换功能,希望对大家有所帮助
下一篇: 家常炖鸡怎么做更鲜美