如何在 pdfbox 中使用 PDActionRemoteGoTo class 添加 /D[0/Fit] 命令到 pdf?

How to add /D[0/Fit] command to pdf using PDActionRemoteGoTo class in pdfbox?

我已经实现了使用 pdfbox 添加 link 到 pdf 文件的功能。下面是我正在使用的代码:

 PDAnnotationLink txtLink = new PDAnnotationLink();
 PDActionRemoteGoTo remoteGoto = new PDActionRemoteGoTo();
 PDComplexFileSpecification fileDesc = new PDComplexFileSpecification();
 file.setExecutable(true);
 fileDesc.setFile(System.IO.Path.GetFileName(filePath));
 fileDesc.setFileUnicode(System.IO.Path.GetFileName(filePath));
 fileDesc.setFileDescription("");
 remoteGoto.setOpenInNewWindow(true);
 remoteGoto.setFile(fileDesc);
 txtLink.setAction(remoteGoto);
 txtLink.setRectangle(rect);
 page.getAnnotations().add(txtLink); 

生成的 link 可以在基于 windows 的 pdf 编辑器中使用,但不能在 ios 的 pdfviewer 中使用。使用上面的代码在pdf中输入的命令是:

12 0 obj
<<
/Type /Annot
/Subtype /Link
/A 17 0 R
/Rect [1578.599 316.56006 1600.6244 326.62427]
>>
endobj
17 0 obj
<<
/S /GoToR
/NewWindow true
/F 20 0 R
>>
endobj
20 0 obj
<<
/Type /Filespec
/F (A201NORTHSOUTHSITEELEVATIONS.pdf)
/UF (A201NORTHSOUTHSITEELEVATIONS.pdf)
/Desc ()
>>
endobj

如果我使用记事本手动将 /D[0/Fit] 命令添加到 /S /GoToR,则 link 有效。 D=那么如何使用 pdfbox 库添加它。

这样做:

PDPageDestination dest = new PDPageFitDestination();
dest.setPageNumber(0);
remoteGoto.setD(dest.getCOSObject());

或者,只需创建一个 COSArray 并添加 0 和 COSName.getPDFName("Fit") 并将其传递给 setD()