解决方案
以下是使用 Java 从 PDF 文档中删除页面的示例程序。
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
public class RemovingPagesFromPdf {
public static void main(String args[]) throws IOException {
//Loading an existing document
File file = new File("C:/pdfBox/RemovePages_IP.pdf");
PDDocument doc = PDDocument.load(file);
//Listing the number of existing pages
System.out.print(doc.getNumberOfPages());
for(int i = 0; i<5; i++){
//removing the pages
doc.removePage(i);
}
System.out.println("page removed");
//Saving the document
doc.save("C:/pdfBox/RemovePages_OP.pdf");
//Closing the document
doc.close();
}
}