【问题标题】:How to create PDF/UA in iText7 with text hyperlink如何在 iText7 中使用文本超链接创建 PDF/UA
【发布时间】:2021-06-26 15:24:23
【问题描述】:

我正在尝试使用 iText 7 创建包含文本超链接的 PDF/UA 兼容文件。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);
}

【问题讨论】:

    标签: pdf hyperlink itext7 accessible


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2013-07-10
      • 2016-07-08
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2013-06-18
      相关资源
      最近更新 更多