PDFBox:在一张A3上放两张A4纸

时间:2016-08-15 09:58:17

标签: java pdfbox drawimage pdfrenderer

我有一张带有一页或多页A4纸的pdf文件。 得到的pdf文档应该是A3纸,其中每页包含两个来自第一页(左边是奇数,甚至是右边)。 我已经得到它将A4页面渲染成图像,并且奇数页面成功地放置在新A3页面的第一部分上,但我无法获得偶数页面。

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

}

我正在使用Apache PDFBox 2.0.2(pdfbox-app-2.0.2.jar)

1 个答案:

答案 0 :(得分:0)

非常感谢您的帮助以及其他问题的链接 - 我想我已经阅读过了,但我还没能在我的代码中使用它。 但最终PDFClown完成了这项工作,尽管我认为在同一个程序中使用PDFBox和PDFClown并不是很好。 无论如何,这是我在A3纸上组合A4页面的工作代码。

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

}

}