pdf - 如何在 iText7 中使用文本超链接创建 PDF/UA
问题描述
我正在尝试创建一个 PDF/UA 兼容文件,其中包含带有 iText 7 的文本超链接。PDF/UA 的 Acrobat Preflight 测试和 PDF 辅助功能检查器 (PAC 3) 都抱怨 PDF 文件说 PDF 不兼容.
PAC 3 说“链接”注释没有嵌套在“链接”结构元素中,并且 Acrobat Preflight 测试表明链接注释在 Contents 键中没有替代描述。以下是我尝试创建包含文本超链接的 PDF/UA 兼容输出。
任何意见,将不胜感激。
public void testHyperLink() throws IOException {
// Create PDF/UA with text hyperlink
String filename = "./results/HyperLink.pdf";
WriterProperties properties = new WriterProperties();
properties.addUAXmpMetadata().setPdfVersion(PdfVersion.PDF_1_7);
PdfWriter writer = new PdfWriter(filename, properties);
pdfDoc = new PdfDocument(writer);
//Make document tagged
pdfDoc.setTagged();
pdfDoc.getCatalog().setLang(new PdfString("en-US"));
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdfDoc.getDocumentInfo();
info.setTitle("Hello Hyperlinks!");
document = new Document(pdfDoc);
// Must embed font for PDF/UA
byte[] inputBytes = Files.readAllBytes(Paths.get("./resources/fonts/opensans-regular.ttf"));
boolean embedded = true;
boolean cached = false;
PdfFont font = PdfFontFactory.createFont(inputBytes, PdfEncodings.CP1252, embedded, cached);
Text text = new Text("This is a Text link");
text.setFont(font);
text.setFontSize(16F);
// Add alternate text for hyperlink
text.getAccessibilityProperties().setAlternateDescription("Click here to go to the iText website");
PdfAction act = PdfAction.createURI("https://itextpdf.com/");
text.setAction(act);
Paragraph para = new Paragraph();
para.add(text);
document.add(para);
document.close();
System.out.println("Created "+ filename);
}
解决方案
Link 对象可能是您想要的:
Link lnk = new Link("This is a Text link",
PdfAction.CreateURI("https://itextpdf.com/"));
lnk.SetFont(font);
lnk.GetLinkAnnotation().SetBorder(new PdfAnnotationBorder(0, 0, 0));//Remove the default border
lnk.GetAccessibilityProperties().SetAlternateDescription("Click here to go to the iText website");
Paragraph para = new Paragraph();
para.Add(lnk);
document.Add(para);
推荐阅读
- wordpress - WooCommerce 页面显示为空白
- angularjs - Angularjs:使用haml语法添加多个ng_if条件
- asp.net-mvc - 为什么 Ajax 在每次调用时都请求重复的 Kendo DropDownList k-list-container?
- ansible - 错误 404:在 Gitlab 中找不到文件
- javascript - 使用带有 SpeechSynthesisUtterance 的 java 脚本的泰米尔语语音响应?
- ruby-on-rails - Ruby on Rails - 如何让非登录用户在我的帖子上留言?
- c - 未定义对 regcomp 的引用
- sql - 比较来自 2 个不同 DBMS 的两个表
- java - Spring Boot异步基于接口的控制器不起作用
- c++ - Pybind - 奇怪地重复出现的模板