How to Add a link to a PDF with ColdFusion and iText

醉酒当歌 提交于 2019-12-24 11:25:12

问题


I used this technique to insert an image

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:


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>


来源:https://stackoverflow.com/questions/2226993/how-to-add-a-link-to-a-pdf-with-coldfusion-and-itext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!