Splitting rows of Nested Table OR Inner Table using iTextPDF - java

How do I split the rows of the inner table. Problem description according to the image.
Here, I have table named PARENT_TABLE that has cell that contains two tables named HEADER_TABLE and CONTENT_TABLE
CONTENT_TABLE has a cell that contains INNER_TABLE. INNER_TABLE contains the dynamic rows/content whose height will be changed according to content.
So, If I make this table using one page it's working perfectly. But incase if I need to split the table in different pages it shows the error like image_2. Please help me out. As a result: The table should look like in the image_1 even if the page split into two or more pages.
Please checkout mentioned image to be more clear.
image_1
Attempt to read from field 'float com.itextpdf.text.pdf.ColumnText.maxY' on a null object reference
image_2

I just found out the solution. The issue occurs only in the case when trying to split the PDFPTable into two pages. I had used tableCell.setRowspan(2), so when trying to split the table above issue occurs.
SOLVED IT: I just made an empty cell in place of using tableCell.setRowspan(2).
private PdfPTable contentWithDateTitleAndDescription(String stringData1, String stringData2, String stringData3, String stringData4) throws IOException, BadElementException {
float relativeWidth[] = {2, 7};
PdfPTable table = new PdfPTable(relativeWidth);
table.setWidthPercentage(100);
PdfPCell cellA = new PdfPCell();
cellA.setBorder(Rectangle.NO_BORDER);
// dateCell.setRowspan(2);
cellA.setPaddingLeft(15);
cellA.setVerticalAlignment(Element.ALIGN_CENTER);
cellA.setPaddingTop(7);
Paragraph dateParagraph = new Paragraph(stringData1);
cellA.addElement(dateParagraph);
PdfPCell emptyCellB = new PdfPCell();
emptyCellB.setBorder(Rectangle.NO_BORDER);
PdfPCell cellC = new PdfPCell();
cellC.setBorder(Rectangle.NO_BORDER);
cellC.setVerticalAlignment(Element.ALIGN_LEFT);
Paragraph titleParagraph = new Paragraph(stringData2 + " / " + stringData3);
cellC.addElement(titleParagraph);
PdfPCell cellD = new PdfPCell();
cellD.setBorder(Rectangle.NO_BORDER);
cellD.setVerticalAlignment(Element.ALIGN_LEFT);
Paragraph descriptionParagraph = new Paragraph(stringData4);
cellD.addElement(descriptionParagraph);
cellD.setPaddingBottom(15);
table.addCell(cellA);
table.addCell(cellC);
table.addCell(emptyCellB);
table.addCell(cellD);
return table;
}

Related

Nested table docx4j

I need to push a second table in my main table.
I use the main table to format correct the text: I'm creating a class in java for generate Curriculum, so I need a main table to create a good template
I try to push a
Tbl element in a Tc (cell element) of main table, but Word gives me error about a wrong template. It ask me if I want open anyway the document: It show correctly the nested table, but I don't want that the error is displayed.
ObjectFactory factory = Context.getWmlObjectFactory();
Tbl mainTable = TblFactory.createTable(2, 2, cellWidthTwips );
List<Object> rows = table.getContent();
Tr row = (Tr) rows.get(0);
List<Object> cells = row.getContent();
Tc cell = (Tc) cells.get(0);
Tbl nestedTable = TblFactory.createTable(1, 5, widthTips/columns );
cell.getContent().add(nestedTable);
I tried also
Tbl nestedTable2 = factory.createTbl();
Where I wrong?
Assuming MainDocumentPart mdp:
int widthTwips = 4788;
Tbl mainTable = TblFactory.createTable(2, 2, widthTwips);
List<Object> rows = mainTable.getContent();
Tr row = (Tr) rows.get(0);
List<Object> cells = row.getContent();
Tc cell = (Tc) cells.get(0);
Tbl nestedTable = TblFactory.createTable(1, 2, 2000);
// You'll get an error in Word if you don't have an empty <p/> after the nested table.
// Since TblFactory.createTable automatically added an empty <p>, add the table before it
cell.getContent().add(0, nestedTable);
mdp.getContent().add(mainTable);
See the explanatory comment in the code snippet.

iText: Cell with Image doesn't apply Rowspan

I have a table (2 x 2).
If I add a Phrase as cell in the first cell with rowspan 2, it works. But if I use an Image as cell, rowspan never apply.
float[] rowwidths2 = {0.6f,0.4f};
PdfPTable felsoegyharmad = new PdfPTable(rowwidths2);
felsoegyharmad.setWidthPercentage(100);
PdfPCell kepcell = new PdfPCell(new Phrase("image place", fontAlap));
kepcell.setRowspan(2);
felsoegyharmad.addCell(kepcell);
felsoegyharmad.addCell(new Phrase("1", fontAlap));
felsoegyharmad.addCell(new Phrase("2", fontAlap));
Code above is works as advertise.
But this code under is never rowspanned:
PdfPCell kepcell = new PdfPCell();
kepcell.addElement(Image.getInstance(path));
kepcell.setRowspan(2);
felsoegyharmad.addCell(kepcell);
felsoegyharmad.addCell(new Phrase("1", fontAlap));
felsoegyharmad.addCell(new Phrase("2", fontAlap));
How can I put this image into the first column but in height as two rows?
In case you wonder why nobody is answering your question. That's simple: the problem you describe can not be reproduced. I have taken your code snippet, and I have created the following standalone example:
PdfPTable table = new PdfPTable(2);
PdfPCell imageCell = new PdfPCell();
imageCell.addElement(Image.getInstance(IMG));
imageCell.setRowspan(2);
table.addCell(imageCell);
table.addCell(new Phrase("1"));
table.addCell(new Phrase("2"));
For the full source code, see ImageRowspan
The resulting PDF looks like this:
As you can see, the table has two columns and two rows. The first cell (the one with the image) spans two rows. You can download cmp_image_rowspan.pdf from out git repository.

Bottomborder for a subtable cell and for the general pdfPcell of parent table are not making a single straight line in iTextPDF

I have used one parent table that is table_body. One child table that is sub_table(as i would like to place this sub_table inside a PdfPCell in the parent table. ). I want to draw a line when a row is complete for the parent table as per my requirement. But, the issue is bottomborder for the subtable and for the general pdfPcell 'body_cell_bottomBorder' is not getting aligned in a single straight line.
PdfPTable table_body = new PdfPTable(2); // main table
table_body.getDefaultCell().setBorder(Rectangle.NO_BORDER); // set border to none
table_body.setWidthPercentage(100.0f);
table_body.setWidths(new float[] {3.0f,1.0f,});
table_body.setSpacingBefore(6);
PdfPTable sub_table = new PdfPTable(1); // sub table
sub_table.getDefaultCell().setBorder(Rectangle.NO_BORDER); // set border to none
body_cell_bottomBorder.setPhrase(new Phrase("Example",font_body)); // this cell has the bottom border only
Image image = Image.getInstance(BarCode.createBarcode("example"));
body_cell = new PdfPCell(image, true); // this cell has no border at all
body_cell.setBorder(Rectangle.NO_BORDER);
sub_table.addCell(body_cell); // added one row in the sub table
sub_table.addCell(body_cell_bottomBorder); // added second row in the sub table and also want a bottom border
table_body.addCell(sub_table); // added subtable into the parent table pdfpcell
body_cell_bottomBorder.setPhrase(new Phrase(RPL,font_body)); // now adding second column value in parent table pdfPcell and want a bottom border only
table_body.addCell(body_cell_bottomBorder); // added to the parent table
Problem is these two cells of parent table do not make a complete single straight line which i want.
One way:
In the 'child' PdfPTable as each PdfPCell is added, explicitly set Rectangle.NO_BORDER.
When you're done with the 'child' table, wrap in a PdfPCell and set border(s), then add to 'parent' PdfPTable.
Example in C#, since this question is tagged w/itextsharp:
string text = "0000";
int topBottomBorder = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
using (Document document = new Document()) {
PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
document.Open();
PdfPTable parentTable = new PdfPTable(2);
PdfPCell cell = new PdfPCell() {
Phrase = new Phrase(text), Border = topBottomBorder
};
parentTable.AddCell(cell);
// in 'child' PdfPTable *ALL* PdfPCells set *WITH NO* border
PdfPTable childTable = new PdfPTable(1);
childTable.AddCell(new PdfPCell
(
(new BarcodeQRCode(text, 1, 1, null)).GetImage()
)
{ Border = Rectangle.NO_BORDER, PaddingTop = 1 }
);
childTable.AddCell(new PdfPCell() { // row 2
Border = Rectangle.NO_BORDER, Phrase = new Phrase(text)
});
// 1. wrap childTable in PdfPCell and set top & bottom borders
PdfPCell childTableCell = new PdfPCell(childTable) {
Border = topBottomBorder
};
// 2. add to main PdfPTable
parentTable.AddCell(childTableCell);
document.Add(parentTable);
}
Result:

how to create table in pdf document last page bottom using itext in java

I have created table in pdf document using itext in java. Working fine in functionality wise.
But I need to display table as a below of document.
this is my code
PdfPTable datatablebottom = new PdfPTable(8);
PdfPCell cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(8);
cell.setBackgroundColor(BaseColor.GRAY);
cell.setBorderWidthTop(5.0f);
cell.setBorderColorTop(BaseColor.DARK_GRAY);
if(msgfb.equals("1")){
//document.add(new Paragraph(""));
cell.addElement(new Paragraph(""));
}else if(msgfb.equals("2")){
//document.add(new Paragraph("Thank you for your business"));
Paragraph pf = new Paragraph("Thank you for your business Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness",BT_NORMAL);
pf.setAlignment(Element.ALIGN_CENTER);
cell.addElement(pf);
}else{
//document.add(new Paragraph(msgfb));
Paragraph pf = new Paragraph(msgfb,BT_NORMAL);
pf.setAlignment(Element.ALIGN_CENTER);
cell.addElement(pf);
//cell.addElement(new Paragraph(msgfb,BT_NORMAL));
}
cell.setPaddingBottom(10.0f);
datatablebottom.addCell(cell);
datatablebottom.setTotalWidth(PageSize.A4.getWidth()-70);
datatablebottom.setLockedWidth(true);
document.add(datatablebottom);
You need to define the absolute width of your table:
datatable.setTotalWidth(document.right(document.rightMargin()) - document.left(document.leftMargin()));
Then you need to replace the line:
document.add(datatablebottom);
with this one:
datatable.writeSelectedRows(0, -1, document.left(document.leftMargin()), datatable.getTotalHeight() + document.bottom(document.bottomMargin()), writer.getDirectContent());
The writeSelectedRows() method draws the table at an absolute position. We calculate that position by asking the document for its left margin (the x value) and by adding the height of the table to the bottom margin of the document (the Y coordinate). We draw all rows (0 to -1).

How to hyphenate text?

I generate an PDF file with iText in Java.
My table columns have fixed widths and text, which is too long for one line is wrapped in the cell.
But hyphenation is not used. The word "Leistungsscheinziffer" is shown as:
Leistungssc //Break here
heinziffer
My code where I use hyphenation:
final PdfPTable table = new PdfPTable(sumCols);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setPadding(4f);
table.setWidths(widthsCols);
table.setWidthPercentage(100);
table.setSpacingBefore(0);
table.setSpacingAfter(5);
final Phrase result = new Phrase(text, font);
result.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
final PdfPCell cell = new PdfPCell(table.getDefaultCell());
cell.setPhrase(result);
table.addCell(cell);
...
Hyphen is activated and the following test results "Lei-stungs-schein-zif-fer
"
Hyphenator h = new Hyphenator("de", "DE", 2, 2);
Hyphenation s = h.hyphenate("Leistungsscheinziffer");
System.out.println(s);
Is there anything I forgot to set to the table that hyphen is working there?
Thanks for your help. If you need more information about my code, I will tell you.
First a remark that is irrelevant to the problem: you create a PdfPCell object, but you don't use it. You add the phrase to the table instead of using the cell object.
Now for your question: normally hyphenation is set on the Chunk level:
Chunk chunk = new Chunk("Leistungsscheinziffer");
chunk.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
table.addCell(new Phrase(chunk));
If you want to set the hyphenation on the Phrase level, you can do so, but this will only work for all subsequent Chunks that are added. It won't work for the content that is already stored inside the Phrase. In other words: you need to create an empty Phrase object and then add one or more Chunk objects:
Phrase phrase = new Phrase();
phrase.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
phrase.add(new Chunk("Leistungsscheinziffer"));
I've made an example based on your code (HyphenationExample); the word "Leistungsscheinziffer" is hyphenated in the resulting PDF: hyphenation_table.pdf.

Categories