ayo alatishe ayo alatishe - 1 month ago 14
Java Question

IText 5.5.3. 508 Compliance with JAWS 16

I am having issues printing 508 compliant Tagged PDF files that has Tables in them. For some reason, JAWS doesn't read the accessibility text for a PdfPTable.

In the below code snippet, JAWS reads the ALT text for the Chunk but it doesn't read for the PdfPCell and PdfPTable. Please Help!

/**
* 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;
}

Answer

Wow! 508 can be really frustrating. After a couple of days, i was able to figure out a way out. I stumbled on the method: createTaggedPDF17(). I created a PdfPHeaderSet for the first row and added an header for each of the cells in the table body.

It worked perfectly! JAWS read the alternate text just the way i wanted it.

I dont know why it didnt work before. But this solved my problem.

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