I am working with iTextG 5.5.8 and Android. I have to split part of the pdf into Two Horizontal views. I can't use PdfPTable because I want left side fully an image and right side fully a Table. Also, I have to present the image in 25mm*12mm size. Can anyone hide me on the right path to achieve this?
There are two simple ways to achieve that:
One table with rowspan use
Assuming you have loaded your image into Image image and you add to a Document document:
PdfPTable table = new PdfPTable(new float[]{2,1,1});
PdfPCell imageCell = new PdfPCell(image);
imageCell.setRowspan(4);
imageCell.setVerticalAlignment(PdfPTable.ALIGN_CENTER);
table.addCell(imageCell);
PdfPCell cell = new PdfPCell(new Phrase("Address1"));
cell.setBorder(Rectangle.TOP | Rectangle.LEFT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Contact Number"));
cell.setBorder(Rectangle.TOP | Rectangle.RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Address2"));
cell.setBorder(Rectangle.LEFT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Fax"));
cell.setBorder(Rectangle.RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Address3"));
cell.setBorder(Rectangle.LEFT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Pin Code"));
cell.setBorder(Rectangle.RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Address4"));
cell.setBorder(Rectangle.BOTTOM | Rectangle.LEFT);
table.addCell(cell);
cell = new PdfPCell(new Phrase(""));
cell.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT);
table.addCell(cell);
document.add(table);
(CreateTable test testSureshOneTableRowspan)
Two tables
Again assuming you have loaded your image into Image image and you add to a Document document:
PdfPTable innerTable = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Phrase("Address1"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase("Contact Number"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase("Address2"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase("Fax"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase("Address3"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase("Pin Code"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase("Address4"));
cell.setBorder(0);
innerTable.addCell(cell);
cell = new PdfPCell(new Phrase(""));
cell.setBorder(0);
innerTable.addCell(cell);
PdfPTable table = new PdfPTable(2);
PdfPCell imageCell = new PdfPCell(image);
imageCell.setVerticalAlignment(PdfPTable.ALIGN_CENTER);
table.addCell(imageCell);
table.addCell(innerTable);
document.add(table);
(CreateTable test testSureshTwoTables)
Related
II am beginner with itext, I have created simple layouts but the below layout in attached image is bit complex to create, but unable to create the layout using iText.
the attached image is just for the sample, entire data is fake, I have all the required data in DTO.
I created method which will generate table, but as we see the rows and columns are not uniform, and here I stuck
private static void createTable(Section subCatPart)
throws BadElementException {
PdfPTable table = new PdfPTable(3);
// t.setBorderColor(BaseColor.GRAY);
// t.setPadding(4);
// t.setSpacing(4);
// t.setBorderWidth(1);
PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 2"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 3"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell("1.0");
table.addCell("1.1");
table.addCell("1.2");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");
subCatPart.add(table);
}
I am writing a Java code to generate a PDF template. In the pdf's header section, I have created a pdfTable which has 7 cells including an image cell (Logo), a text field (Id Number) and remaining 5 cells to populate the actual Id Number.
In the output, I should get a bigger image cell (representing the Logo) and Id number cells must be smaller in size than the image cell. Example, as in the below image (Expected Result).
However when the template is generated, I am unable populate as expected as shown in the above image (Expected Result).
When the PDF is generated, all the cells are taking the size of the image cell.
I have tried with different approaches like setting column widhts, setFixedHeight(), setRowSpan(), setColumnSpan() methods etc. But nothing worked. Below image shows my output (Current Output).
Below is code which I have written.
public class NbaBafTemplateGenerator {
private void createNbaBafTemplate(File outPutfileName, NbaBafTemplateData formData,String logoName) {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPutfileName));
document.open();
PdfPTable table = null;
// Passing the data as a single String
//IdNumber varible is of type String and has 5 characters of the number.
table = NbaBafTemplatePage.createHeaderTable(logoName + ",Id Number: , " +
formData.getIdNumber(), 7, "", "","1.5f, 1f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f");
document.add(table);
}// END OF CLASS NbaBafTemplateGenerator.
//Class NbaBafTemplatePage Begins.
public class NbaBafTemplatePage extends PdfPageEventHelper {
public static PdfPTable createHeaderTable(String text, int columnCount, String colour, String align, String colSize)
throws DocumentException, IOException {
PdfPTable table = null;
table = new PdfPTable(columnCount); // 7 columns.
table.setWidthPercentage(100); // Width 100%
table.setSpacingBefore(0f); /
table.setSpacingAfter(10f);
//Assigning column widths based on input width params.
float[] tablecolumnWidths = {
Float.parseFloat(colSize.split(",")[0]),
Float.parseFloat(colSize.split(",")[1]),
Float.parseFloat(colSize.split(",")[2]),
Float.parseFloat(colSize.split(",")[3]),
Float.parseFloat(colSize.split(",")[4]),
Float.parseFloat(colSize.split(",")[5]),
Float.parseFloat(colSize.split(",")[6])};
PdfPCell imgCell = new PdfPCell(createImageCell(text.split(",")[0]));
//imgCell.setColspan(3);
//imgCell.setRowspan(3);
imgCell.setBorder(PdfPCell.NO_BORDER);
imgCell.setHorizontalAlignment(Element.ALIGN_LEFT);
imgCell.setVerticalAlignment(Element.ALIGN_LEFT);
table.addCell(imgCell);
PdfPCell idCell = new PdfPCell(new Paragraph(text.split(",")[1]));
idCell.setBorderColor(BaseColor.BLACK);
idCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
idCell.setPaddingLeft(10);
idCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
idCell.setVerticalAlignment(Element.ALIGN_RIGHT);
table.addCell(idCell);
PdfPCell cellC0 = new PdfPCell(new Paragraph(text.split(",")[2]));
cellC0.setBorderColor(BaseColor.BLACK);
cellC0.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC0.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC0);
PdfPCell cellC1 = new PdfPCell(new Paragraph(text.split(",")[3]));
cellC1.setBorderColor(BaseColor.BLACK);
cellC1.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC1);
PdfPCell cellC2 = new PdfPCell(new Paragraph(text.split(",")[4]));
cellC2.setBorderColor(BaseColor.BLACK);
cellC2.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC2);
PdfPCell cellC3 = new PdfPCell(new Paragraph(text.split(",")[5]));
cellC3.setBorderColor(BaseColor.BLACK);
cellC3.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC3.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC3);
PdfPCell cellC4 = new PdfPCell(new Paragraph(text.split(",")[6]));
cellC4.setBorderColor(BaseColor.BLACK);
cellC4.setHorizontalAlignment(Element.ALIGN_CENTER);
cellC4.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cellC4);
return table;
}//END OF METHOD createHeaderTable.
public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
Image img = Image.getInstance(path);
PdfPCell cell = new PdfPCell(img, true);
return cell;
}
}
I am using Java and iText 5.x version.
Can anyone please let me know how to generate the pdf table with different cell sizes.
You can create a table with 2 rows (13 cells). Set the column-span of image cell equal to 2. Keep the remaining cells of the first row as blank, set their borders as 0 (invisible), and adjust their heights (cells of the first row) as per your required alignment.
Then add the remaining 6 cells to the table as a second row. Adjust column widths as per your requirement. Hope this helps.
PdfPTable table = new PdfPTable(7);
table.setWidthPercentage(100);
PdfPCell imageCell = new PdfPCell(image);
imageCell.setBorder(0);
imageCell.setRowspan(2);
table.addCell(imageCell);
for(int i=0; i<6;i++) {
PdfPCell blankCell = new PdfPCell();
blankCell.setBorder(0);
blankCell.setFixedHeight(20f);
table.addCell(blankCell);
}
PdfPCell cell22 = new PdfPCell(new Phrase("ID Number"));
table.addCell(cell22);
PdfPCell cell23 = new PdfPCell(new Phrase("9"));
table.addCell(cell23);
PdfPCell cell24 = new PdfPCell(new Phrase("6"));
table.addCell(cell24);
PdfPCell cell25 = new PdfPCell(new Phrase("0"));
table.addCell(cell25);
PdfPCell cell26 = new PdfPCell(new Phrase("5"));
table.addCell(cell26);
PdfPCell cell27 = new PdfPCell(new Phrase("1"));
table.addCell(cell27);
document.add(table);
Using iText5 in java and want to color-code a PdfPcell's results as shown below. The table includes 2 columns and 3 rows, for example.
Anyone have an idea how to accomplish this?
Could I simply set the background color using
PdfPCell cell = new PdfPCell(new Phrase("60 Pass, 40 Fail", myStyle));
cell.setBackgroundColor(new BaseColor(0xff,0x0,0x0)); // red background
then, do what, add a green rectangle to the cell? Use a template? Not sure.
You can accomplish such "interesting" cell features by means of cell event listeners.
E.g. for your task you can implement a PdfPCellEvent like this:
public class PercentileCellBackground implements PdfPCellEvent {
public PercentileCellBackground(float percent, BaseColor leftColor, BaseColor rightColor) {
this.percent = percent;
this.leftColor = leftColor;
this.rightColor = rightColor;
}
#Override
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
float xTransition = position.getLeft() + (position.getRight() - position.getLeft()) * (percent/100.0f);
float yTransition = (position.getTop() + position.getBottom()) / 2f;
float radius = (position.getRight() - position.getLeft()) * 0.025f;
PdfShading axial = PdfShading.simpleAxial(canvas.getPdfWriter(),
xTransition - radius, yTransition, xTransition + radius, yTransition, leftColor, rightColor);
PdfShadingPattern shading = new PdfShadingPattern(axial);
canvas.saveState();
canvas.setShadingFill(shading);
canvas.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
canvas.fill();
canvas.restoreState();
}
final float percent;
final BaseColor leftColor;
final BaseColor rightColor;
}
(PercentileCellBackground)
You can then use this event listener class like this:
Document document = new Document();
try (OutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "TableWithCellsWithPercentileBackground.pdf"))) {
PdfWriter.getInstance(document, os);
document.open();
Font font = new Font(FontFamily.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, BaseColor.WHITE);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(40);
PdfPCell cell = new PdfPCell(new Phrase("Group A"));
table.addCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("60 Pass, 40 Fail", font)));
cell.setCellEvent(new PercentileCellBackground(60, BaseColor.GREEN, BaseColor.RED));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Group B"));
table.addCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("70 Pass, 30 Fail", font)));
cell.setCellEvent(new PercentileCellBackground(70, BaseColor.GREEN, BaseColor.RED));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Group C"));
table.addCell(cell);
cell = new PdfPCell(new Phrase(new Chunk("50 Pass, 50 Fail", font)));
cell.setCellEvent(new PercentileCellBackground(50, BaseColor.GREEN, BaseColor.RED));
table.addCell(cell);
document.add(table);
document.close();
}
(CellsWithPercentileBackground test testCreateTableWithCellsWithPercentileBackground)
The result looks like this:
As you can see, I used a shading to make the green/red transition less harsh. If you want it more harsh, reduce the radius factor 0.025f in the event cellLayout method or use rectangle drawing instructions therein instead.
Working with iText and using table cells.
I have 25 cells (columns) in a table.
The content of each column is rotated 90 degree and is required to be fitted to the height of each cell.
In some cases when the length of the content exceeds the height of the cell, not all the content is visible (Only the part of content is visible that is fitted to the height of the cell, the rest is dropped). I want to get that dropped content and want to show it in the next adjacent cell.
The following code is used -
PdfPCell cell;
for(int i = 0;i< 25;i++)
{
if(locs.get(i) == null)
{
cell = new PdfPCell();
cell.setBorder(0);
table.addCell(cell);
}
else
{
Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD, BaseColor.BLACK);
cell = new PdfPCell(new Phrase(locs.get(i), font));
cell.setRotation(90);
cell.setBorder(0);
cell.setFixedHeight(110f);
//cell.setMinimumHeight(10f);
table.addCell(cell);
}
}
So if the value of locs.get(i) is greater then the height of the cell (cell height is fixed to 110f in the above code), some content that doesn't fit gets dropped to be shown in the view.
How to get that content and show it to the adjacent cell ?
The method used that solved the purpose is the following:
PdfPCell cell;
for(int i = 0;i< 25;i++)
{
if(locs.get(i) != null)
{
Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD, BaseColor.BLACK);
cell = new PdfPCell(new Phrase(locs.get(i), font));
cell.setRotation(90);
cell.setBorder(0);
cell.setFixedHeight(110f);
cell.setColspan(2);
table.addCell(cell);
}
else
{
cell = new PdfPCell();
cell.setBorder(0);
table.addCell(cell);
}
}
So setting the colspan to 2 ensures that if the contents exceeds the length of the first column then move the remaining contents to the next column of the cell (One cell having two columns now after adding the colspan of 2).
If anyone knows the better way to do the same thing then you are welcome!
I have done
business with iText, I
created a table with 9
columns, I have grouped the
first 4 to 1, then I have the
next 4 grouped in one,
staying one at the end, the
point is assigned
cell.setBorderwidth = 2 in
all cells, to generate the
document do not match
edges.
Hope you can help
me.
Here is code used:
public PdfPTable createTable5() throws DocumentException {
PdfPTable table = new PdfPTable(10);
table.setTotalWidth(540);
//table.setWidthPercentage(105);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setLockedWidth(true);
table.setWidths(new float[]{26.16f,48.77f,48.77f,48.77f,40.06f,56.64f,78.41f,53.1f,59.2f,80.11f});
PdfPCell cell;
cell = new PdfPCell(new Phrase(" ",subFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setBorderWidth(0);
//cell.setBorderWidthRight(0);
//cell.setColspan(4);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Cantidades por material",subFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
cell.setBorderWidth(2);
cell.setBorderWidthRight(0);
cell.setColspan(4);
cell.setFixedHeight(g);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Costo por material",subFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
cell.setBorderWidth(2);
cell.setBorderWidthRight(0);
cell.setColspan(4);
cell.setFixedHeight(g);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Total",subFont));// Error in Border
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setUseAscender(true);
cell.setBorderWidth(2);
//cell.setColspan(1);
cell.setFixedHeight(g);
//cell.setBorderWidthRight(0);
table.addCell(cell);
return table;
}
Try setUseBorderPadding(true) for each cell.