The content of cell does not display in iText7 - java

If the content of cell is bigger than height of cell in the height-fixed table, the content does not display.
This is probable a specification of iText library, but is there any way to make the content visible in iText7 in this case?
I'm migrating from iText5 to iText7.
Even though I set the same value(table and cell height, padding value, font size) as iText5, the content of cell does not display in iText7.
If I set the TopPadding and BottomPadding of Cell to 0, the content of cell does display.
I don't think that iText5 and iText have diffent coordinates and units.
How do I get the same result as iText5?
iText 5's code
PdfPTable ptable = new PdfPTable(1);
ptable.setTotalWidth(143f);
ptable.setWidthPercentage(100f);
PdfPCell pcell = new PdfPCell();
pcell.setPadding(2f);
pcell.setUseAscender(true);
pcell.setUseDescender(true);
pcell.setFixedHeight(16.05603f);
Paragraph p = new Paragraph();
p.setLeading(4.032f, 1.0f);
p.setSpacingBefore(0f);
p.setSpacingAfter(0f);
Chunk str = new Chunk("Sample");
Font font = FontFactory.getFont("batang", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
font.setSize(12f);
str.setFont(font);
p.add(str);
pcell.addElement(p);
ptable.addCell(pcell);
ptable.writeSelectedRows(0, -1, 300f, 150f, contentByte);
iText 7's code
Table table = new Table(new float[] {143f});
table.setWidthPercent(100f);
table.setHeight(16.05603f);
Cell cell = new Cell();
cell.setPadding(2f);
cell.setHeight(16.05603f);
Paragraph p = new Paragraph();
p.setFixedLeading(4.032f);
p.setMultipliedLeading(1.0f);
p.setMarginTop(0f);
p.setMarginBottom(0f);
PdfFont font = PdfFontFactory.createRegisteredFont("batang", PdfEncodings.IDENTITY_H, true);
Text str = new Text("Sample").setFontSize(12f).setFont(font);
p.add(str);
cell.add(p);
table.addCell(cell);
table.setFixedPosition(300f, 150f, 143f);
document.add(table);

Related

Font Size not reflecting in IText

I am trying to set the font size inside my table cell but it isn't working. I am using IText 2.0.8 because it is a legacy application and cannot upgrade to the later versions.
I tried using a phrase inside the paragraph and setting the font there as well but it isn't getting reflected in the generated PDF.
PdfPCell cell = new PdfPCell();
PdfPTable innerTable = new PdfPTable(TWO);
BaseFont bf1 = BaseFont.createFont(BaseFont.COURIER_BOLDOBLIQUE,
BaseFont.CP1252, BaseFont.EMBEDDED);
BaseFont bf2 = BaseFont.createFont(BaseFont.TIMES_BOLDITALIC,
BaseFont.CP1252, BaseFont.EMBEDDED);
Paragraph paragraph = new Paragraph(key + ": ", new Font(bf1, 30));
PdfPCell innerCell = new PdfPCell(paragraph);
innerCell.setBorder(Rectangle.NO_BORDER);
innerTable.addCell(innerCell);
paragraph = new Paragraph(value, new Font(bf2, 30));
innerCell = new PdfPCell(paragraph);
innerCell.setBorder(Rectangle.NO_BORDER);
innerTable.addCell(innerCell);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(innerTable);
cell.setFixedHeight(FIXED_HEIGHT);
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Can you have a look,
Font font = new Font(BaseFont.createFont(BaseFont.COURIER_BOLDOBLIQUE,
BaseFont.CP1252, BaseFont.EMBEDDED), 30, Font.BOLD, new Color(0, 0, 0));
Paragraph paragraph = new Paragraph(key + ": ", font);

how to change font size and colour of a list in itext?

I'm using itextpdf for generating my pdf file. I have a list which should be added in a cell of a table. The problem is I can't change the font size of the list present in the cell of the table?
Here my code is
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
List fruitList = new List(List.UNORDERED);
//fruitList.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6)));
fruitList.add("Mango");
fruitList.add("Apple");
fruitList.add("Orange");
cell.addElement(fruitList);
table.addCell(cell);
Here I have added setListSymbol for the created list. But the font or its size is not getting changed. But font is getting changed for cell's which doesn't have list in it. How to change the size and font of the list?
Are you sure this isn't what you want:
Font font = FontFactory.getFont(FontFactory.HELVETICA, 6)
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
List fruitList = new List(List.UNORDERED);
fruitList.add(new ListItem("Mango", font));
fruitList.add(new ListItem("Apple", font));
fruitList.add(new ListItem("Orange", font));
cell.addElement(fruitList);
table.addCell(cell);

iText 7 table ignoring my table border settings

I want a table with no borders. I've tried to set the border property, individual border properties, set the border manually, set the cells border to no border, etc. None remove the border. What is the proper way to have an iText 7 table with no border?
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
Document doc = new Document(pdfDoc);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] { 1, 1 });
table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
table.setProperty(Property.BORDER, Border.NO_BORDER);
table.setBorder(Border.NO_BORDER);
table.setWidthPercent(100);
// Header
File file = new ClassPathResource("logo.png").getFile();
Image logo = new Image(ImageDataFactory.create(file.getPath()));
Paragraph headerParagraph = new Paragraph();
Text headerTitle = new Text("Title of PDF")
.setFont(font)
.setFontSize(20)
.setFontColor(new DeviceRgb(0, 128, 128));
Text headerDescription = new Text("Description")
.setFont(font)
.setFontSize(11);
headerParagraph.add(headerTitle);
headerParagraph.add(NEW_LINE);
headerParagraph.add(headerDescription);
table.addCell(logo);
table.addCell(headerParagraph).setTextAlignment(TextAlignment.RIGHT);
None of these settings seem to work.Using iText 7.0.2
First of all, run the next snippet and see that iText7 can create tables without borders.
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
Table table = new Table(new float[] {50, 50 });
Paragraph headerParagraph = new Paragraph();
Text headerTitle = new Text("Title of PDF")
.setFontSize(20)
.setFontColor(new DeviceRgb(0, 128, 128));
Text headerDescription = new Text("Description")
.setFontSize(11);
headerParagraph.add(headerTitle);
headerParagraph.add(headerDescription);
table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(headerParagraph).setBorder(Border.NO_BORDER).setTextAlignment(TextAlignment.RIGHT));
doc.add(table);
This is the line responsible for such "magic" :
table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
However there is no magic at all.
By default, cells have borders in iText7 (0.5px solid black). So if you want to add a cell without border you should specify it by setting NO_BORDER as a cell border.
On the other hand, tables don't have borders by default (I mean bounding borders). So there is no need in these lines:
table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
table.setProperty(Property.BORDER, Border.NO_BORDER);
table.setBorder(Border.NO_BORDER);
Also you should understand that table.setBorder(border) stands for table.setProperty(Property.BORDER, border). The same for table.setBorderLeft(border), etc.

align cell in itextpdf java

I'm using itextpdf to create my pdf with tables. While creating table i need to align some column to right, but its now working properly , can you guys help me.
I tried googling too, but didt work out for me. im using itextpdf 5.4 version.
public void generateMonthlySubReport(String[][] StrArray,String dueMonth,int Amt){
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(MON_SUB_FILE));
PdfPTable pt = new PdfPTable(StrArray[0].length);
pt.setTotalWidth(new float[]{ 55,120,360,140});
pt.setLockedWidth(true);
PdfPCell pcell = new PdfPCell();
document.open();
addKvLogo(document);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p1 = new Paragraph("Monthly Subscription Report",catFont);
p1.setAlignment(Element.ALIGN_CENTER);
addEmptyLine(p1,2);
document.add(p1);
Paragraph p2 = new Paragraph("Month : "+dueMonth);
p2.add(new Chunk(glue));
p2.add("Per Member : Rs."+Amt);
addEmptyLine(p2,2);
document.add(p2);
for(int i=0;i<StrArray.length;i++){
for(int j=0;j<StrArray[i].length;j++){
pcell = new PdfPCell();
if(i==0){
pcell.setBackgroundColor(BaseColor.LIGHT_GRAY);
}else{
pcell.setBackgroundColor(BaseColor.WHITE);
}
pcell.setUseAscender(true);
pcell.setMinimumHeight(22);
pcell.setPaddingLeft(10);
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
pcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
pcell.addElement(new Phrase(StrArray[i][j]));
pt.addCell(pcell);
}
}
pt.setTotalWidth(PageSize.A4.getWidth()-(document.leftMargin()*2));
pt.setLockedWidth(true);
document.add(pt);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
} `
You are mixing text mode with composite mode.
This is text mode:
pcell = new PdfPCell(new Phrase(StrArray[i][j]));
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
In this case, the alignment of the cell will be used for the alignment of the text.
This is composite mode:
pcell = new PdfPCell();
Paragraph p = new Parapgraph(StrArray[i][j])
p.setAlignment(Element.ALIGN_RIGHT);
pcell.addElement(p);
In this case, the alignment of the cell is ignored, in favor of the alignment of the element.
How to know the difference between text mode and composite mode?
iText automatically switches from text mode to composite mode in a PdfPCell the moment you use the addElement() method. As soon as you do this, some properties defined at the cell level are ignored. This explains why the content you are adding isn't right-aligned.

Itext PDF Cell rotation loses hyperlink

I am trying to create a lateral table with one of the cells being a hyperlink to a web site with iText 5.5.8. If I create a horizontal table, the result is ok:
File file = new File("c://temp//itext-test.pdf");
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fileout);
document.open();
String stampedURL = "http://test.com";
URL validaURL = new URL(stampedURL);
try {
PdfPTable table = new PdfPTable(3); // 3 columns.
PdfPCell cell1 = new PdfPCell();
Chunk paragr = new Chunk("Click Here!");
PdfAction pdfAct = new PdfAction(validaURL);
paragr.setAction(pdfAct);
paragr.setAnchor(validaURL);
cell1.addElement(paragr);
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
document.add(table);
document.close();
} catch(Exception e){
}
This code generates a horizontal table with 3 cells. The first one, contains an anchor and a pdfAction to a certain URL, so it is working fine.
To rotate the table, I simply rotate 90 degrees every cell. So before adding the cells to the table, I do:
cell1.setRotation(90);
cell2.setRotation(90);
cell3.setRotation(90);
Now I have rotated the cells, but the hyperlink has disappeared. I have been trying with several combinations, but with no luck.
Just to see if it was possible, I created a Word Document with rotated hyperlink in a cell, and then converted to PDF, and the link works... I know it is not a very helpful test, but just to try.
Any hint will be appreciated.
Thanks in advanced,
Xisco.

Categories

Resources