提问者:小点点

如何创建PDF/UA在iText7与文本超链接


我正在尝试创建一个PDF/UA兼容的文件,其中包含带有iText 7的文本超链接。AcrobatPDF/UA预检和PDF可访问性检查器(PAC 3)都抱怨PDF文件说PDF不兼容。

PAC 3表示""Link"注释没有嵌套在"Link"结构元素中",Acrobat飞行前测试表示Link注释在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);
}

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