问题描述
function void generateSideBySidePDF() {
File pdf1File = new File(FILE1_PATH);
File pdf2File = new File(FILE2_PATH);
File outPdfFile = new File(OUTFILE_PATH);
PDDocument pdf1 = null;
PDDocument pdf2 = null;
PDDocument outPdf = null;
try {
pdf1 = PDDocument.load(pdf1File);
pdf2 = PDDocument.load(pdf2File);
outPdf = new PDDocument();
// Create output PDF frame
PDRectangle pdf1Frame = pdf1.getPage(0).getCropBox();
PDRectangle pdf2Frame = pdf2.getPage(0).getCropBox();
PDRectangle outPdfFrame = new PDRectangle(pdf1Frame.getWidth()+pdf2Frame.getWidth(),Math.max(pdf1Frame.getHeight(),pdf2Frame.getHeight()));
// Create output page with calculated frame and add it to the document
COSDictionary dict = new COSDictionary();
dict.setItem(COSName.TYPE,COSName.PAGE);
dict.setItem(COSName.MEDIA_Box,outPdfFrame);
dict.setItem(COSName.CROP_Box,outPdfFrame);
dict.setItem(COSName.ART_Box,outPdfFrame);
PDPage outpdfpage = new PDPage(dict);
outPdf.addPage(outpdfpage);
// Source PDF pages has to be imported as form XObjects to be able to insert them at a specific point in the output page
LayerUtility layerUtility = new LayerUtility(outPdf);
PDFormXObject formPdf1 = layerUtility.importPageAsForm(pdf1,0);
PDFormXObject formPdf2 = layerUtility.importPageAsForm(pdf2,0);
// Add form objects to output page
AffineTransform afLeft = new AffineTransform();
layerUtility.appendFormAsLayer(outpdfpage,formPdf1,afLeft,"left");
AffineTransform afRight = AffineTransform.getTranslateInstance(pdf1Frame.getWidth(),0.0);
layerUtility.appendFormAsLayer(outpdfpage,formPdf2,afRight,"right");
outPdf.save(outPdfFile);
} catch (IOException e) {
e.printstacktrace();
} finally {
try {
if (pdf1 != null) pdf1.close();
if (pdf2 != null) pdf2.close();
if (outPdf != null) outPdf.close();
} catch (IOException e) {
e.printstacktrace();
}
}
}
但是,原始文档中包含的表单域不会显示在最终的 PDF 中。我还尝试在最终文档上设置 acroform,这样做:
outDoc.getDocumentCatalog().setAcroForm(acroForm);
但它不起作用。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)