I am creating a PDF using iText 5.5.6.
I wanted to tag table, row and cell as a DIV.
I have written the following code but only the table gets marked as a DIV and not the row and cell.
PdfPTable table = new PdfPTable(2);
table.setRunDirection(PdfAWriter.RUN_DIRECTION_LTR);
table.setRole(PdfName.DIV);
table.setWidthPercentage(80);
table.setWidths(new float[] { 0.8f, 1.2f });
table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
table.setSpacingBefore(10);
table.setSpacingAfter(15);
Paragraph p3 = new Paragraph("Name:", font12);
Paragraph p4 = new Paragraph("FirstName LastName", font12);
Paragraph p5 = new Paragraph("DOB: ", font12);
Paragraph p6 = new Paragraph("00-00-0000", font12);
PdfPCell cellp3 = new PdfPCell(p3);
cellp3.setBorder(0);
PdfPCell cellp4 = new PdfPCell(p4);
cellp4.setBorder(0);
cellp3.setRole(PdfName.DIV);
cellp4.setRole(PdfName.DIV);
table.addCell(cellp3);
table.addCell(cellp4);
PdfPCell cellp5 = new PdfPCell(p5);
cellp5.setBorder(0);
PdfPCell cellp6 = new PdfPCell(p6);
cellp6.setBorder(0);
cellp5.setRole(PdfName.DIV);
cellp6.setRole(PdfName.DIV);
table.addCell(cellp5);
table.addCell(cellp6);
How to solve this?
If I test your code the table and all the cells are tagged as DIV. The rows are indeed not.
In iText the classes PdfPTable, PdfPRow and PdfPCell are responsible for table functionality. PdfPRow is typically only used in the iText implementation internally, because a PdfPTable instance accepts PdfPCells directly.
But you are able to retrieve the table rows to set their role. After you have added all the cells to the table, do this:
for (PdfPRow row : table.getRows())
row.setRole(PdfName.DIV);
Related
I want to keep two cells of a table keep together. So as per below code i gave cell as keep together.
I gave here example for two inner tables actually there are more.
So if both heading and content cell are not able to fit in the page then it starts from next page. But requirement is part of page should not be blank and heading and content should always be together. If content size is more then only content may split.
Table outerTable =new Table(1);
Table innerTable1 =new Table(1);
String content="";/*Large content from a text file*/
Cell cell1 = new Cell().add(new Paragraph("Heading1"));
Cell cell2 = new Cell().add(new Paragraph(content));
innerTable1.add(cell1);
innerTable1.add(cell2);
Cell cell =new Cell().add(innerTable1).setKeepTogether(true);
outerTable.add(cell);
Table innerTable2 =new Table(1);
cell1 = new Cell().add(new Paragraph("Heading2"));
cell2 = new Cell().add(new Paragraph(content));
innerTable2.add(cell1);
innerTable2.add(cell2);
cell =new Cell().add(innerTable2).setKeepTogether(true);
outerTable.add(cell);
I wanted to insert a Table in the position of the form field. How do I get the position and page number of the form field? How do I insert the table in that location?
//Retrieves AcroForm from the document.
//If there is no AcroForm in the document Catalog and createIfNotExist
//flag is true then the AcroForm dictionary will be created and added to the document.
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDoc, true);
//Gets the form fields as a Map.
Map<String, PdfFormField> fields = acroForm.getFormFields();
//Create Table
Table table = new Table(new float[]{1,1});
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.setWidthPercent(95);
Cell cell = new Cell();
cell.add(new Paragraph("Name"));
//cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
Cell cell = new Cell();
cell.add(new Paragraph("Address"));
//cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
...
...
doc.add(table);
...
Field by itself does not have any position or page number. However, its widget annotations do.
To access those, you can use field.getWidgets(). You then can use annotation.getPage() and annotation.getRectangle() to get information about the annotation's position.
To layout an single element at some specific position, one of the best choices is using Canvas layout object. Annotation can then be removed with page.removeAnnotation(annotation);.
Overall, this compiles into following solution:
String fieldName = "Text1";
PdfFormField field = form.getField(fieldName);
for (PdfAnnotation annotation : field.getWidgets()) {
PdfPage page = annotation.getPage();
Rectangle rectangle = annotation.getRectangle().toRectangle();
Table table = new Table(UnitValue.createPointArray(new float[] {-1, -1}));
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
Cell cell = new Cell();
cell.add(new Paragraph("Name"));
table.addCell(cell);
cell = new Cell();
cell.add(new Paragraph("Address"));
table.addCell(cell);
Canvas canvas = new Canvas(new PdfCanvas(page), pdfDocument, rectangle);
canvas.add(table);
canvas.close();
page.removeAnnotation(annotation);
}
Of course, you will have to take full responsibility for creating a table of proper size so that it fits into the area you want. You can use smaller font sizes etc.
I'm attempting to generate PDFs for college transcripts using itext 5.5.9. I've decided on using PdfPTable for controlling the layout. The beginning of the document includes an overview section with the following lines:
Name:
Date of Birth:
Major:
Minor:
Right now the output adds more space between these lines than desired. (It looks as if the lines are double-spaced.) Here are code snippets that seem relevant:
PdfPCell overviewCell = new PdfPCell();
//other codes here...
overviewCell.setPadding(0f);;
overviewCell.setLeading(0f, 0f);
overviewCell.addElement(new Phrase("Name:", normalFont));
overviewCell.addElement(Chunk.NEWLINE);
overviewCell.addElement(new Phrase("Date of Birth:", normalFont));
overviewCell.addElement(Chunk.NEWLINE);
overviewCell.addElement(Chunk.NEWLINE);
overviewCell.addElement(new Phrase("Major:", normalFont));
I also tried obtaining the PdfWriter's ColumnText (ct), and working with that object directly before calling overviewCell.setColumn(ct) but that didn't fix the spacing either.
Please let me know how do control the text's line spacing within the cell.
Thanks!
Why don't you just add create a sub table and add it to the overview cell with your desired amount. This will handle a lot of the formatting for you.
private static final String _name = "Name:";
private static final String _dateOfBirth = "Date Of Birth:";
private static final String _major = "Major:";
PdfPTable table = new PdfPTable(2);
PdfPCell spaceCell = new PdfPCell(new Phrase(" ", normalFont));
PdfPCell nameCell = new PdfPCell(new Phrase(_name, normalFont));
PdfPCell dateOfBirthCell = new PdfPCell(new Phrase(_dateOfBirth, normalFont));
PdfPcell majorCell = new PdfPCell(new Phrase(_major, normalFont));
table.addCell(nameCell);
table.addCell(spaceCell);
table.addCell(dateOfBirthCell);
table.addCell(spaceCell);
table.addCell(spaceCell); // if you want an extra line break here a fill a line
table.addCell(spaceCell); // with space cells
table.addCell(majorCell);
overviewCell.addElement(table);
I have table in itext.
I need to change a specific cell width in the last row.
Is it possible?
edit: colspan does not cover my problem. I need completely different widths in this row.
There is many ways to change your last row cell width :
A. With colspan, of course you'll need cells to fit with other rows.
Example :
PdfPTable table = new PdfPTable(3);
table.addCell("cell A0");
table.addCell("cell A1");
table.addCell("cell A2");
table.addCell("cell B0");
table.addCell("cell B1");
table.addCell("cell B2");
PdfPCell cell = new PdfPCell();
cell.setColspan(2);
cell.addElement(new Phrase("C0 & C1"));
table.addCell(cell);
table.addCell("cell C2");
B. Two tables with the same total width, no space between tables.
Example :
PdfPTable table = new PdfPTable(1);
PdfPTable table1 = new PdfPTable(3);
table1.setWidthPercentage(100f);
table1.addCell("cell A0");
table1.addCell("cell A1");
table1.addCell("cell A2");
PdfPTable table2 = new PdfPTable(2);
table2.setWidthPercentage(100f);
table2.addCell("cell B0");
table2.addCell("cell B1");
table.addCell(table1);
table.addCell(table2);
I am using Java with iText in order to generate some PDFs. I need to put text in columns, so I am trying to use PdfPTable. I create it with:
myTable = new PdfPTable(n);
n being the number of columns. The problem is that PdfPTable fills the table row by row, that is, you first give the cell in column 1 of row 1, then column 2 of row 1, and so on, but I need to do it column by column, because that is how the data is being fed to me.
I would use a Table (which lets you specify the position) like in http://stderr.org/doc/libitext-java-doc/www/tutorial/ch05.html, but I get a "could not resolve to a type", and my Eclipse can't find the proper import.
Edit: in case my previous explanation was confusing, what I want is to fill the table in this order:
1 3 5
2 4 6
Instead of this:
1 2 3
4 5 6
Here is one way: Create a PdfPTable with the number of columns desired, in your case 3. For each iteration through your data create a PdfPTable with 1 column. Create 2 PdfPCell objects, one containing the data element you are currently on and the other containing the next value in your data. So now you have a PdfPTable with 1 column and two rows. Add this PdfPTable to the PdfPTable that has 3 columns. Continue that until you've printed all your data. Better explained with code:
public class Clazz {
public static final String RESULT = "result.pdf";
private String [] data = {"1", "2", "3", "4", "5", "6"};
private void go() throws Exception {
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
doc.open();
PdfPTable mainTable = new PdfPTable(3);
PdfPCell cell;
for (int i = 0; i < data.length; i+=2) {
cell = new PdfPCell(new Phrase(data[i]));
PdfPTable table = new PdfPTable(1);
table.addCell(cell);
if (i+1 <= data.length -1) {
cell = new PdfPCell(new Phrase(data[i + 1]));
table.addCell(cell);
} else {
cell = new PdfPCell(new Phrase(""));
table.addCell(cell);
}
mainTable.addCell(table);
}
doc.add(mainTable);
doc.close();
}
}
One way could be creating inner table of column no = 1 for each main column and add that into a main table.
private static PdfPTable writeColumnWise(String[] data, int noOfColumns, int noOfRows) {
PdfPTable table = new PdfPTable(noOfColumns);
PdfPTable columnTable = new PdfPTable(1);
columnTable.getDefaultCell().setBorderWidth(0.0f);
columnTable.getDefaultCell().setPadding(0.0f);
for(int i=0; i<data.length; i++){
if( i != 0 && i % noOfRows == 0 ){
// add columnTable into main table
table.addCell(columnTable);
//re initialize columnTable for next column
columnTable = new PdfPTable(1);
columnTable.getDefaultCell().setBorderWidth(0.0f);
columnTable.getDefaultCell().setPadding(0.0f);
}
PdfPCell cell = new PdfPCell(new Paragraph(data[i]));
columnTable.addCell(cell);
}
// add columnTable for last column into main table
table.addCell(columnTable);
return table;
}