2 parallel table with image using itext5 - java

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

Related

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 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 create and add table to predefined pdf template using Itext?

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

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?

itext and dotmatrix printers

I have a JSP page which is simply getting parameters, querying database, generating invoice PDF, and sending silently to the default printer. I use itext library.
The invoice has to be printed on a dot-matrix printer with continuous paper.
Each invoice page size is sized a5 landscape.
If i choose the pagesize as a5, code produces a PDF as its seen on here
a5 portrait
when its printed, it prints one page and leave another page blank. User has to scroll the paper back manually.
If i choose the pagesize as a5 landscape (a5.rotate()), code produces a PDF as its seen on here, which is better.
a5 landscape
but when this is printed on paper, it starts to print the page vertically, treating the printer has a a4 paper tray.
Seems to me, I need to define my printer as a dotmatrix printer with continuous paper.
The code i am using is :
Document document = new Document(PageSize.A5,0,0,0,0);
try {
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
document.open();
StringBuffer javascript = new StringBuffer();
javascript.append("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
PdfAction pdfAction= PdfAction.javaScript(javascript.toString(), writer);
writer.addJavaScript(pdfAction);
writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100);
PdfPCell cell1 = new PdfPCell(new Paragraph(""));
PdfPCell cell2 = new PdfPCell(new Paragraph(""));
PdfPCell cell3 = new PdfPCell(new Paragraph(MakbuzNo,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(Duzenleyen,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(MSISDN,FontFactory.getFont(FontFactory.COURIER,9)));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeSaati,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeTarihi,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(45f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(izahat,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(75f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(kopyayazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph(TutarYazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(2);
cell3 = new PdfPCell(new Paragraph(ToplamTutar,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell3);
document.add(table);
document.newPage();
} catch (DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
document.close();
So how to use dotmatrix printer with continous paper and stop paper scroller when character on page is already printed ?
Welcome to hell :-) Here are all the daemons that have their tiny claws in your process:
The PDF file itself has an orientation
The PDF viewer/printer might try to rotate the pages to fit the page
The printer driver might do it, too
If you had a laser printer, that might rotate the page as well.
The most "simple" solution is to render the PDF in a BufferedImage, save that as a pixel image (say, PNG) and print that. This will allow you to make sure the orientation is what you want.
Also check the settings of the printer driver: Some of them send a "Form Feed" character after printing the file which would result in an empty page if you fill the whole a5 page.
I have experienced the similar problem with the Jasper reports printing(with continuous paper), After fiddling with the code for days I found a solution I think will work for you.
I think you have to do is to generate a PDF file as output and simply get the AdobeReader exe to do the printing, you can use
AcroRd32.exe /N /T PdfFile PrinterName
parameters to achieve this.
After long researches, I could not find any way to print on clients' dot-matrix printer without any extra installation.
Most efficient way is to have a signed applet. Java installation required at clients' side.

Categories

Resources