Substitute command for ChildFieldEvent? - java

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?

Related

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

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.

iTextPDF - Unable to use getOverContent() as a paremeter for table.writeSelectedRows() when internal anchors have been added to the table

I've been trying to create a table of contents which dynamically links to other pages in the PDF using anchors. I've ran into an issue with using stamper.getOverContent() as the canvas parameter in the method table.writeSelectedRows().
table.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(4));
The example below will write a table to page 4 containing an anchor destination, and a table to page 8 containing an anchor hyperlink which links back to page 4. (Note that I'm using tables for this because it is just a part of what ultimately needs to be achieved).
String strFileName = "C:\\link\\to\\existing\\PDF\\document.pdf";
PdfReader reader = new PdfReader(strFileName);
FileOutputStream out = new FileOutputStream("results/tables/link_in_positioned_table.pdf");
PdfStamper stamper = new PdfStamper(reader, out);
stamper.setRotateContents(true);
//Page 4 table containing destination
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
table.setTotalWidth(500);
Anchor target = new Anchor("page 4");
target.setName("page4");
p.add(target);
cell.addElement(p);
table.addCell(cell);
table.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(4));
//Page 8 table containing link
PdfPTable table1 = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell();
Paragraph p1 = new Paragraph();
table1.setTotalWidth(500);
Anchor anchor = new Anchor("page4 link");
anchor.setReference("#page4");
p1.add(anchor);
cell1.addElement(p1);
table1.addCell(cell1);
table1.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(8));
stamper.close();
reader.close();
out.close();
This works fine if anchor.setReference(); is an external hyperlink, but if it is internal it returns with the following error:
at
com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:126)
at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2178)
at
com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1643)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1162) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:994) at
com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1541) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1000) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:994) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:982) at
com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:583) at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:833)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:966)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:912)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:891)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.createPdf(StamperPDFPlugin.java:1727)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.main(StamperPDFPlugin.java:1684)
Am I doing something wrong? Or is this an issue?
Note: I asked a similar question here - iTextPDF - Cannot use writeSelectedRows() on a table where an anchor has been inserted which didn't identify that getOverContent() was the issue and instead focussed on the actual implementation of the anchors.

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