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

Add titles as H1 in accessible pdf using iText 7 and C#

iText5中,我们可以使用章节来添加标题和书签。
然后标题将在可访问的 PDF 中显示为 H1 标签。
我如何在 iText7 中做到这一点?

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

使用 Adob​​e 检查标签 Reader 确认应用了正确的标签。