如何使用 pdfbox 在另一个 PDPage 中插入一个 PDPage [英] How to insert an PDPage within another PDPage with pdfbox

查看:31
本文介绍了如何使用 pdfbox 在另一个 PDPage 中插入一个 PDPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用处理等不同的工具来创建矢量图.这些图被写成单页或多页 pdf.我想使用 pdfbox 将这些图包含在一个类似报告的 pdf 中.

I use different tools like processing to create vector plots. These plots are written as single or multi-page pdfs. I would like to include these plots in a single report-like pdf using pdfbox.

我当前的工作流程将这些 pdf 作为图像包含在以下伪代码中

My current workflow includes these pdfs as images with the following pseudo code

PDDocument inFile = PDDocument.load(file);
PDPage firstPage = (PDPage) inFile.getDocumentCatalog().getAllPages().get(0);
BufferedImage image = firstPage.convertToImage(BufferedImage.TYPE_INT_RGB, 300);
PDXObjectImage ximage = new PDPixelMap(document, image);

PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawXObject(ximage, 0, 0, ximage.getWidth(), ximage.getHeight());
contentStream.close();

虽然这有效,但它失去了矢量文件格式的优势,尤其是文件/大小与打印质量.

While this works it looses the benefits of the vector file formats, espectially file/size vs. printing qualitity.

是否可以使用 pdfbox 将其他 pdf 页面作为嵌入对象包含在页面中(未添加为单独的页面)?我可以例如使用 PDStream?我更喜欢像 pdflatex 这样的解决方案能够将 pdf 数字嵌入到新的 pdf 文档中.

Is it possible to use pdfbox to include other pdf pages as embedded objects within a page (Not added as a separate page)? Could I e.g. use a PDStream? I would prefer a solution like pdflatex is able to embed pdf figures into a new pdf document.

您可以为该任务推荐哪些其他 Java 库?

What other Java libraries can you recommend for that task?

推荐答案

是否可以使用pdfbox将其他pdf页面作为嵌入对象包含在页面中

Is it possible to use pdfbox to include other pdf pages as embedded objects within a page

应该是可以的.PDF 格式允许使用所谓的表单 xobjects 作为此类嵌入对象.不过,我没有看到明确的实现,但该过程与 PageExtractorPDFMergerUtility 所做的非常相似.

It should be possible. The PDF format allows the use of so called form xobjects to serve as such embedded objects. I don't see an explicit implementation for that, though, but the procedure is similar enough to what PageExtractor or PDFMergerUtility do.

使用 PDFBox 2.0.0 开发版本的当前快照从 PageExtractor 派生的概念证明:

A proof of concept derived from PageExtractor using the current SNAPSHOT of the PDFBox 2.0.0 development version:

PDDocument source = PDDocument.loadNonSeq(SOURCE, null);
List<PDPage> pages = source.getDocumentCatalog().getAllPages();

PDDocument target = new PDDocument();
PDPage page = new PDPage();
PDRectangle cropBox = page.findCropBox();
page.setResources(new PDResources());
target.addPage(page);

PDFormXObject xobject = importAsXObject(target, pages.get(0));
page.getResources().addXObject(xobject, "X");

PDPageContentStream content = new PDPageContentStream(target, page);
AffineTransform transform = new AffineTransform(0, 0.5, -0.5, 0, cropBox.getWidth(), 0);
content.drawXObject(xobject, transform);
transform = new AffineTransform(0.5, 0.5, -0.5, 0.5, 0.5 * cropBox.getWidth(), 0.2 * cropBox.getHeight());
content.drawXObject(xobject, transform);
content.close();

target.save(TARGET);
target.close();
source.close();

此代码将源文档的第一页作为 XObject 导入目标文档,然后将其两次放入具有不同缩放和旋转变换的页面上,例如对于这个来源

This code imports the first page of a source document to a target document as XObject and puts it twice onto a page there with different scaling and rotation transformations, e.g. for this source

它创造了这个

实际执行导入的辅助方法 importAsXObject 定义如下:

The helper method importAsXObject actually doing the import is defined like this:

PDFormXObject importAsXObject(PDDocument target, PDPage page) throws IOException
{
    final PDStream src = page.getContents();
    if (src != null)
    {
        final PDFormXObject xobject = new PDFormXObject(target);

        OutputStream os = xobject.getPDStream().createOutputStream();
        InputStream is = src.createInputStream();
        try
        {
            IOUtils.copy(is, os);
        }
        finally
        {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }

        xobject.setResources(page.findResources());
        xobject.setBBox(page.findCropBox());

        return xobject;
    }
    return null;
}

如上所述,这只是概念验证,尚未考虑极端情况.

As mentioned above this is only a proof of concept, corner cases have not yet been taken into account.

这篇关于如何使用 pdfbox 在另一个 PDPage 中插入一个 PDPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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