m0_63645168
2022-05-07 18:50
采纳率: 83.3%
浏览 38

java 切割PDF 分页读取、操作

问题遇到的现象和发生背景

public static void main(String[] args) throws Exception{
splitPdf("https://res.ygyg.cn/userbusipub/M00/14/A1/CicmImJ2OGqAf1iXAc4gpYaD6HU101.pdf", 1);

}
public static  void splitPdf(String filename, int splitSize) throws Exception {
    PdfReader reader;
    try {
        reader = new PdfReader(filename, null, true);
    } catch (IOException e) {
        throw new Exception("读取PDF文件失败");
    }
    int numberOfPages = reader.getNumberOfPages();
    int newFileCount = 0;
    // PageNumber是从1开始计数的
    int pageNumber = 1;
    while (pageNumber <= numberOfPages) {
        Document doc = new Document();
        String splitFileName = filename.substring(0, filename.length() - 4) + "(" + newFileCount + ").pdf";
        PdfCopy pdfCopy;
        try {
            pdfCopy = new PdfCopy(doc, new FileOutputStream(splitFileName));
        } catch (FileNotFoundException | DocumentException e) {
            throw new Exception("切割文件副本创建失败");
        }
        doc.open();
        // 将pdf按页复制到新建的PDFfor (int i = 1; pageNumber <= numberOfPages && i <= splitSize; ++i, pageNumber++) {
            doc.newPage();
            PdfImportedPage page = pdfCopy.getImportedPage(reader, pageNumber);
            pdfCopy.addPage(page);
        }
        doc.close();
        newFileCount++;
        pdfCopy.close();
    }
}

为什么是自己本地的PDF可以切割 但是以https为路径的PDF切割不了

img


如果想吧一个100页的大PDF 以每15页切割成一个小PDF怎么操作

2条回答 默认 最新

相关推荐 更多相似问题