如何在iText7中使用文本超链接创建PDF/UA [英] How to create PDF/UA in iText7 with text hyperlink

查看:242
本文介绍了如何在iText7中使用文本超链接创建PDF/UA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个与PDF/UA兼容的文件,其中包含带有iText 7的文本超链接.PDF/UA的Acrobat Preflight测试和PDF Accessibility Checker(PAC 3)都抱怨该PDF文件说该PDF不符合要求.

I am trying to create a PDF/UA compliant file that contains a text hyperlink with iText 7. Both the Acrobat Preflight test for PDF/UA and the PDF Accessibility Checker (PAC 3) complain that the PDF file say that the PDF is not compliant.

PAC 3会说" Link".注释没有嵌套在链接"链接中.结构元素"并且Acrobat Preflight测试表明链接"注释在内容"键中没有替代描述.以下是我创建包含文本超链接的PDF/UA兼容输出的尝试.

PAC 3 says ""Link" annotation is not nested inside a "Link" structure element" and the Acrobat Preflight test says the Link annotation does not have an alternate description in the Contents key. The following is my attempt to create PDF/UA compliant output that contains a text hyperlink.

任何建议将不胜感激.

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对象:

A Link object might be what you want:

            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);

这篇关于如何在iText7中使用文本超链接创建PDF/UA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆