commit | author | age
|
d73443
|
1 |
package vip.xiaonuo.modular.blogarticle; |
I |
2 |
|
|
3 |
import com.itextpdf.text.Document; |
|
4 |
import com.itextpdf.text.DocumentException; |
|
5 |
import com.itextpdf.text.pdf.PdfCopy; |
|
6 |
import com.itextpdf.text.pdf.PdfImportedPage; |
|
7 |
import com.itextpdf.text.pdf.PdfReader; |
|
8 |
|
|
9 |
import java.io.FileOutputStream; |
|
10 |
import java.io.IOException; |
|
11 |
import java.util.List; |
|
12 |
|
|
13 |
/** |
|
14 |
* pdf 切割合并demo |
|
15 |
*/ |
|
16 |
public class PdfUtils { |
|
17 |
|
|
18 |
public static void main(String args[]) throws IOException, DocumentException { |
|
19 |
try { |
|
20 |
//抽取页面 |
|
21 |
|
|
22 |
splitPDF("D:\\inleft\\Redis开发与运维(付磊).pdf", "D:\\inleft\\redis-split-1.pdf", |
|
23 |
28, 59); |
|
24 |
|
|
25 |
// splitPDF("D:\\桌面\\翻译任务\\IEC-61158-2-2003.pdf", "D:\\桌面\\翻译任务\\output1.pdf", |
|
26 |
// 231, 243); |
|
27 |
// splitPDF("D:\\桌面\\翻译任务\\IEC-61158-2-2003.pdf", "D:\\桌面\\翻译任务\\output2.pdf", |
|
28 |
// 260, 261); |
|
29 |
// //合并页面 |
|
30 |
// String[] files = {"C:\\Users\\Administrator\\Desktop\\毕业设计.pdf", "C:\\Users\\Administrator\\Desktop\\毕业论文中期检查表.pdf"}; |
|
31 |
// String savepath = "C:\\Users\\Administrator\\Desktop\\K.pdf"; |
|
32 |
// mergePdfFiles(Arrays.asList(files), savepath); |
|
33 |
} catch (Exception e) { |
|
34 |
e.printStackTrace(); |
|
35 |
} |
|
36 |
|
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* 合并原pdf为新文件 |
|
41 |
* |
|
42 |
* @param files pdf绝对路径集 |
|
43 |
* @param newfile 新pdf绝对路径 |
|
44 |
* @return |
|
45 |
* @throws IOException |
|
46 |
* @throws DocumentException |
|
47 |
*/ |
|
48 |
public static void mergePdfFiles(List<String> files, String newfile) throws IOException, DocumentException { |
|
49 |
Document document = new Document(new PdfReader(files.get(0)).getPageSize(1)); |
|
50 |
PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile)); |
|
51 |
document.open(); |
|
52 |
for (int i = 0; i < files.size(); i++) { |
|
53 |
PdfReader reader = new PdfReader(files.get(i)); |
|
54 |
int n = reader.getNumberOfPages(); |
|
55 |
for (int j = 1; j <= n; j++) { |
|
56 |
document.newPage(); |
|
57 |
PdfImportedPage page = copy.getImportedPage(reader, j); |
|
58 |
copy.addPage(page); |
|
59 |
} |
|
60 |
} |
|
61 |
document.close(); |
|
62 |
} |
|
63 |
|
|
64 |
public static void splitPDF(String bytes, String newFile, int start, int end) { |
|
65 |
Document document = null; |
|
66 |
PdfCopy copy = null; |
|
67 |
try { |
|
68 |
//源读取 |
|
69 |
PdfReader reader = new PdfReader(bytes); |
|
70 |
//获取pdf页数 |
|
71 |
int n = reader.getNumberOfPages(); |
|
72 |
if (end == 0) { |
|
73 |
end = n; |
|
74 |
} |
|
75 |
|
|
76 |
document = new Document(reader.getPageSize(1));//读取源第一页 |
|
77 |
copy = new PdfCopy(document, new FileOutputStream(newFile));//输出pdf初始化 |
|
78 |
|
|
79 |
document.open(); |
|
80 |
for (int j = start; j <= end; j++) { |
|
81 |
document.newPage(); |
|
82 |
PdfImportedPage page = copy.getImportedPage(reader, j);//翻页到初始页 |
|
83 |
copy.addPage(page);//输出文件追加内容 |
|
84 |
} |
|
85 |
document.close(); |
|
86 |
} catch (Exception e) { |
|
87 |
e.printStackTrace(); |
|
88 |
System.err.println("split pdf file error:" + e.getMessage()); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
} |