Java实现PDF文件分割

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

 


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Split2 {
    public static void main(String[] args) {
        String inputFile= "C:\\1.pdf";
        splitFile(inputFile,1,1);//第1页为一个文件
        splitFile(inputFile,2,2);//第2页为一个文件
        splitFile(inputFile,3,4);//第3,4页为一个文件
        splitFile(inputFile,5,8);//以下类似
        splitFile(inputFile,9,12);
        splitFile(inputFile,13,13);
        splitFile(inputFile,14,18);
    }

    public static String splitFile(String pdfFile,Integer from,Integer end){

        Document document = null;
        PdfCopy copy = null;
        try {
            PdfReader reader = new PdfReader(pdfFile);
            int n = reader.getNumberOfPages();
            if(end==0){
                end = n;
            }
            List<String> savepaths = new ArrayList<String>();
            int a = pdfFile.lastIndexOf(".pdf");
            String staticpath = pdfFile.substring(0, a);
            String savepath = staticpath+ "_from_"+from+"_to_"+end+"_.pdf";
            savepaths.add(savepath);
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
            document.open();
            for(int j=from; j<=end; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, j);
                copy.addPage(page);
            }
            document.close();
            return new File(savepath).getName();
        } catch (IOException e) {
            return null;
        } catch(DocumentException e) {
            return null;
        }
    }

}

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要导出多个PDF文件,你需要编写Java代码来生成每个文件。以下是一个简单的示例代码,用于将多个PDF文件从单个输入文件中创建: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.pdmodel.PDDocument; public class PDFCreator { public static void main(String[] args) throws IOException { // 读取输入文件 File inputFile = new File("input.pdf"); PDDocument document = PDDocument.load(inputFile); // 分割输入文件为单独的页面 List<PDDocument> documents = new ArrayList<>(); for (int i = 0; i < document.getNumberOfPages(); i++) { PDDocument singlePageDocument = new PDDocument(); singlePageDocument.addPage(document.getPage(i)); documents.add(singlePageDocument); } // 保存每个单独的页面为PDF文件 for (int i = 0; i < documents.size(); i++) { PDDocument singlePageDocument = documents.get(i); String outputFileName = "output_" + (i + 1) + ".pdf"; singlePageDocument.save(outputFileName); singlePageDocument.close(); } // 合并所有输出文件为单个PDF文件 PDFMergerUtility merger = new PDFMergerUtility(); for (int i = 0; i < documents.size(); i++) { String outputFileName = "output_" + (i + 1) + ".pdf"; merger.addSource(outputFileName); } merger.setDestinationFileName("output.pdf"); merger.mergeDocuments(); // 关闭输入文件 document.close(); } } ``` 这个示例程序将输入文件 "input.pdf" 分割为单独的页面,并将每个页面保存为一个单独的PDF文件(如 "output_1.pdf","output_2.pdf"等)。然后,它使用 PDFBox 库的 `PDFMergerUtility` 类将所有输出文件合并为单个PDF文件(如 "output.pdf")。你可以根据自己的需求来修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值