[英]How to split a pdf into multiple pages in a single pdf using PDFBOX
Requirement: My pdf has 5 pages, I need to start the split on the second page and end at the last page, in a single pdf.要求:我的pdf有5页,我需要在第二页开始拆分并在最后一页结束,在一个pdf中。 Currently, I have made it so that it splits one pdf per page.
目前,我已经做到了,每页拆分一个 pdf。
Current code:当前代码:
public static boolean SepararFC(String sequence, String pathfrom, String pathto) {
try (PDDocument document = PDDocument.load(new File(pathfrom))) {
Splitter splitter = new Splitter();
List<PDDocument> Pages = splitter.split(document);
for (int i = 0; i < Pages.size(); i++) {
PDDocument doc = Pages.get(i);
doc.save(new File(pathfrom, sequence + "_" + i + ".pdf"));
}
} catch (IOException e){
System.err.println(e);
}
return true;
Since you don't want page 1, you can just remove it and save a new file由于您不想要第 1 页,因此您可以将其删除并保存一个新文件
try (PDDocument document = PDDocument.load(new File(pathfrom))) {
//Listing the number of existing pages
int noOfPages= document.getNumberOfPages();
System.out.print(noOfPages);
//Removing the pages
document.removePage(0);
System.out.println("page removed");
//Saving the document
document.save(new File(pathfrom, sequence + "new"+ ".pdf"));
//Closing the document
document.close();}
catch (IOException e){
System.err.println(e);
}
Assuming your code is correct:假设您的代码是正确的:
public static boolean SepararFC(String sequence, String pathfrom, String pathto) {
int start = 1;
int end = 4;
try (PDDocument document = PDDocument.load(new File(pathfrom))) {
Splitter splitter = new Splitter();
List<PDDocument> Pages = splitter.split(document);
for (int i = 0; i < Pages.size(); i++) {
PDDocument doc = Pages.get(i);
if(i > start && i <= end){
doc.save(new File(pathfrom, sequence + "_" + i + ".pdf"));
}
}
} catch (IOException e){
System.err.println(e);
}
return true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.