PDFBox:在一张 A3 纸上放两张 A4 纸 [英] PDFBox: put two A4 pages on one A3

查看:108
本文介绍了PDFBox:在一张 A3 纸上放两张 A4 纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份包含一页或多页 A4 纸的 pdf 文档.生成的 pdf 文档应该是 A3 纸,其中每页包含第一页中的两页(左侧奇数,甚至右侧).我已经把 A4 页面渲染成图像,奇数页面成功放置在新 A3 页面的第一部分,但我无法放置偶数页面.

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 {

}

我使用的是 Apache PDFBox 2.0.2 (pdfbox-app-2.0.2.jar)

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

推荐答案

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

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

}

}

这篇关于PDFBox:在一张 A3 纸上放两张 A4 纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆