How set miminum height for split PdfPCell - java

On page I have table, and at the end of the page I have one paragraph, if rows contains small count of rows paragraph must still stay at the end of the page, but if rows too much and table take more than one page paragraph must be just after the table end. At first it looks easy for me and I just set minimum height for PdfPCell which contains table, but then I discover if page is split on pages minimum height apply to each part of table, and because of it paragraph isn't under the table on second page. Is there any solution for it?

I found one not very good solution for this problem, I've added one column to the table, which width it too small and it isn't visible for users, in this column I've add table with two rows, for first row I set minimum height which I need, here the code for creating this additional column
private PdfPCell createMinHeight(float minH) {
PdfPCell cell = new PdfPCell();
PdfPTable table = new PdfPTable(1);
cell.setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.setHeaderRows(0);
PdfPCell firstRow = new PdfPCell();
firstRow.setBorder(Rectangle.NO_BORDER);
firstRow.setMinimumHeight(minH);
firstRow.setPadding(0);
table.addCell(firstRow);
table.addCell("");
cell.setPadding(0);
cell.addElement(table);
return cell;
}

If I understand you right, you don't want the paragraph splits? You can use setKeepTogether(boolean); for the elements you need:
paragraph.setKeepTogether(true);
or
table.setKeepTogether(true);

Related

iText 7 borderless table (no border)

This code below does not work.
Table table = new Table(2);
table.setBorder(Border.NO_BORDER);
I am new to iText 7 and all I wanted is to have my table borderless.
Like how to do it?
The table itself is by default not responsible for borders in iText7, the cells are. You need to set every cell to be borderless if you want a borderless table (or set the outer cells to have no border on the edge if you still want inside borders).
Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
You could write a method which runs though all children of a Table and sets NO_BORDER.
private static void RemoveBorder(Table table)
{
for (IElement iElement : table.getChildren()) {
((Cell)iElement).setBorder(Border.NO_BORDER);
}
}
This gives you the advantage that you can still use
table.add("whatever");
table.add("whatever");
RemoveBorder(table);
instead of changing it on all cells manual.

Keep cell content on one page

I have a table, with one column. Each cell contains a paragraph.
How can I stop paragraphs from splitting across two pages?
PdfPtable table = new PdfPTable(1);
//report must be printed as compat as possible
table.setSplitLate(false);
//I can't set keep together, because table can be larger than page size
//table.setKeepTogether(true);
for (int i = 0; i < 100; i++) {
//Random text. Can contain ~400 chars.
String text = "aaaaaaaaaaaaaaa sssssssssssss ddddddddddd ffffffffff";
Paragraph p = new Paragraph(text);
//That instruction does not work. I don't know why, may be because paragraph printed in cell.
p.setKeepTogether(true);
table.addCell(p);
}
Change
table.setSplitLate(false);
into
table.setSplitLate(true);
This way, your cell will not be split unless the complete cell doesn't fit on a single page.

iText combining rowspan and colspan - PDFPTable

Working on a calendar projcet and using iText to generate a pdf to print appointments. I can plot a cell with a colspan, and a cell with a rowspan, but I can't combine it. Table has width of 4 cells. I want to achieve something like this:
(A)(B)(C)(C)
(D)(E)(C)(C)
so (1,1), (1,2) and (2,1) (2,2) are regular cells. But there should be a cell in (1,3) covering (1,3) (1,4) (2,3) and (2,4) thus having a colspan of 2 AND a rowspan of 2.
Current code:
PdfPTable table = new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Phrase(" 1,1 "));
table.addCell(cell);
cell = new PdfPCell(new Phrase(" 1,2 "));
table.addCell(cell);
PdfPCell cell23 = new PdfPCell(new Phrase("multi 1,3 and 1,4"));
cell23.setColspan(2);
cell23.setRowspan(2);
table.addCell(cell23);
cell = new PdfPCell(new Phrase(" 2,1 "));
table.addCell(cell);
cell = new PdfPCell(new Phrase(" 2,2 "));
table.addCell(cell);
// 2,3 and 2,4 should be filled because 1,3 has rowspan 2 and colspan 2.
//table.completeRow(); //no effect
However that generates an error:
ExceptionConverter: java.io.IOException: The document has no pages.
If i don't start creating the second row, it just plots fine ( 1 row, and cell on (1,3) has a colspan of 2. Since there is no second row, the rowspan(2) has no effect.
Any help is appreciated. Thanks
At first sight, I'd say: you get a "document has no pages" exception because you're not adding any content to the document. I don't see:
document.add(table);
anywhere in your code snippet.
I have copy/pasted your code into a full example and I posted the full example here: ColspanRowspan. The resulting PDF looks like this:
This seems to be the desired behavior. I can only think of two differences: (1) you're forgetting to add the actual table (which was my initial answer), or (2) you are using a mighty old version of iText in which rowspan wasn't fully supported.

Add a PDFPTable to bottom of page in iText

I'm trying to add a table as a footer containing all the copyright text, page number etc. But I can't find any supporting method that'll accept a PdfPTable
For a phrase there is code like:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(
String.format("%d", document.getPageNumber())),
(document.getPageSize().getLeft() + document.getPageSize().getRight())/2,
document.getPageSize().getBottom() + 18, 0);
The PdfPTable class has a method writeSelectedRows() that can be used to add (a selection of columns and) rows at an absolute position.
Examples:
http://itextpdf.com/examples/iia.php?id=89 adds rows at an absolute position.
http://itextpdf.com/examples/iia.php?id=90 adds a selection of columns/rows at an absolute position.
http://itextpdf.com/examples/iia.php?id=91 an alternative solution where you wrap a table in a ColumnText object.
The examples posted by Bruno are a good pointer, here's an example without magic numbers:
private void writeFooterTable(PdfWriter writer, Document document, PdfPTable table) {
final int FIRST_ROW = 0;
final int LAST_ROW = -1;
//Table must have absolute width set.
if(table.getTotalWidth()==0)
table.setTotalWidth((document.right()-document.left())*table.getWidthPercentage()/100f);
table.writeSelectedRows(FIRST_ROW, LAST_ROW, document.left(), document.bottom()+table.getTotalHeight(),writer.getDirectContent());
}
This will write the PdfPTable within the document margins at the bottom overlapping any text you have at the bottom. If you wish to write the table in the margin, use: document.bottom() instead of document.bottom()+table.getTotalHeight().
Header/Footer Example
As a relevant note if you're following the example on this link, the "art" box does not appear to be required and the magic numbers 36, 54, 559, 788 correspond to:
document.left(), document.bottom(), document.right(), document.top()
To implement a custom footer you need to implement the PdfPageEventHelper.

JAVA+POI API Excel- Need to increase width of the column

I want to increase the width of the column of excel sheet. as the i am writing trough code is long.
and I need to drag the column manually to see the full text.
I did this –
HSSFRow dataRow = sampleDataSheet.createRow(0);
HSSFCellStyle cellStyle = setHeaderStyle(sampleWorkbook);
cellStyle.setWrapText(true);
***sampleDataSheet.autoSizeColumn(1000000);***
But its not changing anything..
This should work. However,
sampleDataSheet.autoSizeColumn(1000000);
auto-expands column 1000000.
If you want to auto-expand column 0(the first column), use:
sampleDataSheet.autoSizeColumn(0);
To auto-expand column 0 to 9(the first 10 columns):
for (int i=0; i<10; i++){
sampleDataSheet.autoSizeColumn(i);
}
Also, you should create all your rows and fill them with content first, before you call autoSizeColumn(so the column gets the width of the value with the broadest width).
(If you want to set the column width to a fixed value, use HSSFSheet.setColumnWidth(int,int) instead.)
// We can set column width for each cell in the sheet
sheet.setColumnWidth(0, 1000);
sheet.setColumnWidth(1, 7500);
sheet.setColumnWidth(2, 7500);
// By applying style for cells we can see the total text in the cell for specified width
HSSFCellStyle cellStyle = workBook.createCellStyle();
cell.setCellStyle(cellStyle );
cellStyle.setWrapText(true);
sheet.autoSizeColumn(columnNumber) works. this will resize the column to the largest cell length.

Categories