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)

7
  • 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. Aug 15, 2016 at 10:33
  • 1
    Btw, steps for a more advanced solution can be found here: stackoverflow.com/questions/26256546/… Aug 15, 2016 at 10:35
  • @TilmanHausherr You might want to make that a close/duplicate. By the way, interested in PDFBox stackoverflow documentation?
    – mkl
    Aug 15, 2016 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. Aug 15, 2016 at 12:12
  • @mkl as for the documentation why not contribute to pdfbox-docs on github? Aug 15, 2016 at 14:18

1 Answer 1

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
  • 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. Aug 16, 2016 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.