Archive for Juli 21st, 2009

I just managed to update my HelloWidget Tutorial after Andreas Kompanez review (thx 4 that).

Go to Android: HelloWidget Tutorial to get the updated turorial and sourcecode. I consider writing a small eBook. I will probably add every new step I make to Android widgets and hopefully get some kind of eBook out of it. I keep you in the loop.

I finally found a very good open source combination for creating PDF-template files and using them with iText in a java project.

Just go here and get OpenOffice. Create a new document with OpenOffice Text. Go to “View > Toolbars” and select “Formcontrols” (hope it’s right, I’m using the german version and I’m translating). Now you can add form controls to your document like a textfield i.g. Right click on that field and select “control field …”
The properties window of that control will pop up and you can set a name to identify this control. I’ll give it the name “firstTextField”.
Now click the “Export PDF-Button” at the top toolbar of OpenOffice and save the document as PDF somewhere.

Now, go to your java project, don’t forget to import your iText libs and manipulate your previously saved pdf file like this:

1
2
3
4
5
6
7
PdfReader reader = new PdfReader("formfields.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("formfields_itext.pdf"));
 
AcroFields form = stamper.getAcroFields();
form.setField("firstTextField", "Setting text with iText into form control.");
stamper.close();

To make sure your user is not able to change any values in that form, you have to set the property “read only” of the field control in OpenOffice to true. iText can still access this field and set the text but using a PDF-Reader, the user cannot change it. If you’re using this mechanism to create pdf-templates your might not want to have frames around your text field. You can set frames to “no frames” in the property window as well.

I also had the problem of adding a table to an existing PDF which is not possible with form fields. Although the book iText in Action covers a lot important stuff, this problem is not explained in detail. Here’s how you add a PdfPTable to your existing pdf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
// use the stamper from previous code but don't close it
// parameter is the pageNo starting with 1 for the first page
PdfContentByte contentByte = stamper.getOverContent(1);
 
// add PDFTable
float[] widths = {0.23f, 0.23f, 0.23f, 0.23f};
PdfPTable table = new PdfPTable(widths);
 
// there is no default width so you have to set it
// otherwise you'll get an exception (table width can't be zero)
table.setTotalWidth(reader.getPageSize(1).getWidth() - 50f);
 
PdfPCell cell;
cell = new PdfPCell(new Paragraph("Headline Cell 1", STANDARD_FONT_BOND));
cell.setBorder(1);
table.addCell(cell);
 
... add more cells
 
// INFO: 0,0 is the bottm left corner of a pdf-page
table.writeSelectedRows(
0,  // 0 = from line 1
-1,  // -1 = to last line
20.0f,  // X-Position (from left border)
300.0f,  // Y-Position  (from bottom to top)
contentByte);
 
stamper.close();

Enjoy the open source pdf-templating.