0

我有一个包含一页或多页 A4 纸的 pdf 文档。生成的 pdf 文档应该是 A3 纸,其中每页包含第一页中的两页(左侧奇数,右侧偶数)。我已经可以将 A4 页面渲染为图像,并且奇数页已成功放置在新 A3 页面的第一部分,但我无法放置偶数页。

公共类 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)

4

1 回答 1

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

}

}

于 2016-08-16T09:16:57.890 回答