From d734432a8bbbf863dc3de305f56e831c56ac767a Mon Sep 17 00:00:00 2001 From: inleft <inleft@qq.com> Date: Tue, 15 Feb 2022 14:53:47 +0800 Subject: [PATCH] pdf 切割合并demo --- snowy-main/src/main/java/vip/xiaonuo/modular/blogarticle/PdfUtils.java | 92 ++++++++++++++++++++++++++++++++++++++++++++++ snowy-main/pom.xml | 10 +++++ 2 files changed, 102 insertions(+), 0 deletions(-) diff --git a/snowy-main/pom.xml b/snowy-main/pom.xml index edcbd78..1557809 100644 --- a/snowy-main/pom.xml +++ b/snowy-main/pom.xml @@ -36,6 +36,16 @@ <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> + + <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> + <dependency> + <groupId>com.itextpdf</groupId> + <artifactId>itextpdf</artifactId> + <version>5.5.13.2</version> + </dependency> + + + </dependencies> <build> diff --git a/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticle/PdfUtils.java b/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticle/PdfUtils.java new file mode 100644 index 0000000..34c0df8 --- /dev/null +++ b/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticle/PdfUtils.java @@ -0,0 +1,92 @@ +package vip.xiaonuo.modular.blogarticle; + +import com.itextpdf.text.Document; +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.pdf.PdfCopy; +import com.itextpdf.text.pdf.PdfImportedPage; +import com.itextpdf.text.pdf.PdfReader; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; + +/** + * pdf 切割合并demo + */ +public class PdfUtils { + + public static void main(String args[]) throws IOException, DocumentException { + try { + //抽取页面 + + splitPDF("D:\\inleft\\Redis开发与运维(付磊).pdf", "D:\\inleft\\redis-split-1.pdf", + 28, 59); + +// splitPDF("D:\\桌面\\翻译任务\\IEC-61158-2-2003.pdf", "D:\\桌面\\翻译任务\\output1.pdf", +// 231, 243); +// splitPDF("D:\\桌面\\翻译任务\\IEC-61158-2-2003.pdf", "D:\\桌面\\翻译任务\\output2.pdf", +// 260, 261); +// //合并页面 +// String[] files = {"C:\\Users\\Administrator\\Desktop\\毕业设计.pdf", "C:\\Users\\Administrator\\Desktop\\毕业论文中期检查表.pdf"}; +// String savepath = "C:\\Users\\Administrator\\Desktop\\K.pdf"; +// mergePdfFiles(Arrays.asList(files), savepath); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * 合并原pdf为新文件 + * + * @param files pdf绝对路径集 + * @param newfile 新pdf绝对路径 + * @return + * @throws IOException + * @throws DocumentException + */ + public static void mergePdfFiles(List<String> files, String newfile) throws IOException, DocumentException { + Document document = new Document(new PdfReader(files.get(0)).getPageSize(1)); + PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile)); + document.open(); + for (int i = 0; i < files.size(); i++) { + PdfReader reader = new PdfReader(files.get(i)); + int n = reader.getNumberOfPages(); + for (int j = 1; j <= n; j++) { + document.newPage(); + PdfImportedPage page = copy.getImportedPage(reader, j); + copy.addPage(page); + } + } + document.close(); + } + + public static void splitPDF(String bytes, String newFile, int start, int end) { + Document document = null; + PdfCopy copy = null; + try { + //源读取 + PdfReader reader = new PdfReader(bytes); + //获取pdf页数 + int n = reader.getNumberOfPages(); + if (end == 0) { + end = n; + } + + document = new Document(reader.getPageSize(1));//读取源第一页 + copy = new PdfCopy(document, new FileOutputStream(newFile));//输出pdf初始化 + + document.open(); + for (int j = start; j <= end; j++) { + document.newPage(); + PdfImportedPage page = copy.getImportedPage(reader, j);//翻页到初始页 + copy.addPage(page);//输出文件追加内容 + } + document.close(); + } catch (Exception e) { + e.printStackTrace(); + System.err.println("split pdf file error:" + e.getMessage()); + } + } + +} -- Gitblit v1.9.1