JAVA PDF 截取N页,生成新文件,转图片
import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPageTree; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; public class PdfUtil { /** * 截取pdfFile的第from页至第end页,组成一个新的文件名 * * @param pdfFile 要切割的pdf文件 * @param newFile 切割后形成的新的pdf文件 * @param from 从第N页开始 * @param end 到第N页结束 */ public static void partitionPdf(String pdfFile, String newFile, int from, int end) { Document document = null; PdfCopy copy = null; PdfReader reader = null; try { reader = new PdfReader(pdfFile); int pageCount = reader.getNumberOfPages(); if (from < 1) { from = 1; } if (from > pageCount) { from = pageCount; } if (end == 0 || end > pageCount) { end = pageCount; } document = new Document(reader.getPageSize(1)); copy = new PdfCopy(document, new FileOutputStream(newFile)); document.open(); for (int j = from; j ) { document.newPage(); PdfImportedPage page = copy.getImportedPage(reader, j); copy.addPage(page); } } catch (Exception e) { e.printStackTrace(); } finally { if (document != null) { document.close(); } if (copy != null) { copy.close(); } if (reader != null) { reader.close(); } } } /** * pdf转图片 * * @param pdfFile PDF 文件 * @param imageFile 输出的图片文件 * @param from 开始页 从1开始 * @param end 结束页 最大为PDF总页数 * @throws Exception */ public static void pdfToImage(String pdfFile, String imageFile, int from, int end) throws Exception { PDDocument doc = null; ByteArrayOutputStream os = null; InputStream stream = null; OutputStream out = null; try { //pdf路径 stream = new FileInputStream(pdfFile); // 加载解析PDF文件 doc = PDDocument.load(stream); PDFRenderer pdfRenderer = new PDFRenderer(doc); PDPageTree pages = doc.getPages(); int pageCount = pages.getCount(); if (from < 1) { from = 1; } if (from > pageCount) { from = pageCount; } if (end == 0 || end > pageCount) { end = pageCount; } for (int i = from; i ) { BufferedImage bim = pdfRenderer.renderImageWithDPI(i - 1, 200); //PDFBOX 是从0开始的,from初始值为1,所以这边要减 i-1 os = new ByteArrayOutputStream(); ImageIO.write(bim, "jpg", os); byte[] dataList = os.toByteArray(); // jpg文件转出路径 File file = new File(imageFile.replace(".jpg", i + ".jpg")); if (!file.getParentFile().exists()) { // 不存在则创建父目录及子文件 file.getParentFile().mkdirs(); file.createNewFile(); } out = new FileOutputStream(file); out.write(dataList); } } catch (Exception e) { e.printStackTrace(); } finally { if (doc != null) { doc.close(); } if (os != null) { os.close(); } if (stream != null) { stream.close(); } if (out != null) { out.close(); } } } }
Original: https://www.cnblogs.com/vipsoft/p/16624803.html
Author: VipSoft
Title: JAVA PDF 截取N页,生成新文件,转图片
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/609416/
转载文章受原作者版权保护。转载请注明原作者出处!