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);
Related
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);
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);
I have a predefined Template and i want to create and add table to 3rd page of template. Is it possible to do so?There are lot of option for creating new pdf using itext but i haven't seen any example where modifying existing pdf and adding table to it is provided. Code example will be highly appreciated.
PdfReader reader = new PdfReader("BCC Statements-Template.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
try {
PdfStamper stamper= new PdfStamper(reader, fileOutputStream);
PdfContentByte overContentByte = stamper.getOverContent(3);
PdfPTable pdfPTable = new PdfPTable(4);
pdfPTable.setTotalWidth(40);
//Create cells
PdfPCell pdfPCell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell pdfPCell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell pdfPCell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPCell pdfPCell4 = new PdfPCell(new Paragraph("Cell 4"));
//Add cells to table
pdfPTable.addCell(pdfPCell1);
pdfPTable.addCell(pdfPCell2);
pdfPTable.addCell(pdfPCell3);
pdfPTable.addCell(pdfPCell4);
pdfPTable.writeSelectedRows(1, 1, 110, 150, overContentByte);
stamper.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
}`
You use PdfStamper for that. The code goes like this:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte canvas = stamper.getOverContent(1);
PdfPTable table = ...;
//add data to table
table.writeSelectedRows(... , canvas);
stamper.close();
reader.close();
I am doing html to pdf conversion using iText.
I am already using HTMLWorker class (deprecated) having the following content code:
String htmlString = "<html><body> This is my Project <table width= '50%' border='0' align='left' cellpadding='0' cellspacing='0'><tr><td>{VERTICALTEXT}</td></tr></table></body></html>";
OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlString ));
document.close();
file.close();
}
Now I want to replace {VERTICALTEXT} dynamically with some string.
So I further added the code below:
PdfPTable table = null;
PdfPCell cell;
cell = new PdfPCell(new Phrase("My Vertical Text"));
cell.setRotation(90);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
String verticalLoc = table.toString(); //this variable should hold the text "My Vertical Text" in 90 degree rotated form.
HashMap<String, String> map = new HashMap<String, String>();
map.put("VERTICALTEXT", verticalLoc);
html = new String(buffer);
for (HashMap.Entry<String, String> e : map.entrySet())
{
String value = e.getValue() != null ? e.getValue():"";
html = html.replace("{" + e.getKey() + "}", value);
}
htmlWorker.parse(new StringReader(htmlStr));
In the Output:
{VERTICALTEXT} is replaced with com.itextpdf.text.pdf.PdfPTable#41d62bcO
Desired Output:
{VERTICALTEXT} should be replaced with My Vertical Text in a 90 degree rotated form.
This is the solution figured out and tested -
Java file relevant code:
static PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell = new PdfPCell(new Phrase("My Vertical Text"));
cell.setRotation(90);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
htmlWorker.parse(new StringReader(htmlStr));
table.setTotalWidth(400f);
table.writeSelectedRows( 0, -1, 80, 330, writer.getDirectContent());
So the magic of method writeSelectedRows worked in placing the table to the (x, y) location.
Where,
x = 80
y = 330
Complete details about writeSelectedRows.
This will help somebody facing the same problems with itext positioning.
I am trying to create a formatted PdfPCell where my text is on the left and an image(QRCode) is "floated" (in the css sense) to the right. My current code moves the image to the right but the text is on the next line not the same line as the image.
Ideas?
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
p.add(new Paragraph("Ciao Baby",RESTNAME));
BarcodeQRCode qrcode = new BarcodeQRCode("http://www.tvfoodmaps.com", 72, 72, null);
Image img = qrcode.getImage();
img.scaleToFit(32,32);
img.setAlignment(Element.ALIGN_RIGHT);
cell.addElement(img);
cell.addElement(p);
You can try replacing
img.setAlignment(Element.ALIGN_RIGHT);
with
img.Alignment = Image.TEXTWRAP | Image.ALIGN_RIGHT;
Try this.
Phrase phrase = new Phrase("Ciao Baby",RESTNAME);
BarcodeQRCode qrcode = new BarcodeQRCode("http://www.tvfoodmaps.com", 72, 72, null);
Image img = qrcode.getImage();
img.scaleToFit(32,32);
phrase.add(new Phrase(new Chunk(img, 0, 0)));
cell.addElement(phrase);