Xu Yanhua
Xu Yanhua

Reputation: 31

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

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?

Upvotes: 2

Views: 639

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18861

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

Upvotes: 2

Related Questions