New file |
| | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | | } |