How to create and add table to predefined pdf template using Itext? - java

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();

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

2 parallel table with image using itext5

how to generate pdf for below content using itext5.
I am able to get simple table using itext. but for above image how to achieve this?
Below is my code. it is not showing image in the table.
Paragraph paragraph = new Paragraph();
PdfPCell cell = null;
// Main table
PdfPTable mainTable = new PdfPTable(2);
//mainTable.setWidthPercentage(100.0f);
// First table
PdfPCell firstTableCell = new PdfPCell();
firstTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable firstTable = new PdfPTable(1);
mainTable.setHorizontalAlignment(Element.ALIGN_LEFT);
Image img = Image.getInstance(appIconNameWithPath);
//img.setWidthPercentage(10);
cell = new PdfPCell(img, true);
cell.setBorder(PdfPCell.NO_BORDER);
firstTable.addCell(cell);
mainTable.addCell(firstTableCell);
// Second table
PdfPCell secondTableCell = new PdfPCell();
secondTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable secondTable = new PdfPTable(2);
secondTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
secondTableCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell = new PdfPCell(new Phrase(appName));
cell.setBorder(PdfPCell.NO_BORDER);
secondTable.addCell(cell);
cell = new PdfPCell(new Phrase(packageName));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setBorder(PdfPCell.NO_BORDER);
secondTable.addCell(cell);

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.

How to add a PdfPTable to the HTML string in a document at (x,y) location using iText?

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.

Substitute command for ChildFieldEvent?

I'm trying to make an interactive PDF Form using iText and I have used this command:
PdfFormField personal = PdfFormField.createEmpty(writer);
personal.setFieldName("personal");
PdfPTable table= new PdfPTable(3);
PdfPCell cell;
cell= new PdfPCell();
cell.setColspan(2);
TextField field = new TextField(writer, new Rectangle(0,0),"name");
field.setFontSize(12);
cell.setCellEvent(new ChildFieldEvent(personal, field.getTextField(), 1));
table.addCell(cell);
But it's showing
ChildFieldEvent cannot be resolved into a type
Is there any substitute command for it?

Categories

Resources