使用iText 7和C#将标题添加为可访问的pdf中的H1

时间:2018-06-04 07:34:42

标签: c# itext7 accessible

iText5中,我们可以使用章节和章节来添加标题和书签 然后,标题将在可访问的PDF中显示为H1标记 我如何在iText7中执行此操作?

1 个答案:

答案 0 :(得分:7)

在iText7中,您可以这样做:

@Test
public void run() throws IOException {

    File outputFile = getOutputFile();

    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outputFile));
    pdfDocument.setTagged();

    Document layoutDocument = new Document(pdfDocument);

    Paragraph para = new Paragraph("The Raven")
                        .setFontColor(new DeviceRgb(8, 73, 117))
                        .setFontSize(20f);
                        para.getAccessibilityProperties().setRole(StandardRoles.H1);
    layoutDocument.add(para);

    layoutDocument.add(new Paragraph("Once upon a midnight dreary\nWhile I pondered weak and weary\nOver many a quaint and curious volume\nOf forgotten lore"));

    pdfDocument.close();

    Desktop.getDesktop().open(outputFile);
}

使用Adobe Reader检查标签可验证是否已应用正确的标记。

enter image description here