Splitting a PDF results in very large PDF documents with PDFBox 2.0.2

旧巷老猫 提交于 2019-12-23 01:01:49

问题


I want to use command

java -jar pdfbox-app-2.y.z.jar PDFSplit [OPTIONS] <PDF file> 

to split one PDF into many other PDFs. But I found that there was a problem: the PDF splited is "ActiveMQ In Action(Manning-2011).pdf" and it's 14.1MB. But when I run

java -jar pdfbox-app-2.0.2.jar PDFSplit -split 5 -startPage 21 -endPage 40 -outputPrefix abc "ActiveMQ In Action(Manning-2011).pdf"

every PDF is lager than 79MB! How can I prevent this?


回答1:


This is a known bug in PDFBox 2.0.2. Splitting works fine in 2.0.1, and will work fine again in 2.0.3. The "bad" code has already been reverted. The reasons for the problem are discussed here. Long story short: version 2.0.2 does a deep clone on every source page, which results in duplication of resources.

Update: here's some workaround code for people who are using 2.0.2:

static public PDPage importPageFixed(PDDocument document, PDPage page) throws IOException
{
    PDPage importedPage = new PDPage(new COSDictionary(page.getCOSObject()), document.getResourceCache());
    InputStream in = null;
    try
    {
        in = page.getContents();
        if (in != null)
        {
            PDStream dest = new PDStream(document, in, COSName.FLATE_DECODE);
            importedPage.setContents(dest);
        }
        document.addPage(importedPage);
    }
    catch (IOException e)
    {
        IOUtils.closeQuietly(in);
    }
    return importedPage;
}


来源:https://stackoverflow.com/questions/37771252/splitting-a-pdf-results-in-very-large-pdf-documents-with-pdfbox-2-0-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!