PDFBox: put two A4 pages on one A3

旧巷老猫 提交于 2020-01-25 20:41:10

问题


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)


回答1:


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

}

}



来源:https://stackoverflow.com/questions/38952984/pdfbox-put-two-a4-pages-on-one-a3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!