0

I have a pdf document with one or more pages A4 paper. The resulting pdf document should be A3 paper where each page contains two from the first one (odd on the left, even on the right side). I already got it to render the A4 pages into images and the odd pages are successfully placed on the first parts of a new A3 pages but I cannot get the even pages to be placed.

public class CreateLandscapePDF {

public void renderPDF(File inputFile, String output) {
    PDDocument docIn = null;
    PDDocument docOut = null;
    float width = 0;
    float height = 0;
    float posX = 0;
    float posY = 0;
    try {
        docIn = PDDocument.load(inputFile);
        PDFRenderer pdfRenderer = new PDFRenderer(docIn);
        docOut = new PDDocument();
        int pageCounter = 0;
        for(PDPage pageIn : docIn.getPages()) {
            pageIn.setRotation(270);
            BufferedImage bufferedImage = pdfRenderer.renderImage(pageCounter);
            width = bufferedImage.getHeight();
            height = bufferedImage.getWidth();
            PDPage pageOut = new PDPage(PDRectangle.A3);
            PDImageXObject image = LosslessFactory.createFromImage(docOut, bufferedImage);
            PDPageContentStream contentStream = new PDPageContentStream(docOut, pageOut, AppendMode.APPEND, true, true);                
            if((pageCounter & 1) == 0) {
                pageOut.setRotation(90);
                docOut.addPage(pageOut);
                posX = 0;
                posY = 0;
            } else {
                posX = 0;
                posY = width;
            }
            contentStream.drawImage(image, posX, posY);
            contentStream.close();
            bufferedImage.flush();                
            pageCounter++;
        }
        docOut.save(output + "\\LandscapeTest.pdf");
        docOut.close();
        docIn.close();
    } catch(IOException io) {
        io.printStackTrace();
    }
}

}

I'm using Apache PDFBox 2.0.2 (pdfbox-app-2.0.2.jar)

  • Why are you creating the A3 page for each source page? It would be logical to create it only every 2 pages. You're adding it every 2 pages. Btw adding the page should be done after closing the content strream, not before. – Tilman Hausherr Aug 15 '16 at 10:33
  • 1
    Btw, steps for a more advanced solution can be found here: https://stackoverflow.com/questions/26256546/how-to-insert-an-pdpage-within-another-pdpage-with-pdfbox – Tilman Hausherr Aug 15 '16 at 10:35
  • @TilmanHausherr You might want to make that a [close/duplicate](http://stackoverflow.com/questions/38952984/pdfbox-put-two-a4-pages-on-one-a3#). By the way, interested in [PDFBox stackoverflow documentation](http://stackoverflow.com/documentation/pdfbox)? – mkl Aug 15 '16 at 12:08
  • @mkl not yet because 1) I think it would still be useful for kAy to understand the (alleged) programming error(s), 2) we don't know yet if kAy is required to have the A3 contents as two A4 images. Maybe later. – Tilman Hausherr Aug 15 '16 at 12:12
  • @mkl as for the documentation why not contribute to pdfbox-docs on github? – Maruan Sahyoun Aug 15 '16 at 14:18
  • @MaruanSahyoun - because I didn't know there is such a thing ;). And as I'm mostly on so, that is a media break for me. That been said, my question really was meant as a question, and an answer "No, I'm only documenting at xxx." would also be of interest to me. – mkl Aug 15 '16 at 14:27
  • @mkl I see you have edited your comment after I responded. Re documentation, first priority should be the PDFBox website and/or the javadocs. – Tilman Hausherr Aug 15 '16 at 15:19

1 Answers1

0

Thank you very much for your help and the link to the other question - I think I already read it but wasn't able to use in in my code yet. But finally the PDFClown made the job, though I think it's not very nice to use PDFBox and PDFClown in the same program. Anyway here's my working code to combine A4 pages on A3 paper.

public class CombinePages {

public void run(String input, String output) {
    try {
        Document source = new File(input).getDocument();
        Pages sourcePages = source.getPages();

        Document target = new File().getDocument();
        Page targetPage = null;

        int pageCounter = 0;
        double moveByX = .0;
        for(Page sourcePage : source.getPages()) {

            if((pageCounter & 1) == 0) {
                //even page gets a blank page
                targetPage = new Page(target);
                target.setPageSize(PageFormat.getSize(PageFormat.SizeEnum.A3, PageFormat.OrientationEnum.Landscape));
                target.getPages().add(targetPage);
                moveByX = .0;
            } else {
                moveByX = .50;
            }

            //get content from source page
            XObject xObject = sourcePages.get(pageCounter).toXObject(target);
            PrimitiveComposer composer = new PrimitiveComposer(targetPage);
            Dimension2D targetSize = targetPage.getSize();
            Dimension2D sourceSize = xObject.getSize();
            composer.showXObject(xObject, new Point2D.Double(targetSize.getWidth() * moveByX, targetSize.getHeight() * .0), new Dimension(sourceSize.getWidth(), sourceSize.getHeight()), XAlignmentEnum.Left, YAlignmentEnum.Top, 0);
            composer.flush();
            pageCounter++;
        }
        target.getFile().save(output + "\\CombinePages.pdf", SerializationModeEnum.Standard);
        source.getFile().close();
    } catch (FileNotFoundException fnf) {
        log.error(fnf);
    } catch (IOException io) {
        log.error(io);
    }

}

}

  • 1
    You could take a look at the SuperimposePage.java sample in the PDFBox examples package. This shows how to 'mount' a PDF onto another. – Maruan Sahyoun Aug 16 '16 at 10:17