IText 5.5.3。 508遵守JAWS 16

时间:2016-10-14 19:10:53

标签: java pdf pdf-generation itext

我在打印508兼容Tagged PDF文件时遇到问题,这些文件中包含表格。出于某种原因,JAWS没有读取PdfPTable的辅助功能文本。

在下面的代码片段中,JAWS读取Chunk的ALT文本,但它没有读取PdfPCell和PdfPTable。请帮助!

    /**
 * Creates a PDF with information about the movies
 * @param    filename the name of the PDF file that will be created.
 * @throws    DocumentException 
 * @throws    IOException
 */
public void createPdf(String filename)
    throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    writer.setPdfVersion(PdfWriter.VERSION_1_7);
    writer.setTagged();
    writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
    document.addLanguage("en-US");
    document.addTitle("Some title");
    writer.createXmpMetadata();
    // step 3
    document.open();
    // step 4
    Paragraph p1 = new Paragraph();
    p1.setFont(FontFactory.getFont(
            FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20));
    Chunk c1 = new Chunk("What is this?");
    c1.setAccessibleAttribute(PdfName.ALT, new PdfString(" Q1. What is this?"));

    p1.add(c1);
    document.add(p1);
    document.add(createFirstTable());
    // step 5
    document.close();
}

/**
 * Creates our first table
 * @return our first table
 */
public static PdfPTable createFirstTable() {
    // a table with three columns
    PdfPTable table = new PdfPTable(3);
    table.setAccessibleAttribute(PdfName.ALT, new PdfString("My Table"));
    // the cell object
    PdfPCell cell;
    // we add a cell with colspan 3
    cell = new PdfPCell(new Phrase("Cell with colspan 3"));
    cell.setAccessibleAttribute(PdfName.TD, new PdfString("Cell 1"));
    cell.setColspan(3);
    table.addCell(cell);
    // now we add a cell with rowspan 2
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
            cell.setRowspan(2);
    table.addCell(cell);
    // we add the four remaining cells with addCell()
    table.addCell("row 1; caa*");
    table.addCell("row 1; c+");
    table.addCell("row 2; c-");
    table.addCell("row 2; c/");
    return table;
}

1 个答案:

答案 0 :(得分:0)

哇! 508真的很令人沮丧。几天后,我找到了出路。我偶然发现了这个方法:createTaggedPDF17()。我为第一行创建了一个PdfPHeaderSet,并为表体中的每个单元格添加了一个标题。

完美无缺! JAWS按照我想要的方式阅读备用文本。

我不知道为什么它之前没有用。但这解决了我的问题。

public static PdfPTable createFirstTable() {
    // a table with three columns
    PdfPTable table = new PdfPTable(3);

    // the cell object
    PdfPCell cell;
    // we add a cell with colspan 3
    //create header cell for first row
    PdfPHeaderCell headerCell = new PdfPHeaderCell();
    headerCell.setScope(PdfPHeaderCell.ROW);
    headerCell.setPhrase(new Phrase("Cell with colspan 3"));
    headerCell.setName("header Cell");
    headerCell.setAccessibleAttribute(PdfName.TD, new PdfString("Cell 1"));
    headerCell.setColspan(3);
    table.addCell(headerCell);
    // now we add a cell with rowspan 2
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));

    //add metadata for each cell in the body.
    cell.addHeader(headerCell);

    cell.setRowspan(2);
    table.addCell(cell);
    // we add the four remaining cells with addCell()
    table.addCell("row 1; caa*");
    table.addCell("row 1; c+");
    table.addCell("row 2; c-");
    table.addCell("row 2; c/");

    //set header row for the table
    table.setHeaderRows(1);

    return table;
}