使用 PDFBox 2.0.2 拆分 PDF 会生成非常大的 PDF 文档 [英] Splitting a PDF results in very large PDF documents with PDFBox 2.0.2

查看:97
本文介绍了使用 PDFBox 2.0.2 拆分 PDF 会生成非常大的 PDF 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用命令

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

将一个 PDF 拆分为多个其他 PDF.但是我发现有一个问题:拆分的PDF是ActiveMQ In Action(Manning-2011).pdf",大小为14.1MB.但是当我跑

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"

每个 PDF 都大于 79MB!我怎样才能防止这种情况?

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

推荐答案

这是 PDFBox 2.0.2 中的已知错误.拆分在 2.0.1 中工作正常,在 2.0.3 中再次正常工作.坏"代码已经恢复.此处讨论了问题的原因.长话短说:2.0.2 版对每个源页面进行深度克隆,导致资源重复.

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.

更新:这里有一些适用于使用 2.0.2 的人的解决方法代码:

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;
}

这篇关于使用 PDFBox 2.0.2 拆分 PDF 会生成非常大的 PDF 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆