我想知道是否有办法创建链接(使用 HTML)来打开嵌入在 pdf 文档中的附件。
类似这样的事情...
<a href="attachment.pdf">Open the attachment file</a> or <a href="file:///attachment.pdf">Open the attachment file</a>
有什么建议或推荐吗?
谢谢
最佳答案
在阅读下一篇有用的文章后,我已经能够实现这个场景
https://groups.google.com/forum/#!topic/flying-saucer-users/KuwPoTjaQYU
步骤:
- 查看下一个存储库 https://github.com/osnard/flyingsaucer
- 分析文件 ITextOutputDevice.java ,方法 processLink(RenderingContext c, Box box)
- 根据您的需求进行更改
- 构建项目并使用生成的 jar 文件
这里是基于 Base64 内容创建嵌入文件的代码。
Java
...String doEmbedFile = handler.getAttributeValue( elem, "data-fs-embed-file" );
if ("true".equals(doEmbedFile.toLowerCase())) {
String fileName = new File(uri).getName();
String content = handler.getAttributeValue( elem, "content" );
com.lowagie.text.Rectangle targetArea = checkLinkArea(c, box);
if (targetArea == null) {
return;
}
try {
if (!_attachments.contains(fileName)) {
byte[] fileBytes = Base64.getDecoder().decode(content.getBytes("UTF-8"));
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(_writer, null, fileName, fileBytes);
fs.addDescription(fileName, true);
_writer.addFileAttachment(fs);
_attachments.add(fileName);
}
targetArea.setBorder(0);
targetArea.setBorderWidth(0);
//This only works on Adobe Acrobat Reader
PdfAction action = PdfAction.javaScript(
"this.exportDataObject({cName:\"" + fileName + "\", nLaunch:2});",
_writer
);...
HTML
<body><div id='div1'><p><a href='test.png' data-fs-embed-file='true' content='iVBORw0KGgoAAAANSUhEU...'>open test.png file</a></p></div><div id='div2'><p><a href='test.pdf' data-fs-embed-file='true' content='JVBERi0xLjUNCiW1tbW1D...'>open test.pdf file</a></p></div><div id='div3'><p><a href='test.txt' data-fs-embed-file='true' content='VEVFRUVFRUVFRVNUIFRYVA=='>open test.txt file</a></p></div></body>
*base64 内容被截断
输出
问候,我希望它可以帮助其他人
关于html - Flying Saucer - 从 pdf 链接打开附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48258914/