当前位置:   article > 正文

Java使用PDFBox将一个 PDF 文档拆分为多个 PDF_pdfbox 将一页的pdf裁剪为两页的pdf

pdfbox 将一页的pdf裁剪为两页的pdf

要将一个 PDF 文档拆分为多个 PDF 文档,您可以使用 PDFBox Java API 的 Splitter.split() 方法。

在本教程中,我们将学习使用示例 Java 程序拆分 PDF 文档。

PDFBox 示例 1 - 将 PDF 拆分为多个 PDF

在此示例中,我们将采用多页 PDF,并将此 PDF 文档拆分为多个 PDF,其中每个生成的 PDF 文档仅包含源文档中的一页。

SplitPDFExample.java

import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
 
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
 
public class SplitPDFExample {
 
    public static void main(String[] args) throws IOException {
          File file = new File("/home/tk/sample_pdf.pdf");
           
          // load pdf file
          PDDocument document = PDDocument.load(file);
 
          // instantiating Splitter
          Splitter splitter = new Splitter();
           
          // split the pages of a PDF document
          List<PDDocument> Pages = splitter.split(document);
 
          // Creating an iterator
          Iterator<PDDocument> iterator = Pages.listIterator();
 
          // saving splits as pdf
          int i = 0;
          while(iterator.hasNext()) {
             PDDocument pd = iterator.next();
             // provide destination path to the PDF split
             pd.save("/home/tk/pdfs/sample_part_"+ ++i +".pdf");
             System.out.println("Saved /home/tk/pdfs/sample_part_"+ i +".pdf");
          }
          System.out.println("Provided PDF has been split into multiple.");
          document.close();
    }
 
}

输出

Saved /home/tk/pdfs/sample_part_1.pdf
Saved /home/tk/pdfs/sample_part_2.pdf
Saved /home/tk/pdfs/sample_part_3.pdf
Saved /home/tk/pdfs/sample_part_4.pdf
Saved /home/tk/pdfs/sample_part_5.pdf
Saved /home/tk/pdfs/sample_part_6.pdf
Provided PDF has been split into multiple.

PDFBox 示例 2 - 以指定间隔拆分 PDF

以下是将 PDF 文档拆分为多个 PDF 文档的分步指南,这些文档已在源文档中以特定间隔拆分。

SplitPDFAtPageExample.java

import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
 
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
 
public class SplitPDFAtPageExample {
 
    public static void main(String[] args) throws IOException {
          File file = new File("/home/tk/sample_pdf.pdf");
           
          // load pdf file
          PDDocument document = PDDocument.load(file);
 
          // instantiating Splitter
          Splitter splitter = new Splitter();
           
          splitter.setSplitAtPage(2);
           
          // split the pages of a PDF document
          List<PDDocument> Pages = splitter.split(document);
 
          // Creating an iterator
          Iterator<PDDocument> iterator = Pages.listIterator();
 
          // saving splits as pdf
          int i = 0;
          while(iterator.hasNext()) {
             PDDocument pd = iterator.next();
             pd.save("/home/tk/pdfs/sample_part_"+ ++i +".pdf");
             System.out.println("Saved /home/tk/pdfs/sample_part_"+ i +".pdf");
          }
           
          // close the document
          document.close();
    }
 
}

输出

Saved /home/tk/pdfs/sample_part_1.pdf
Saved /home/tk/pdfs/sample_part_2.pdf
Saved /home/tk/pdfs/sample_part_3.pdf

默认情况下,splitAtPage 设置为 1。

结论

在本PDFBox 教程中,我们学习了将 PDF 文档拆分为多个 PDF。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/530713
推荐阅读
相关标签
  

闽ICP备14008679号