Archive for Juli, 2009

Surprisingly I never really had to use any Input- Outputstreaming with Java since a few days ago. … Well, ok, some FileInputStreams or some InputStreams from resources; but that’s not what I mean.

Playing around with iText I had to manipulate a few PDF-Templates as explained earlier under PDF-Templates for iText and then merge them together into one PDF-file. The problem was, that iText needs to have an OutputStream to put the content to. At first I was on the wrong track trying to use PipedStreams … but those only work between different threads, not in the same so I had to throw that idea away.

I came accross the ByteArrayOutput- and ByteArrayInputStreams. You just go ahead and create a ByteArrayOutputStream and give that one to the iText PdfReader. If you need the Stream again, you can now use it, manipulate it a 2nd time and in the end pass it to the user as a File (FileOutputStream) or through the browser using the response.getOutputStream().

The code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
PdfCopyFields copy = new PdfCopyFields(outputStream);
 
// walk through the readers and add several PDF-files to one document
for (PdfReader pdfReader : readers) {
  copy.addDocument(pdfReader);
}
copy.close();
 
// save result of this reader in memory
PdfReader read = new PdfReader(new ByteArrayInputStream(outputStream.toByteArray()));
 
// stamper to have access to AcroFields
PdfStamper stamp = new PdfStamper(read, finalOutputStream);
 
// fill pdf form fields with given stamper
fillPdfFormFields(stamp);
stamp.close();
 
outputStream.flush();
outputStream.close();
finalOutputStream.close();

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.

Yesterday we had a great famliy day (my g/f’s sister with kids and her parants) at Frankfurt zoo. Kids are so interessted in animals, it’s just awesome.
Right at the beginning of our zoo tour we passed the bear cages. So if you ever think YOU are chilling, think of this picture here:

Chilling sloth bear

Chilling sloth bear

The only thing missing in this picture is the thought bubble: “That’s how we role at the zoo, bro!”

I was so fascinated that I probably spend 15 minutes watching the bear doing just nothing.
Ok, just wanted to share my experience with you; there’s only one statement left to say:

chilling bear is … chilling!

Power at the gym

Juli 14th, 2009 No Comments

Wow,
after 6 weeks of low carb diet with a maximum of 2000 calories a day (inspired by the body of Jason Statham seen in the movie Death Race) and unfortunately one week of having a cold and being down, I started to change to “regular” food again. Last night I hit the gym and *BAM* … what a new feeling. I managed all my exercises without any Problems and actually had enough power for another round.
The difference to working out on the low carb diet compared to “regular” is amazing.

I’m also thrilled about the fact that the diet really works. I lost 8 kg (about 16 lbs) in 6 weeks. It’s easy if you really want it but having low (or almost ‘no’) carb means that you don’t eat any of the following food:

  • pasta
  • bread
  • rice
  • potatoes
  • cornflakes
You should create a list of foodstuff with its substances to quickly find out what you can/should eat or not. There are several websites where you can find those lists. Just use Google.
Being on this diet without doing anything else is probably pretty senseless so you should hit the gym and do some cardio exercises regularly. For workout; I had two training units (for different muscle groups), each one took about an hour. I hit the gym three to four times a week, played basketball 2 hours a week and went running outside once or sometimes twice a week.
You’ll have less power at the gym during that time and you might not accomplish your usual weight, repetitions and workout sets as usual … that’s ok!
My best friends concerning food during that diet were:
  • tuna (or fish in general)
  • turkey breast (or meat in general)
  • vegetables (mostly tomatoes, salad and cucumber)
  • cheese
  • mayonnaise as dressing (be careful with that, it has a lot of calories)
  • water (a lot of it ’cause it fills you up)
For breakfast I usually had an apple and a peach cut into pieces mixed with some yoghurt and sunflower seeds. Why the seeds? Low carb means, that you won’t have anything to bite. There are no crunchy things to bite on on a low carb diet so I had to put those seeds into my food. Try it out, it’s good.
The hard part is to have varied meals because your options are very limited. You need time to plan your meals, that’s for sure.
There’s no guarantee that this diet will work for. The daily calorie rate also depends on your actual weight and your metabolism plays a big part two. It worked for me and I’m pretty happy that I did it. I started at 90 kg and I’m down to 82 right now. I still try not to eat too much ’cause I want to get down to 79.
I’m not a sport student, physician or coach. I’m just interested in sports. I don’t have any special education that qualifies me to tell anyone what to do to loose weight or what he/she should do for sport. I just wanted to share my experience with you. If your have any questions, just write a comment to this post. I’d be happy if you share your experiences with me too.

I spent a lot of time looking for an android hello world widget how to but I couldn’t find a simple example so I had to spend some time on cutting an existing one into pieces and then start from scratch with a HelloWidget tutorial.

I hope this will help you to get started on android desktop widgets. Please give me some feedback about the tutorial and/or any errors or wrong explanations.

Download the tutorial here. Get the source code here.