如何使用PDFBOX在一个pdf中将pdf分成多页

问题描述 投票:0回答:2

要求:我的pdf有5页,我需要在一个PDF中从第二页开始拆分,最后一页结束。目前,我已经做到了,以便每页分割一个pdf。

当前代码:


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;

java pdfbox
2个回答
0
投票

假设您的代码正确:

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;

0
投票

由于不想使用第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);
}
© www.soinside.com 2019 - 2024. All rights reserved.