开发者

Merged documents in PDFBox either have no blank pages or too many... but only when printed

The project I'm currently working on requires big print runs of documents, which will be mailed out to clients. For instance, a single print run could include over 1000 letters, each of which will go to a different client.

To produce this print run, an external service (i.e. not using PDFBox) first generates each of these letters as an individual PDF file. Then my code loads all of these PDF files, merges them all together with PDFBox (so that they can be printed as a single job in the printing room), and I add a "banner page" to the front (also with PDFBox) which tells the printing room what to do with the letters.

Everything is printed double-sided. But since each letter is going to a different client, I can't have two letters printed back-to-back on a single sheet of paper. That means that any letter with an odd number of pages needs to have a blank page added to it. Just prior to printing, the merged PDF document for a very small print run containing only two letters could look something like this:

Banner Page
Blank
Letter 1
Blank
Letter 2
Blank

But this is where my problem comes in. I've added the blank pages with this code:

PDFMergerUtility docMerger = new PDFMergerUtility();
for (PDDocument currDoc : documents) {

    // Add blank page to document if odd number
    if (currDoc.getNumberOfPages() % 2 != 0) {
        currDoc.addPage(new PDPage());
    }

    // Merge into single document
    docMerger.appendDocument(mergedDoc, currDoc);

    // Close currDoc
    currDoc.close();
}

This seems to work consistently for banner开发者_StackOverflow pages, but for some types of letters, it screws up. If I open the resultant PDF, it looks okay, but when I print, I get an extra blank sheet of paper between each letter.

So while I see this in Acrobat Reader (or even PDFBox's viewer):

Sheet 1: Banner Page
Sheet 2: Letter 1
Sheet 3: Letter 2

The printed output looks like this:

Sheet 1: Banner Page
Sheet 2: Letter 1
Sheet 3: BLANK!
Sheet 4: Letter 2
Sheet 5: BLANK!

Meanwhile, if I take that new PDPage() code out, I get the following result, in both Acrobat and printed output:

Sheet 1: Banner Page
Sheet 2: Letter 1 (front) AND Letter 2 (back)

I've tried debugging through PDDocument.silentPrint(PrinterJob), but things start getting really dicey when it gets deep into the PDF's structure and nothing jumps out as being out of place. Any idea what could be going on?


Okay, I think I may have figured the problem out. It looks like some of the letters were designed with "A4" paper, rather than "Letter". If I just looked at the files in Acrobat Reader, it looked perfectly fine, and if I printed the letters from there, Reader must have automatically squeezed the output onto the available paper size. Unfortunately, PDFBox doesn't seem to do the same squeezing. So when printing through PDFBox, the letter would run slightly over onto the next page, causing the weird behaviour I saw.

I haven't had a chance to test this theory yet... the letter developer found the bug, but his changes haven't been put on our testing server yet. If this doesn't fix the problem, I'll report back.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜