iText - PdfPTable RowSpan using mytable.writeSelectedRows - java

I'm using iText 5.1.3, and I want to add a header to my Pdf Document. I used the known solution posted here: http://itextpdf.com/examples/iia.php?id=104
This solution used the PdfPageEventHelper class, and overridden the method onEndPage() to add the Header exactly after finishing every page. The example provided in the link above works fine as it adds a table as the Header of the document. I'm trying to do exactly the same with 1 difference, that I want some cells in that table to have Rowspan and/or Colspan.
I tried, and found that using table.writeSelectedRows() differs from document.add(table) when it comes to Rowspan. This is a sample of what I'm trying to do in onEndPage:
PdfPTable mytable = new PdfPTable(3);
mytable.setTotalWidth(527);
PdfPCell cell1 = new PdfPCell(new Phrase("Hello"));
cell1.setColspan(2);
cell1.setRowspan(2);
mytable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Girls !"));
mytable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("Boys !"));
mytable.addCell(cell3);
mytable.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
and instead of having a cell at the left as 2x2 with "Hello", I get the "Hello" cell as 1x2 not 2x2
Any ideas?

well .. I found the solution myself :D It's simply replacing mytable.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent()); with the following:
ColumnText column = new ColumnText(writer.getDirectContent());
column.addElement(mytable);
column.setSimpleColumn(-12, -20, 604, 803); // set LLx, LLy, URx, and URy of the header
column.go();
That's it :) worked :)

iText writeSelectedRows not work with rowSpan when I use iText-2.1.7, The rowSpan cell not grow to other rows,But it has a workaround.
ArrayList tmp = table.getRows(0, table.getRows().size());
table.getRows().clear();
table.getRows().addAll(tmp);
table.writeSelectedRows(0, -1, x, y, directContent);

Related

iText column width wrong

Im using iText in android to build a pdf file with a table . It seems easy to do but the result is wrong. Help appreciated!
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(output));
Document documento = new Document(pdfDoc);
float[] columnWidths = new float[]{40, 160, 50, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25};
Table table = new Table(columnWidths);
table.setWidth(500);
com.itextpdf.layout.element.Cell cell = new com.itextpdf.layout.element.Cell(1, columnWidths.length).add("(Continuação)");
table.addHeaderCell(cell);
cell = new com.itextpdf.layout.element.Cell(1, columnWidths.length).add("Continua...");
table.addFooterCell(cell);
table.setSkipFirstHeader(true);
table.setSkipLastFooter(true);
Cell cellA;
for (int i = 0; i < 100; i++) {
cellA = new Cell(1, 1).add(String.valueOf(i+1));
cellA.setTextAlignment(TextAlignment.CENTER);
cellA.setWidth(40);
cellA.setFontSize(7);
table.addCell(cellA);
cellA = new Cell(1, 1).add("ALYNE BORGES MADEIRA");
cellA.setTextAlignment(TextAlignment.LEFT);
cellA.setWidth(160);
cellA.setFontSize(7);
table.addCell(cellA);
cellA = new Cell(1, 1).add("100.00");
cellA.setTextAlignment(TextAlignment.CENTER);
cellA.setWidth(50);
cellA.setFontSize(7);
table.addCell(cellA);
for (int j = 0; j < 10; j++) {
cellA = new Cell(1, 1).add("10.00");
cellA.setTextAlignment(TextAlignment.CENTER);
cellA.setWidth(25);
cellA.setFontSize(7);
table.addCell(cellA);
}
}
The problem is in the last column where width is not equal to prior 9.
Summary
If you are only using it to generate PDF tables instead of needing RTF and HTML output, then it might be better to use the PdfPTable class [Table is now unsupported] which is better supported and less prone to quirks (as explained here: PdfpTable vs. Table (vs. SimpleTable?)).
Sizing
If you use PdfPTable with setWidths(float[]) then you should be able to do absolute widths without needing to specify the width in each cell for code readability. (You can also use percentage widths, which, for a basic table, might be easier to get aligned properly on the page.)
Headers & Footers
Since you are using headers & footers, PdfPTable has setHeaderRows(int) and setFooterRows(int), but note that:
The number of footer rows are subtracted from the header rows. For example, for a table with two header rows and one footer row the code would be:
table.setHeaderRows(3);
table.setFooterRows(1);
PdfPCell Porting
Also note that PdfPCell has changed setTextAlignment to be setHorizontalAlignment.
Examples and Documentation
Some examples of using it are here (note the usage of setLockedWidth: https://developers.itextpdf.com/examples/tables-itext5/cell-and-table-widths
The JavaDoc for PdfPTable is here: http://itextsupport.com/apidocs/itext5/5.5.9/com/itextpdf/text/pdf/PdfPTable.html
And the JavaDoc for PdfPCell is here: http://itextsupport.com/apidocs/itext5/5.5.9/com/itextpdf/text/pdf/PdfPCell.html

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.

Need to make PDF sample with boxes as table columns by android app

I need to make PDF sample file just like the below image but I didn't find any suitable guide to do exactly like this. I have followed some links
http://itextpdf.com/examples/iia.php?id=102
Is there a way to draw a rectangle into a PdfPCell in iText (the Java version)?
but from the second link I didn't understand how could I make more likely to my requirement.
Thanks in advance.
It is unclear to me why you refer to this example: http://itextpdf.com/examples/iia.php?id=102
The PDF that is created with that example shows me in a Superman outfit. What is the link with creating a table with rounded borders?
Please take a look at the NestedTableRoundedBorder example. It creates a PDF that looks like this: nested_table_rounded_border.pdf
This construction consists of nested tables. The outer table only has one column, but we use it to create the rounded corners:
class RoundRectangle implements PdfPCellEvent {
public void cellLayout(PdfPCell cell, Rectangle rect,
PdfContentByte[] canvas) {
PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
cb.roundRectangle(
rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
rect.getHeight() - 3, 4);
cb.stroke();
}
}
This cell event is used like this:
cell = new PdfPCell(innertable);
cell.setCellEvent(roundRectangle);
cell.setBorder(Rectangle.NO_BORDER);
cell.setPadding(8);
outertable.addCell(cell);
The inner tables are used to create cells with or without borders, for instance like this:
// inner table 1
PdfPTable innertable = new PdfPTable(5);
innertable.setWidths(new int[]{8, 12, 1, 4, 12});
// first row
// column 1
cell = new PdfPCell(new Phrase("Record Ref:"));
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 2
cell = new PdfPCell(new Phrase("GN Staff"));
cell.setPaddingLeft(2);
innertable.addCell(cell);
// column 3
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 4
cell = new PdfPCell(new Phrase("Date: "));
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 5
cell = new PdfPCell(new Phrase("30/4/2015"));
cell.setPaddingLeft(2);
innertable.addCell(cell);
// spacing
cell = new PdfPCell();
cell.setColspan(5);
cell.setFixedHeight(3);
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
If some of the dimensions are quite like you want, it's sufficient to change parameters such as the widths array, the padding, the fixed height, etc.

add border to pdf page using itext

this is my source code. why am I not able to add border to my pdf page even after enabling borders for all sides? I've set border and its color too still I'm not able to add border.
void create() throws DocumentException,IOException{
// step 1
Document document = new Document();
// step 2
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.setPageSize(PageSize.LETTER);
document.setMargins(36, 72, 108, 180);
document.setMarginMirroring(false);
// step 3
document.open();
// step 4
Rectangle rect= new Rectangle(36,108);
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorder(2);
rect.setBorderColor(BaseColor.BLACK);
document.add(rect);
Font font = new Font(Font.FontFamily.TIMES_ROMAN, 26, Font.UNDERLINE, BaseColor.BLACK);
Paragraph title= new Paragraph("CURRICULUM VITAE\n\n",font);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
Font f1= new Font (Font.FontFamily.UNDEFINED, 13, Font.NORMAL, BaseColor.BLACK);
Paragraph info= new Paragraph("Name\n\nEmail\n\nContact Number",f1);
Paragraph addr= new Paragraph("Street\n\nCity\n\nPin",f1);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.spacingAfter();
PdfPCell cell = new PdfPCell(info);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.disableBorderSide(Rectangle.BOX);
cell.setExtraParagraphSpace(1.5f);
table.addCell(cell);
cell = new PdfPCell(addr);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(Rectangle.BOX);
cell.setExtraParagraphSpace(1.5f);
table.addCell(cell);
document.add(table);
document.add(new Chunk("\n"));
document.add(new LineSeparator(2f,100,BaseColor.DARK_GRAY,Element.ALIGN_CENTER,-1f));
You didn't define a border width.
You're adding the border only once. What if you want the border to appear on every page?
You can fix (1.) by adding:
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(2);
Note that I would remove the enableBorderSide() calls. You'll notice that you've used the setBorder() method in the wrong way.
To fix (2.), I would use a page event. Note that you can't use document.add() in a page event, so you'll have to do something as described in the DrawRectangle example that was written in answer to the question iText: PdfContentByte.rectangle(Rectangle) does not behave as expected
You didn't define a page size when creating the Document object, which means that iText will be using PageSize.A4. A couple of lines later, you use PageSize.LETTER. These values are immutable Rectangle objects. You can create a new Rectangle using the dimensions/coordinates of PageSize.A4 (or in your case: PageSize.LETTER). You can obtain the dimensions using the getWidth() and getHeight() method and the coordinates using getLeft(), getBottom(), getRight() and getTop().
Rectangle rect= new Rectangle(577,825,18,15); // you can resize rectangle
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorderColor(BaseColor.BLACK);
rect.setBorderWidth(1);
document.add(rect);

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).

Categories