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

放肆的年华 提交于 2019-12-06 08:13:04

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