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);
Related
Please check the code here. Facing issue to merge 2 PDFPtable top to bottom.
PdfPTable table = new PdfPTable(new float[] { 1.0f, 2.0f, 5.0f, 1.0f, 2.0f });
table.setWidthPercentage(100.0f);
ArrayList<String> lHeaders = new ArrayList<>();
lHeaders.add("S. No.");
lHeaders.add("Course Code");
lHeaders.add("Course Name");
lHeaders.add("Credit");
lHeaders.add("Letter Grade");
ArrayList<String> lData = new ArrayList<>();
lData.add("1");
lData.add("D 210");
lData.add("COMPUTER & INFORMATION TECHNOLOGY FUNDAMENTAL LAB-II");
lData.add("1.0");
lData.add("BB");
// Create and add a title across both columns.
Font headerfont = new Font(Font.TIMES_ROMAN, 15, Font.BOLD);
// PdfPCell cell = new PdfPCell (new Paragraph ("New Mustang
// Features"));
PdfPCell cell;
for (String i : lHeaders) {
cell = new PdfPCell(new Paragraph(i, headerfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(new Color(255, 0, 0));
// cell.setPadding (10.0f);
table.addCell(cell);
}
// Add header cells for these columns.
// cell = new PdfPCell (new Paragraph ("Feature"));
// PdfPCell cell1;
Font datafont = new Font(Font.HELVETICA, 10, Font.NORMAL);
for (String j : lData) {
cell = new PdfPCell(new Paragraph(j, datafont));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setPadding (10.0f);
table.addCell(cell);
}
Font headerfont2 = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
PdfPTable table2 = new PdfPTable(8);
table2.setWidthPercentage(100.0f);
PdfPCell cell2;
cell2 = new PdfPCell(new Paragraph("CURRENT SEMESTER RECORD", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CUMULATIVE SEMESTER RECORD", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT POINTS", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CREDITS EARNED", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("SGPA", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT POINTS", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CREDITS EARNED", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CGPA", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("Result declared on :", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setPaddingTop(40);
cell2.setBorderWidthLeft(0.5f);
cell2.setBorderWidthBottom(0.5f);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("Controller of Examinations", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingTop(40);
cell2.setBorderWidthRight(0.5f);
cell2.setBorderWidthBottom(0.5f);
cell2.setColspan(4);
table2.addCell(cell2);
I have created two PDFPtable instances one is table and another is table2, i just want to merge these two instances and return from my code. Is it possible? I don't want to add it to the doc from my code and return the document instance. The pdf looks like the attached image.
To summarize, you essentially want to merge those two tables into a single one because your method is designed to return only a single table.
If possible, the best solution would be to change the design of the method and allow it to return multiple tables, as an Iterable (e.g. a List) or as an array.
If that is not possible (in your case returning a single table appears to be a project requirement), a solution would be to create another, single-column table and put your first table into its first row and your second one into the second row. For proper appearances you may want to also adapt table properties for margins and borders accordingly.
Be aware, though, that iText 5 tables were not designed for intense table stacking; having tables inside tables inside tables is likely to cause interesting effects during e.g. page breaks and may be quite resource hungry.
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 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);
I am using the opensource http://itextpdf.com/ , to create some pdf .
I could create a table , but width of all column are same , I need to change the width of the particular column .
PdfPTable table = new PdfPTable(6);
Phrase tablePhrse = new Phrase("Sl n0", normalFontBold);
PdfPCell c1 = new PdfPCell(tablePhrse);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
I could not find any method to set the width of the column , pls suggest some way to do achieve this ..
You can make use of setWidths() method.
table.setWidths(new int[]{200,50});
public void setWidths(int[] relativeWidths)
Try this.
float[] columnWidths = new float[]{10f, 20f, 30f, 10f};
table.setWidths(columnWidths);
Here is the corrected one if table.setWidths(new int[]{200,50}); does not work.
innertable.setWidthPercentage(50);
float[] columnWidths = {1, 5, 5};
// columnWidths = {column1, column2, column3...}
PdfPTable table = new PdfPTable(columnWidths)
Make sure to set table total width
// Set Column Number
PdfPTable table = new PdfPTable(2);
// Set Table Total Width
table.setTotalWidth(500);
// Set Each Column Width - Make Sure Array is the same number specified in constructor
table.setWidths(new int[]{50, 450});
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;
}