【发布时间】:2021-03-25 20:30:56
【问题描述】:
我正在使用 pdfbox 库 2.0 版本。我需要在新的浏览器选项卡(即打印视图)中打开 PDF。
就好像我们是从 iText 迁移到 PDFBox 一样,下面是 iText 的现有代码。
使用下面的代码,有 PDFAction 类来实现相同的。是的,
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
并在文档上应用打印 Javascript,
copy.addJavaScript(action);
我需要与 PDFBox 等效的解决方案。
Document document = new Document();
try{
outputStream=response.getOutputStream();
// step 2
PdfCopy copy = new PdfCopy(document, outputStream);
// step 3
document.open();
// step 4
PdfReader reader;
int n;
//add print dialog in Pdf Action to open file for preview.
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
// loop over the documents you want to concatenate
Iterator i=mergepdfFileList.iterator();
while(i.hasNext()){
File f =new File((String)i.next());
is=new FileInputStream(f);
reader=new PdfReader(is);
n = reader.getNumberOfPages();
for (int page = 0; page < n; ) {
copy.addPage(copy.getImportedPage(reader, ++page));
}
copy.freeReader(reader);
reader.close();
is.close();
}
copy.addJavaScript(action);
// step 5
document.close();
}catch(IOException io){
throw io;
}catch(DocumentException e){
throw e;
}catch(Exception e){
throw e;
}finally{
outputStream.close();
}
我也尝试了以下参考,但找不到 PDDocument 类型的 print() 方法。
请指导我。
【问题讨论】:
-
你能分享你用 itext 得到的结果 PDF 吗?
-
@TilmanHausherr 添加了有问题的文件视图。此外,这是该文件的链接:drive.google.com/file/d/1UShCgqx1Kyx40C7v9ehU-KdMFnYxlKWn/…
-
是的,我需要文件本身。屏幕截图没有“打印”字段,所以我想知道我是否误解了这个问题。
-
顺便说一句:在与 iText 相关的代码中,您使用
PdfCopy,而PdfStamper会更经济地创建更忠实的结果。