0

I've got a printer which doesn't support the feature I need.

The printer prints A2 paper size. I would like to print two A3 size pages, that would fit on a single A2 paper, but my printer doesn't support this.

I already called the support of the company, but they told me I need to buy a newer one because my printer doesn't support this function. (Its very funny because an even older version of that printer does support this function).

So I tried to use the Apache PDFBox, where I can load my pdf file like this:

File pdfFile = new File(path);
PDDocument pdfDocument = load(pdfFile);

The file I loaded is size A3. I think it would be enough if I could get a new PDDocument with A2 paper size. Then put my loaded pdfFile twice in an A2 paper.

All in all, I need the file I loaded there two times on one page. I just don't know how to do that.

Best regards.

Leigh
  • 28,765
  • 10
  • 55
  • 103
unrated
  • 252
  • 4
  • 22
  • If using iText also was an option, I would propose looking at the [NUpTool.java](http://itextpdf.com/examples/iia.php?id=115) sample. With some minor modification (instead of scaling down the pages in a target document with the original document's page size you'd have to keep the scale and use a target document with twice the page size of the original). I assume, though, that that somehow is possible with PDFBox, too. – mkl May 20 '13 at 09:52

1 Answers1

0

You might want to look at the PageCombinationSample.java which according to its JavaDoc does nearly what you need:

This sample demonstrates how to combine multiple pages into single bigger pages (for example two A4 modules into one A3 module) using form XObjects [PDF:1.6:4.9].

Form XObjects are a convenient way to represent contents multiple times on multiple pages as templates.

The central code:

// 1. Opening the source PDF file...
File sourceFile = new File(filePath);

// 2. Instantiate the target PDF file!
File file = new File();

// 3. Source page combination into target file.
Document document = file.getDocument();
Pages pages = document.getPages();
int pageIndex = -1;
PrimitiveComposer composer = null;
Dimension2D targetPageSize = PageFormat.getSize(SizeEnum.A4);
for(Page sourcePage : sourceFile.getDocument().getPages())
{
    pageIndex++;
    int pageMod = pageIndex % 2;
    if(pageMod == 0)
    {
        if(composer != null)
        {composer.flush();}

        // Add a page to the target document!
        Page page = new Page(
            document,
            PageFormat.getSize(SizeEnum.A3, OrientationEnum.Landscape)
        ); // Instantiates the page inside the document context.
        pages.add(page); // Puts the page in the pages collection.
        // Create a composer for the target content stream!
        composer = new PrimitiveComposer(page);
    }

    // Add the form to the target page!
    composer.showXObject(
        sourcePage.toXObject(document), // Converts the source page into a form inside the target document.
        new Point2D.Double(targetPageSize.getWidth() * pageMod, 0),
        targetPageSize,
        XAlignmentEnum.Left,
        YAlignmentEnum.Top,
        0
    );
}
composer.flush();

// 4. Serialize the PDF file!
serialize(file, "Page combination", "combining multiple pages into single bigger ones", "page combination");

// 5. Closing the PDF file...
sourceFile.close();
mkl
  • 90,588
  • 15
  • 125
  • 265
  • As a note: This uses pdfclown, but not pdfbox as asked for in the question. Got me for a short moment when I was searching for the corresponding classes in pdfbox and did not find them... So the answer may be suitable for the original poster, but not for people who already have a lot of code depending on pdfbox. – cello Apr 21 '14 at 11:20
  • @cello Oh, you're right. I actually have no idea why I turned to PdfClown for an example... especially after I referred to iText in a comment to the original question... – mkl Apr 22 '14 at 12:47