IText 5.5.3. 508 Compliance with JAWS 16 - java

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

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

Related

iText 5 - How to set cells of a table in different sizes using iText 5 in Java

I am writing a Java code to generate a PDF template. In the pdf's header section, I have created a pdfTable which has 7 cells including an image cell (Logo), a text field (Id Number) and remaining 5 cells to populate the actual Id Number.
In the output, I should get a bigger image cell (representing the Logo) and Id number cells must be smaller in size than the image cell. Example, as in the below image (Expected Result).
However when the template is generated, I am unable populate as expected as shown in the above image (Expected Result).
When the PDF is generated, all the cells are taking the size of the image cell.
I have tried with different approaches like setting column widhts, setFixedHeight(), setRowSpan(), setColumnSpan() methods etc. But nothing worked. Below image shows my output (Current Output).
Below is code which I have written.
public class NbaBafTemplateGenerator {
private void createNbaBafTemplate(File outPutfileName, NbaBafTemplateData formData,String logoName) {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPutfileName));
document.open();
PdfPTable table = null;
// Passing the data as a single String
//IdNumber varible is of type String and has 5 characters of the number.
table = NbaBafTemplatePage.createHeaderTable(logoName + ",Id Number: , " +
formData.getIdNumber(), 7, "", "","1.5f, 1f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f");
document.add(table);
}// END OF CLASS NbaBafTemplateGenerator.
//Class NbaBafTemplatePage Begins.
public class NbaBafTemplatePage extends PdfPageEventHelper {
public static PdfPTable createHeaderTable(String text, int columnCount, String colour, String align, String colSize)
throws DocumentException, IOException {
PdfPTable table = null;
table = new PdfPTable(columnCount); // 7 columns.
table.setWidthPercentage(100); // Width 100%
table.setSpacingBefore(0f); /
table.setSpacingAfter(10f);
//Assigning column widths based on input width params.
float[] tablecolumnWidths = {
Float.parseFloat(colSize.split(",")[0]),
Float.parseFloat(colSize.split(",")[1]),
Float.parseFloat(colSize.split(",")[2]),
Float.parseFloat(colSize.split(",")[3]),
Float.parseFloat(colSize.split(",")[4]),
Float.parseFloat(colSize.split(",")[5]),
Float.parseFloat(colSize.split(",")[6])};
PdfPCell imgCell = new PdfPCell(createImageCell(text.split(",")[0]));
//imgCell.setColspan(3);
//imgCell.setRowspan(3);
imgCell.setBorder(PdfPCell.NO_BORDER);
imgCell.setHorizontalAlignment(Element.ALIGN_LEFT);
imgCell.setVerticalAlignment(Element.ALIGN_LEFT);
table.addCell(imgCell);
PdfPCell idCell = new PdfPCell(new Paragraph(text.split(",")[1]));
idCell.setBorderColor(BaseColor.BLACK);
idCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
idCell.setPaddingLeft(10);
idCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
idCell.setVerticalAlignment(Element.ALIGN_RIGHT);
table.addCell(idCell);
PdfPCell cellC0 = new PdfPCell(new Paragraph(text.split(",")[2]));
cellC0.setBorderColor(BaseColor.BLACK);
cellC0.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC0.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC0);
PdfPCell cellC1 = new PdfPCell(new Paragraph(text.split(",")[3]));
cellC1.setBorderColor(BaseColor.BLACK);
cellC1.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC1);
PdfPCell cellC2 = new PdfPCell(new Paragraph(text.split(",")[4]));
cellC2.setBorderColor(BaseColor.BLACK);
cellC2.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC2);
PdfPCell cellC3 = new PdfPCell(new Paragraph(text.split(",")[5]));
cellC3.setBorderColor(BaseColor.BLACK);
cellC3.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC3.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC3);
PdfPCell cellC4 = new PdfPCell(new Paragraph(text.split(",")[6]));
cellC4.setBorderColor(BaseColor.BLACK);
cellC4.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC4.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC4);
return table;
}//END OF METHOD createHeaderTable.
public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
Image img = Image.getInstance(path);
PdfPCell cell = new PdfPCell(img, true);
return cell;
}
}
I am using Java and iText 5.x version.
Can anyone please let me know how to generate the pdf table with different cell sizes.
You can create a table with 2 rows (13 cells). Set the column-span of image cell equal to 2. Keep the remaining cells of the first row as blank, set their borders as 0 (invisible), and adjust their heights (cells of the first row) as per your required alignment.
Then add the remaining 6 cells to the table as a second row. Adjust column widths as per your requirement. Hope this helps.
PdfPTable table = new PdfPTable(7);
table.setWidthPercentage(100);
PdfPCell imageCell = new PdfPCell(image);
imageCell.setBorder(0);
imageCell.setRowspan(2);
table.addCell(imageCell);
for(int i=0; i<6;i++) {
PdfPCell blankCell = new PdfPCell();
blankCell.setBorder(0);
blankCell.setFixedHeight(20f);
table.addCell(blankCell);
}
PdfPCell cell22 = new PdfPCell(new Phrase("ID Number"));
table.addCell(cell22);
PdfPCell cell23 = new PdfPCell(new Phrase("9"));
table.addCell(cell23);
PdfPCell cell24 = new PdfPCell(new Phrase("6"));
table.addCell(cell24);
PdfPCell cell25 = new PdfPCell(new Phrase("0"));
table.addCell(cell25);
PdfPCell cell26 = new PdfPCell(new Phrase("5"));
table.addCell(cell26);
PdfPCell cell27 = new PdfPCell(new Phrase("1"));
table.addCell(cell27);
document.add(table);

iText - image breaks cell alignment [duplicate]

This question already has answers here:
Right aligning text in PdfPCell
(6 answers)
Closed 7 years ago.
I have a table where in the first row there are only images and in the second row there are only descriptions of the images. I handle this by creating a table with the size (columns) of the amount of images and then filling the cells with tables with size 1 (2 rows = 1st row the image, 2nd row the description). After setting cell alignment of the 1st row to center, the second row will not apply the align and the description stays on the left ... is this a bug?
Integer size = filepathArray.length;
PdfPTable pdfPTable = new PdfPTable(size);
for (int i = 0; i < size; i++) {
PdfPTable inner = new PdfPTable(1);
try {
PdfPCell image = new PdfPCell();
PdfPCell description = new PdfPCell();
PdfPCell cell = new PdfPCell();
image.setImage(Image.getInstance(getImageAsByteArray(filepathArray[i])));
image.setFixedHeight(32);
image.setBorder(Rectangle.NO_BORDER);
image.setHorizontalAlignment(Element.ALIGN_CENTER);
inner.addCell(image);
description.addElement(new Chunk(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setBorder(Rectangle.NO_BORDER);
description.setHorizontalAlignment(Element.ALIGN_CENTER);
inner.addCell(description);
cell = new PdfPCell();
cell.addElement(inner); // needed to actually remove the border from the cell which contains the inner table because tables have no setter for the border
cell.setBorder(Rectangle.NO_BORDER);
pdfPTable.addCell(cell);
} catch (Exception e) {
}
}
pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT);
Result: the image is centered, the text is not, no way, I've tried everything! Also addElement() removes all previously set alignments (table and cell elements, is this a bug?) so I have to set the alignment AFTER I added the content to the cell or table.
This is wrong:
PdfPCell description = new PdfPCell();
description.addElement(new Chunk(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setHorizontalAlignment(Element.ALIGN_CENTER);
It is wrong because you are mixing text mode:
PdfPCell description = new PdfPCell(new Phrase(filepathArray[i], FontFactory.getFont("Arial", 8)));
description.setHorizontalAlignment(Element.ALIGN_CENTER);
With composite mode:
PdfPCell description = new PdfPCell();
Paragraph p = new Paragraph(filepathArray[i], FontFactory.getFont("Arial", 8));
p.setAlignment(Element.ALIGN_CENTER);
description.addElement(p);
Seems like you have tried everything but using the approaches that are explained in the documentation ;-)

How to get the Content added to the cell that doesn't fit the height of the cell?

Working with iText and using table cells.
I have 25 cells (columns) in a table.
The content of each column is rotated 90 degree and is required to be fitted to the height of each cell.
In some cases when the length of the content exceeds the height of the cell, not all the content is visible (Only the part of content is visible that is fitted to the height of the cell, the rest is dropped). I want to get that dropped content and want to show it in the next adjacent cell.
The following code is used -
PdfPCell cell;
for(int i = 0;i< 25;i++)
{
if(locs.get(i) == null)
{
cell = new PdfPCell();
cell.setBorder(0);
table.addCell(cell);
}
else
{
Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD, BaseColor.BLACK);
cell = new PdfPCell(new Phrase(locs.get(i), font));
cell.setRotation(90);
cell.setBorder(0);
cell.setFixedHeight(110f);
//cell.setMinimumHeight(10f);
table.addCell(cell);
}
}
So if the value of locs.get(i) is greater then the height of the cell (cell height is fixed to 110f in the above code), some content that doesn't fit gets dropped to be shown in the view.
How to get that content and show it to the adjacent cell ?
The method used that solved the purpose is the following:
PdfPCell cell;
for(int i = 0;i< 25;i++)
{
if(locs.get(i) != null)
{
Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD, BaseColor.BLACK);
cell = new PdfPCell(new Phrase(locs.get(i), font));
cell.setRotation(90);
cell.setBorder(0);
cell.setFixedHeight(110f);
cell.setColspan(2);
table.addCell(cell);
}
else
{
cell = new PdfPCell();
cell.setBorder(0);
table.addCell(cell);
}
}
So setting the colspan to 2 ensures that if the contents exceeds the length of the first column then move the remaining contents to the next column of the cell (One cell having two columns now after adding the colspan of 2).
If anyone knows the better way to do the same thing then you are welcome!

Edges do not match colspan table itext

I have done
business with iText, I
created a table with 9
columns, I have grouped the
first 4 to 1, then I have the
next 4 grouped in one,
staying one at the end, the
point is assigned
cell.setBorderwidth = 2 in
all cells, to generate the
document do not match
edges.
Hope you can help
me.
Here is code used:
public PdfPTable createTable5() throws DocumentException {
PdfPTable table = new PdfPTable(10);
table.setTotalWidth(540);
//table.setWidthPercentage(105);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setLockedWidth(true);
table.setWidths(new float[]{26.16f,48.77f,48.77f,48.77f,40.06f,56.64f,78.41f,53.1f,59.2f,80.11f});
PdfPCell cell;
cell = new PdfPCell(new Phrase(" ",subFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setBorderWidth(0);
//cell.setBorderWidthRight(0);
//cell.setColspan(4);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cantidades por material",subFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
cell.setBorderWidth(2);
cell.setBorderWidthRight(0);
cell.setColspan(4);
cell.setFixedHeight(g);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Costo por material",subFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
cell.setBorderWidth(2);
cell.setBorderWidthRight(0);
cell.setColspan(4);
cell.setFixedHeight(g);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Total",subFont));// Error in Border
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
cell.setBorderWidth(2);
//cell.setColspan(1);
cell.setFixedHeight(g);
//cell.setBorderWidthRight(0);
table.addCell(cell);
return table;
}
Try setUseBorderPadding(true) for each cell.

A new row in pdfptable

I am using iText library to print certain data in table format in a pdf file. I have 11 columns and can have multiple rows. After creating header for titles of each column, how do I create a new row in pdfptable so that I can print the real data on a separate row.
In the constructor of PdfPTable you specify the number of columns in a row.
PdfPTable table = new PdfPTable(4) // 4 Columns
PdfPCell cell;
cell = new PdfPCell( new Phrase("Cell with colspan of 4") ) ;
cell. setColspan(4) ; // an entire row
anotherCell = new PdfPCell( new Phrase("Cell with colspan of 4") );
anotherCell.setColspan(4); // a second row
As you can see, a new row is created when you reach the colspan in the current row.

Categories