PDFBox LayerUtility-将图层导入到现有的PDF中 [英] PDFBox LayerUtility - Importing layers into existing PDF

查看:261
本文介绍了PDFBox LayerUtility-将图层导入到现有的PDF中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pdfbox操纵PDF内容.我有一个很大的PDF文件(例如500页).我还有一些其他的单页PDF文件,仅包含一个图像,最大每个文件大约8-15kb.我需要做的是将这些单页pdf像叠加层一样导入到大PDF文件的某些页面上.

I am using pdfbox to manipulate PDF content. I have a big PDF file (say 500 pages). I also have a few other single page PDF files containing only a single image which are around 8-15kb per file at the max. What I need to do is to import these single page pdf's like an overlay onto certain pages of the big PDF file.

我已经尝试了成功的pdfbox的LayerUtility,但它会创建一个非常大的文件作为输出.在处理之前,源pdf约为1MB,当添加较小的pdf文件时,其最大大小为64MB.有时我需要在较大的PDF文件中包含两个较小的PDF文件.

I have tried the LayerUtility of pdfbox where I've succeeded but it creates a very large sized file as the output. The source pdf is about 1MB before processing and when added with the smaller pdf files, the size goes upto 64MB. And sometimes I need to include two smaller PDF's onto the bigger one.

是否有更好的方法可以做到这一点?或者我做错了吗?在下面的代码中尝试将两个图层添加到单个页面上:

Is there a better way to do this or am I just doing this wrong? Posting code below trying to add two layers onto a single page:

...
...
..
overlayDoc[pCounter] = PDDocument.load("data\\" + overlay + ".pdf");
outputPage[pCounter] = (PDPage) overlayDoc[pCounter].getDocumentCatalog().getAllPages().get(0);

LayerUtility lu = new LayerUtility( overlayDoc[pCounter] );
form[pCounter] = lu.importPageAsForm( bigPDFDoc, Integer.parseInt(pageNo)-1);
lu.appendFormAsLayer( outputPage[pCounter], form[pCounter], aTrans, "OVERLAY_"+pCounter );
outputDoc.addPage(outputPage[pCounter]);

mOverlayDoc[pCounter] = PDDocument.load("data\\" + overlay2 + ".pdf");                      
mOutputPage[pCounter] = (PDPage) mOverlayDoc[pCounter].getDocumentCatalog().getAllPages().get(0);

LayerUtility lu2 = new LayerUtility( mOverlayDoc[pCounter] );
mForm[pCounter] = lu2.importPageAsForm(outputDoc, outputDoc.getNumberOfPages()-1);
lu.appendFormAsLayer( mOutputPage[pCounter], mForm[pCounter], aTrans, "OVERLAY_2"+pCounter );

outputDoc.removePage(outputPage[pCounter]);
outputDoc.addPage(mOutputPage[pCounter]);
...
...

推荐答案

使用以下代码,我看不到大小的任何意外增长:

With code like the following I don't see any unepected growth of size:

PDDocument bigDocument = PDDocument.load(BIG_SOURCE_FILE);
LayerUtility layerUtility = new LayerUtility(bigDocument);
List bigPages = bigDocument.getDocumentCatalog().getAllPages();

// import each page to superimpose only once
PDDocument firstSuperDocument = PDDocument.load(FIRST_SUPER_FILE);
PDXObjectForm firstForm = layerUtility.importPageAsForm(firstSuperDocument, 0);

PDDocument secondSuperDocument = PDDocument.load(SECOND_SUPER_FILE);
PDXObjectForm secondForm = layerUtility.importPageAsForm(secondSuperDocument, 0);

// These things can easily be done in a loop, too
AffineTransform affineTransform = new AffineTransform(); // Identity... your requirements may differ
layerUtility.appendFormAsLayer((PDPage) bigPages.get(0), firstForm, affineTransform, "Superimposed0");
layerUtility.appendFormAsLayer((PDPage) bigPages.get(1), secondForm, affineTransform, "Superimposed1");
layerUtility.appendFormAsLayer((PDPage) bigPages.get(2), firstForm, affineTransform, "Superimposed2");

bigDocument.save(BIG_TARGET_FILE);

如您所见,我将FIRST_SUPER_FILE 的第一页叠加在目标文件的两页上,但是我只导入了该页面一次 .因此,该导入页面的资源也仅一次导入.

As you see I superimposed the first page of FIRST_SUPER_FILE on two pages of the target file but I only imported the page once. Thus, also the resources of that imported page are imported only once.

此方法也适用于循环,但请勿多次导入同一页面!而是将所有必需的模板页面一次作为表单导入,并在以后的循环中一次又一次地引用这些表单.

This approach is open for loops, too, but don't import the same page multiple times! Instead import all required template pages once up front as forms and in the later loop reference those forms again and again.

(我希望这可以解决您的问题.如果不能,请提供更多代码和样本PDF来重现您的问题.)

(I hope this solves your issue. If not, supply more code and the sample PDFs to reproduce your issue.)

这篇关于PDFBox LayerUtility-将图层导入到现有的PDF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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