views:

319

answers:

1

I used this technique to insert an image

http://stackoverflow.com/questions/697122/adding-a-dynamic-image-to-a-pdf-using-coldfusion-and-itext

Now, I need to insert a link to a external URL at X/Y and text inside with itext and coldfusion.

Can someone help me do this?

Thanks.

+1  A: 

Here is rough example that works with CF9. There are probably more elegant methods, but this should give you the basic idea.

Note - IIRC CF8 uses an earlier version of iText (1.4). CF9 uses 2.1.0. So I am relatively certain it will not run "as is" with CF8. If needed, you can use the JavaLoader.cfc to run a later version.

Update: Modified to show one way of defining a specific font, size and color. The correct settings will vary depending on your system, desired font, encoding, etcetera.

<cfscript>
     inputPath = "c:\sourceFile.pdf";
     outputPath = "c:\sourceFileWithLink.pdf";

     try {
        // initialize objects
        pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init( inputPath );
        outStream = createObject("java", "java.io.FileOutputStream").init( outputPath );
        pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init( pdfReader, outStream );

        // create a chunk with a link to www.google.com
        chunk = createObject("java", "com.lowagie.text.Chunk").init("Go To Google");
        chunk.setAnchor("http://www.google.com");

        //////////////////////////////////////////
        // Define embedded font 
        BaseFont = createObject("java", "com.lowagie.text.pdf.BaseFont");
        Font = createObject("java", "com.lowagie.text.Font");
        bf = BaseFont.createFont("c:/windows/fonts/Framd.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);

        // Create the main font object (size 14)
        Color = createObject("java", "java.awt.Color");
        textFont = Font.init(bf, 14, Font.UNDERLINE, Color.RED);   
        // Apply the font to the chunk text
        chunk.setFont( textFont );
        //////////////////////////////////////////

        // prepare to write the link onto the *first* page only        
        cb = pdfStamper.getOverContent(1); // first page
        ct = createObject("java", "com.lowagie.text.pdf.ColumnText").init(cb);
        ct.addElement( chunk );

        // position towards bottom right of page
        page = pdfReader.getPageSize(1);
        llx =  page.getRight()-200;   
        lly = page.getBottom();       
        urx = page.getRight();                
        ury = page.getBottom() + 36;     
        // initialize column dimensions
        ct.setSimpleColumn(llx, lly, urx, ury);
        // write the text
        ct.go();

        WriteOutput("Finished!");
    }        
    finally 
    {
        // cleanup
        if (IsDefined("pdfStamper")) {
            pdfStamper.close();
        }
        if (IsDefined("outStream")) {
            outStream.close();
        }
    } 
</cfscript>
Leigh
Hooo ..marvelous .. thats work !!Could you explain me how to change font, size, color and unlerline ?for text ?
Alain
Create a Font object and apply it to the Chunk holding the text. You will probably need to create a BaseFont and java.awt.Color object first. Then use them to create a Font() with the desired size, style and Color. http://api.itextpdf.com/com/itextpdf/text/Font.html
Leigh
Oops, that is the wrong version of the API (5.0.0). This one looks closerhttp://www.docjar.com/docs/api/com/lowagie/text/Font.html
Leigh
thanks .. but i have realy a probleme for translate Java in coldfusion :S
Alain
@Alain - I updated the example with a font. You may want to study it first. See what it is doing. It is never a good idea to use code you do not understand ;)
Leigh
Tanks,you are true ...This is my first time whis iText, I will strudy that, realy poverfull for PDF.This is my first time here , I need to .. close question ? hors somthings ?
Alain
Yes, if it answers your question you should close it by accepting it as the answer.
Leigh