Using iText PDF & ColdFusion

Mike Steele sent me an email in reference to an article I wrote for the ColdFusion Developer’s Journal a year or so ago. In the email, he mentions that he is trying to use the iText Java-PDF library with ColdFusion MX:

… The getInstance method is static and according to your July 2003 CFDJ article, you can’t instantiate an object in CF this way.

In the article I said this:

… using the CreateObject() function does not get you access to an instance of an object. In order to access a Java object, you must either a) first call the CreateObject() method and then the init() method, which in the above example, maps to the default constructor in Java, or b) call any nonstatic method on the object, which causes ColdFusion to then instantiate the object for you.

I guess this statement needs to be amended to include a third possible, but not always valid solution: call a static method on the class which returns an instance of the object in question. In this case the API designer included a static method ‘getInstance()’ on the PDFWriter class. Given that news, you can take the quick example that the author of the iText library gives here to create a PDF in a snap using ColdFusion:

// create a 'Document' object
document = CreateObject("java", "com.lowagie.text.Document");
// get an outputstream for the PDF Writer
fileIO = CreateObject("java", "");
// call the constructor, pass the location where you want
// the pdf to be created
// get a PDF Writer var
writer = CreateObject("java", "com.lowagie.text.pdf.PdfWriter");
// call the static 'getInstance' factory method
writer.getInstance(document, fileIO);
// open the document;
// create a new paragraph
paragraph = CreateObject("java", "com.lowagie.text.Paragraph");
paragraph.init("Hello World!");
// add the paragraph
// close the document (PDF Writer is listening and will automatically
// create the PDF for us

Copy that code into a cfml page and make sure you’ve downloaded the iText jar to the /lib/ directory of your ColdFusion server and you should be able to create PDF’s in a jiffy!

Full source code available here.

33 thoughts on “Using iText PDF & ColdFusion”

  1. Good work, Mr. Johnson. I wonder if you’d come up a sample for a related library: JasperReport with CFMX? I’ve tried but with no success. Thank you.

  2. I too I have tried to get JasperReports working with CF and have found that it doesn’t work due to problems with commons-logging version in campatiablities.

    Our work around was to make a standard Java web app and deploy that war file to the JRun server. Fortunatly we have CF Enterprise so we can do this since we have a full version of JRun.

  3. hi

    please do you can explain WHERE i need to install iText files? All files? In CFMX directory? Please, i’m a newbie in Java development and i’ll appreciate to know iText, that powerfull tool. Thanx in advance and congratulations for all resources…

  4. hi Spectrum,

    The iText.jar file needs to be copied to the {cfusion}/WEB-INF/lib/ directory… restart ColdFusion and you should be able to use it right away!


  5. thanks..
    Iam using cfml for generating reports.can u help me in writing the resultset to pdf is oracle9i and coldfusionmx.

  6. I have gotten and used jasperreports with CFMX. The key is setting your jave parameters to headless.
    I design with iReport and created a tag that executes the report and saves it to a pdf.

    I need to make this tag and stuff more stable, but if I get enough request, I might be motivated to get a doc written up and the tag done quicker.

  7. I have added a tag that takes a jasperreports xml file and turns it into a pdf file. You can find it on the Macromedia Exchange.. called Pyro_JasperReports.

  8. I copied all the *.java files into the {cfusionmx}/wwwroot/WEB-INF/lib/ directory and restarted the server.. However, I get the following error:
    Object Instantiation Exception.
    Class not found: com.lowagie.text.Document

    Anyone any idea what might be causing this???

    Paul – Java Newbie

  9. Don’t put the classes in the web-inf/lib/ dir – just put the whole jar file in there and the axample will work as written.

  10. Just put the Jar file in “CfusionMX\runtime\jre\lib” and also put an entry into ColdFusion Admin Classpath.

  11. Note that iText jars with ColdFusion 7 don’t work due to compatibility issues with the iText version already installed and used by the tag. You have to refactor iText with another name to use it on CFMX7.

  12. How do you refactor itext as you described? I stopped CF server, removed the old version, and replaced it with the new version and it seemed to work. MM said absolutely not to do this but I don’t use their CFDocument tag or reporting tags, so I don’t think it will be an issue? Any ideas, should I do this refactoring you mentioned?

  13. My latest troubles involve merging pdf form fields from within cold fusion, can someone paste the code that makes this possible? I’ve expermineted with tons of variations, but would like someone elses fresh mind helping.

  14. Thanks for the script – it took me a long way towards my own project, which i’ve included the code for.

    Have you got any ideas on how to use the table.writeSelectedRows(0, -1, 50, pos, writer.getDirectContent()); as I get an error

    Class can not access a member of class com.lowagie.text.pdf.PdfWriter with modifiers “protected”

    // create a ‘Document’ object
    rectangle = CreateObject(“java”, “com.lowagie.text.Rectangle”);
    pageSize = Rectangle.init(240, 155);

    Chunk = CreateObject(“java”, “com.lowagie.text.Chunk”);
    Element = CreateObject(“java”, “com.lowagie.text.Element”);

    CMYKcolor = CreateObject(“java”, “com.lowagie.text.pdf.CMYKColor”);
    document = CreateObject(“java”, “com.lowagie.text.Document”).init(pageSize, 0, 0, 0, 0);
    Image = CreateObject(“java”, “com.lowagie.text.Image”);

    BaseFont = CreateObject(“java”, “com.lowagie.text.pdf.BaseFont”);
    Font = CreateObject(“java”, “com.lowagie.text.Font”);
    arial = BaseFont.createFont(#expandpath(“TGL.TTF”)#, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
    arial = Font.init(arial, 12);

    PdfPCell = CreateObject(“java”, “com.lowagie.text.pdf.PdfPCell”);
    PdfPTable = CreateObject(“java”, “com.lowagie.text.pdf.PdfPTable”);

    fileIO = CreateObject(“java”, “”);
    // call the constructor, pass the location where you want
    // the pdf to be created
    // get a PDF Writer var
    writer = CreateObject(“java”, “com.lowagie.text.pdf.PdfWriter”);
    // call the static ‘getInstance’ factory method
    writer.getInstance(document, fileIO);

    // open the document;
    // create a new paragraph
    paragraph = CreateObject(“java”, “com.lowagie.text.Paragraph”);
    phrase = CreateObject(“java”, “com.lowagie.text.Phrase”);

    phrase0 = paragraph.init(” “);

    phrase = paragraph.init(“”, arial);

    phrase2 = paragraph.init(“#url.jobtitle#”, arial);

    phrase3 = paragraph.init(“#url.clubname#”, arial);

    phrase4 = paragraph.init(“#url.address1#”, arial);

    phrase5 = paragraph.init(“#url.address2#”, arial);

    phrase6 = paragraph.init(“#url.postcode#”, arial);

    phrase7 = paragraph.init(“T : #url.telephone#”, arial);

    phrase8 = paragraph.init(“F : #url.fax#”, arial);

    phrase9 = paragraph.init(“E :”, arial);

    phrase10 = paragraph.init(“”, arial);

    //add the image
    png = Image.getInstance(“#expandpath(“businesscardside1.tif”)#”);
    png.setAbsolutePosition(0, 0);
    png.scaleToFit(240, 156);


    table = PdfPTable.init(1);
    cell = PdfPCell.init(Paragraph.init(“”, arial));
    cell.setBorderWidth( 0.0 );
    table.getDefaultCell().setBorderWidth( 0.0 );
    table.addCell(” “);
    table.addCell(” “);
    table.addCell(” “);

    cell = PdfPCell.init(Paragraph.init(“”, arial));
    cell.setBorderWidth( 0.0 );


    png1 = Image.getInstance(“#expandpath(“businesscardside2.tif”)#”);

    png1.setAbsolutePosition(0, 0);
    png1.scaleToFit(240, 156);

    // close the document (PDF Writer is listening and will automatically
    // create the PDF for us

  15. i also get the

    Object Instantiation Exception.
    Class not found: com.javaimage.ImageObj

    i have all the files in the root.should i changed the folder structure…
    pls make a reply

  16. You should be able to run both the CFMX 7 cfdocument tag as well as the iText.jar by placing the itext-1.4.jar in a “higher” lib folder (e.g. C:\JRun4\jre\lib). In fact, I’m running Fop, cfdocument, and now iText all on the same CFMX 7 server.

  17. Aaron, great post. Do you have an example of READING a pdf using the PdfReader? I’m trying something simple like:


    But I’m having no success

  18. Has anyone experienced issues with generating a pdf file with itext that is bigger than exactly 14 megs. It seems to work fine until it reaches that 14 meg threshold.


  19. Has anyone tried using iText to silently print pdfs? I have a number of PDFs that the user can select using checkboxes. Upon clicking a button I want to send those PDFs to the users default local printer without any further dialogue boxes. There is code out there that pertains to this, but I’ve not been able to get anything working.


  20. Please i’m just a newbee in java ,in my project i have one problem …
    After the execution of my program ,this is located in a given location ,but my wish is to open it automatically …
    PLZ how can i do it ?

    thank a lot for your support .

    Os :fedora 8,netbeans 6.5

  21. Sorry i wanted to say that my pdf file is located in a given location ,but i dont want to browser there and open it manually ….

  22. I investigated using iText with ColdFusion and have had great success serving PDFs from my CFM that I generated myself with iText. Not only does it work, but it’s ridiculously easy. I’m new to ColdFusion and Java and was pleasantly surprised to see how easily and well the two played together.

    However, in order to create headers and footers on each page, I found I have to extend the PdfPageEventHelper class and add an override to the method OnPageEnd( ), and attach that helper to my instance of the PDFWriter. This is fine, except it’s not obvious how to do this in CF. Putting a class declaration inside the where I do everything else made CF choke (unsurprisingly).

    This is roughly what I’m trying to do in Java:

    class HeaderFooterPageEventHandler extends pdfPageEventHelper {
    public void onEndPage( PdfWriter writer, Document document ) {
    header = Phrase.Init( “This is the header of the document.” );
    footer = Phrase.Init( “This is the footer of the document.” );

    center = ( document.right( ) – document.left( ) ) / 2;

    ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, header,
    center + document.leftMargin( ), ) + 10, 0 );

    ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, footer,
    center + document.leftMargin( ), document.bottom( ) – 10, 0 );

    I’m looking into using to implement this class so I can use it in my CFM. Is this the right way to do it? It is even possible? I’d hate to have to make my own jar just to do this simple thing, but I imagine that wouldn’t be too bad.

    I’m still a little hazy about the relationship between ColdFusion components and the Java objects I know they are underneath, and how interchangeable they are with non-CF Java objects. The idea of extending CF with Java is very exciting and I imagine will be very handy for me in the future.

    Many thanks,


  23. I want the code to read the pdf file and store it into text document( .txt file) using iText … Can you help me for this…?
    Its urgent…

  24. @Iain B – it’s been a long time since your post, but I’ve been having the same problem. After some searching on the interwebs, the best answer I have been able to find is Paolo’s statement “That method was always public. Whatever is happening it’s not iTexts fault.”

    This appears to be the problem for me. If I define writer as
    then I get the error.

    However, if I define oWriter as

    Then I don’t get the error. Both approaches work for everything else (and I have seen the first approach used a lot in Coldfusion examples online), but I think in the first case you are getting a reference to the static class, and in the second case you are getting a reference to a PdfWriter object.

Leave a Reply

Your email address will not be published. Required fields are marked *