Wednesday, October 26, 2005

Using iText on the new Website (Reference)

Today I read an interesting blog post regarding Java PDF interaction with the iText package (I noticed this package for the first time in Rod Johnson´s book Expert One-on-One J2EE Design and Development (Programmer to Programmer) but never had the full time to experiment But I noticed this is a good interesting starting point to check out. The blog URL is on September 21st and titled Nontheless I copy the same contents as this blog for inmediate reference. Also notice that it is quoted and formatted to fit this blog needs.

Using iText on the new Website

I saw a blog the other day about using iText to manipulate PDFs. I have really wanted to take a look at iText, but just have not had the chance. So thanks to this blog, I finally took the time to look at it and really liked it. After checking out the related article, it took me all of 30 minutes to learn iText and replace SourceBeat?s current PDF encryption code (originally pdftk). It was too easy. Below is an example and unit test showing how SourceBeat is now using iText.

package com.sourcebeat.util;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class EncryptorTest extends TestCase {

protected final Log log = LogFactory.getLog(getClass());
protected Encryptor encryptor;
protected String unencryptedFile = "test/unencrypted.pdf";
protected String encryptedFile = "test/encrypted.pdf";

public void setUp() throws Exception {

encryptor = new Encryptor();

public void tearDown() throws Exception {

encryptor = null;

public void testEncrypt() throws Exception {

// Encrypt the file
encryptor.encrypt(unencryptedFile, encryptedFile, "userPass", "ownerPass");

// Make sure that the file was written
File newFile = new File(encryptedFile);

// remove the file

It is pretty simple. As you can see Encryptor.encrypt() method takes the path to the original unencrypted file, the path to the output (the new encrypted file), and the user and owner passwords. You can see this method below:

package com.sourcebeat.util;

import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;


public class Encryptor {

public void encrypt(String inputFile,
String outputFile,
String userPassword,
String ownerPassword) throws Exception {

PdfReader reader = new PdfReader(inputFile);
new FileOutputStream(outputFile),
| PdfWriter.AllowDegradedPrinting
| PdfWriter.AllowFillIn
| PdfWriter.AllowModifyAnnotations
| PdfWriter.AllowPrinting
| PdfWriter.AllowScreenReaders);

Easy enough?you first have to create a PdfReader using the passed in input file path and then invoke the PdfEncryptor.encrypt() method. The parameters passed to this method are relatively self-explanatory, with the exception of the third parameter, false. The third parameter is a boolean, which represents the encryption strength (true for 128 bit key length, false for 40 bit key length). That is it. It is now part of the, soon to be released, new site.

I hope to take enough time to test this and perhaps it could be invaluable in my near future Java projects.

Monday, October 24, 2005

An example of Firebird using Mono

Today I read an article I found in news in entry "05-Oct-2005 Miguel de Icaza's web log provides a set by step description of setting up classic Firebird on Linux for use as an embedded database with Mono.", then I followed the link and seemed to me very interesting. You can link to the Miguel de Icaza's web log by clicking here. There you can notice how to use the embedded Firebird SQL server to use inside the Mono project (.NET for Linux). More links are there for you to follow.

By the way, Firebird is to me the best Open Source SQL server as it is very simple to administer and use, and it supports the full ACID database model. Other competitor like MySQL is about releasing this functionality. In my opinion, Firebird has very solid enterprise features.

Friday, October 21, 2005

My blogsphere site

This is my first post ever to the blogsphere. I am from a Spanish background, in fact I am from Medellin, Colombia, South America, but preferred to post in English as I like the language and in fact English is the universal one. But sometimes when it is necessary to do I would post Spanish items.

Hope this is a stop for the ones who land here.