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

查看:816
本文介绍了如何使用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作为这样的嵌入对象。我没有看到明确的实现,但是该过程类似于 PageExtractor PDFMergerUtility

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开发版本的当前SNAPSHOT从 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天全站免登陆