how to add the checkbox into the Itext pdf table - java

am generating check-box inside Itext PDF Table but inside table check box is not generating its generating outside table.could please help me how to append that check box into PDF table.could you please help me out.
Below is my code:
Class A{
public void createFourColumnBody() throws DocumentException, FileNotFoundException {
com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
com.itextpdf.text.pdf.PdfWriter writer1 = PdfWriter.getInstance(document, new FileOutputStream("D:\\PDF_Java.pdf", false));
document.open();
float[] widths = new float[]{30f, 30f};
com.itextpdf.text.pdf.PdfPTable table = new com.itextpdf.text.pdf.PdfPTable(widths);
table.setWidthPercentage(100);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
PdfFormField checkboxGroupField = PdfFormField.createCheckBox(writer1);
PdfPCell cell = table.getDefaultCell();
PdfPCell cell12;
cell = new PdfPCell(new Paragraph("checkbox3"));
table.addCell(cell);
cell12 = new PdfPCell(table.getDefaultCell());
cell12.setCellEvent(new CellField(writer1, checkboxGroupField, true));
table.addCell(cell12);
writer1.addAnnotation(checkboxGroupField);
document.add(table);
document.close();
}
public static void main(String[] args) throws DocumentException, FileNotFoundException {
A a1 = new A();
a1.createFourColumnBody();
}
}

This is explained in the CheckboxCell example. That example was written in answer to Resizing a form field in iTextSharp, but it also answers your question.
Note that your question remained unanswered for so long because you used the [itextpdf] tag instead of [itext] or [itextsharp]. I only found this question by coincidence.

Related

OpenPDF: Table not located in footer

When I add a table to the footer of the page, the footer resizes to the right size, however, the table does not stay within this footer, however it locates itself at the top of the page.
I have created a test scenario to illustrate what I mean.
public class TestClass {
public static void main(String[] args) {
try {
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("fail.pdf"));
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.addCell(new PdfPCell(new Paragraph("CONTENT")));
table.addCell(new PdfPCell(new Paragraph("CONTENT")));
Paragraph footerParagraph = new Paragraph();
footerParagraph.add(table);
HeaderFooter footer = new HeaderFooter(footerParagraph, false);
footer.setAlignment(Element.ALIGN_CENTER);
document.setFooter(footer);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
}
In this example the the footer has the correct size for the table:
However as mentioned the table is not located at the bottom, but at the top:
You need to know the page size and calculate from there.
You can use
showTextAligned(ELEMENT.ALIGN_BOTTOM)
This was a bug with OpenPDF, which should have been fixed now.
https://github.com/LibrePDF/OpenPDF/issues/373

itext - setSimpleColumn unable to understand llx,lly,urx,ury

I am developing a simple invoice generation software for my store.
I am trying to print the final things in a PDF using iText but I am not able to work properly with the method setSimpleColumn that I am using with ColumnText.
I am not able to understand the concept of llx,lly,urx,ury but I am trying through hit n try method but not able to get any result
This is my code
public class CreateInvoicePdf {
public static final String DEST="C:\\Users\\sanju\\Desktop\\resul.pdf";
public static void main(String args[]) throws FileNotFoundException, DocumentException
{
File file = new File (DEST);
file.getParentFile().mkdirs();
new CreateInvoicePdf().createPdf(DEST);
}
public void createPdf(String dest) throws FileNotFoundException, DocumentException
{
Document d = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(dest));
d.open();
PdfContentByte can = writer.getDirectContent();
Paragraph companyDetails = new Paragraph("CLIMAX EXCLUSIVE\nShop No. G1 G2 Mohan Bazar\nCentral Market Ashok Vihar\nNew Delhi-110052");
Paragraph invoiceDetails = new Paragraph("Invoice No : 1234\nDate : 10/05/2018\nPay : CASH");
PdfPTable itemTable = new PdfPTable(9);
itemTable.addCell("1");
itemTable.addCell("Shirt");
itemTable.addCell("1999");
itemTable.addCell("1");
itemTable.addCell("1999");
itemTable.addCell("12");
itemTable.addCell("1500");
itemTable.addCell("250");
itemTable.addCell("250");
ColumnText ct = new ColumnText(can);
ct.setSimpleColumn(100,200,300,400);
ct.addElement(companyDetails);
ct.go();
ct.setSimpleColumn(400,200,600,700);
ct.addElement(invoiceDetails);
ct.go();
ct.setSimpleColumn(100, 700, 500, 500);
ct.addElement(itemTable);
ct.go();
d.close();
writer.close();
}}
and the output I am getting is weird.
I want the company details and address to be on the upper left corner
The invoice number date and details at upper right corner
The invoice items table below these

iTextPdf - Writing on same page's copy multiple times and merging them

I have a pdf template on which I have to write, say the number of person visited a place along with other information. The pdf template has 25 rows.
So, if the count of people visiting the place is more than 25, I need to append a copy of the same pdf template as page 2 which will contain the entry for person 26 till 50. My current set up is :
reader = new PdfReader(PATH_TO_PDF_TEMPLATE);
stamper = new PdfStamper(reader, WRITTEN_FILE_PATH);
I want to use the same PDF Template again and append to the written 1st page in case the number of people is more than 25.
Here is a simple example of table which has 21 rows per page. You can set cell.setFixedHeight(33.68f) (bigger than 33.68) cell height to get 25 rows per page.
public class SimpleTable {
public static final String DEST = "C:\\Users\\krezus\\Desktop\\simple_table.pdf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SimpleTable5().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
table.setSkipFirstHeader(true);
System.out.println(PageSize.A4.getHeight()/25);
table.setSkipLastFooter(true);
for (int i = 0; i < 350; i++) {
PdfPCell celltable = new PdfPCell(new Phrase(Integer.toString(i)));
celltable.setFixedHeight(33.68f);
table.addCell(celltable);
}
document.add(table);
document.close();
}
}
I have used PdfPageEvent.onStartPage() and onEndPage().
writer = PdfWriter.getInstance(document, exportOutputStream);
writer.setPageEvent(pageEventHandler);

How to write Marathi/Hindi text to PDF using itext library in java?

I am using itext library for creating pdfs in java. I need to write Marathi/Hindi text to the PDF.
Please refer following similar SO Question.
itext Marathi (indian) language display issue
I also have the same issue. Since, this is an old post, is there any support in itext as of today?
The solution mentioned in the above SO post to draw marathi text as image, since I will be having multiple such marathi texts in different tables, cells etc, so it might be difficult to draw them at exact x and y locations as data will vary.
Below is my code
public class MarathiFont {
public static final String FONT = "/home/Documents/Lohit-Devanagari.ttf";
private static String RESULT = "/home/Documents/MarathiPdf.pdf";
public static void main(String[] args) throws Exception{
Document document = new Document();
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
Font font = FontFactory.getFont(FONT,BaseFont.IDENTITY_H,true);
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell customerLblCell = new PdfPCell(new Phrase("जन्मतिथि ",
font));
table.addCell(customerLblCell);
document.add(table);
document.close();
}
}

How to append the text "Continuation of ..." at the new page, when an element got split by setKeepTogether(true)

When an Element gets moved to a new page because of a setKeepTogether(true) configuration, I need to a text "Continutation of ..." to the new page.
I tried that with the PageEvent.onStartPage() but it's not allowed to call Document.add() inside a PageEvent.
But how to solve that now?
This small example reproduces my problem:
public class Main {
public static String continuation = null;
public static void main(final String[] args) throws Exception {
final Document document = new Document(PageSize.A7);
final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("text.pdf"));
document.open();
// Now I add the introduction text
document.add(new Paragraph("A nice \nIntroduction \nover some lines."));
// Now I put my "huge" paragraph. When it breaks,
// the first line of the new page should be "Continuation of smth."
continuation = "smth.";
final Paragraph paragraph = new Paragraph("What is \nthe answer \nto life the \nuniverse and \neverything?\n\nThe Answer to \nthis question \nis:\n42");
paragraph.setKeepTogether(true);
document.add(paragraph);
document.close();
}
}
The result should look somehow like this.
This can be hacked by utelizing the headerRows of a PdfPTable.
It is really a hack, maybe someone else has a better idea to reach the goal:
public static void main(final String[] args) throws Exception
{
final Document document = new Document(PageSize.A7);
PdfWriter.getInstance(document, new FileOutputStream("text.pdf"));
document.open();
// The content gets wrapped into an table. If the table breaks
// we utilize the "headerRows" to print `Continuation of xx`.
final PdfPTable table = new PdfPTable(1);
table.setHeadersInEvent(false);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
table.setWidthPercentage(100);
table.getDefaultCell().setBorder(0);
table.getDefaultCell().setPadding(0);
// This is the header we print ONLY if the page breaks.
table.addCell("Continuation of " + "smth.");
// Now I add the introduction text
final PdfPTable firstText = new PdfPTable(1);
firstText.addCell("A nice \nIntroduction \ninto \"smth.\" \nover some lines.");
// in the original code i add some other things here as well
table.addCell(firstText);
// Now I put a "huge" paragraph (it will not fit on the first page
final PdfPTable secondText = new PdfPTable(1);
secondText.addCell("Force \na pagebreak\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf\nasdf.");
// in the original code i add some other things here as well
table.addCell(secondText);
document.add(table);
document.close();
}
The code generates the correct PDF. But the way is really ugly...
I've uploded the PDF here.

Categories

Resources