Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / May 2008



Tip: Looking for answers? Try searching our database.

Adding an image at a specified place in an existing PDF doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wezcornell2008 - 29 Apr 2008 22:07 GMT
Hi Guys

Does anyone know how to add an image to an existing PDF document where we have left a space for an image to be included programatically?

All the best

Wez
-==cfSearching==- - 30 Apr 2008 02:03 GMT
If you do not mind dipping into java, you could use iText.  It is built into
MX7.  

The tutorials should get you started. Though they are now a tad dated.
http://itextdocs.lowagie.com/tutorial/objects/images/
wezcornell2008 - 30 Apr 2008 08:23 GMT
Thank you for your reply. I had found IText before but wondered if there was as near pure CF solution as poss.
-==cfSearching==- - 30 Apr 2008 14:12 GMT
Unless you are generating the pdf's with cfdocument [i]and[i] add the image
when the files are generated, I think iText is about as close as it gets.  
Besides, iText is what cfdocument uses behind the scenes to generate pdf's
anyway.
BKBK - 30 Apr 2008 19:15 GMT
Hi Wez,

How many pages does the PDF have? What is the image type, i.e. jpg, tiff, png?
How do you wish to add the image, for example, on which page(s)? Where on the
page is the space you reserve for the image? For example, what are the
coordinates of the top-left corner of the rectangle?
BKBK - 01 May 2008 04:55 GMT
Another look at your post tells me you probably want to insert the image on a
single page. Here then is some code that should do it. I have included
alternatives, for example, how to insert the image on every even-numbered page.

<!---
Inserts an image into an existing PDF document.
--->
<!---  get the image --->
<cfset image =
createobject("java","com.lowagie.text.Image").getInstance(expandPath("foobar.jpg
"))>

<!--- absolute position of image in the PDF --->
<!--- (coordinates relative to bottom-left corner of page) --->
<!--- adjust these numbers(floats) till you get correct image position --->
<cfset xPosition = 100.0>
<cfset yPosition = 273.8>

<cftry>
    <cfscript>
    // input PDF in which to insert image
    inputPDF="myDoc.pdf";
   
    // output PDF containing image
    outputPDF=replaceNoCase(inputPDF,".pdf","_with_img.pdf");
   
    // set up required objects
    // (making use of iText JAR library included in Coldfusion MX7)
    pdfReader=createObject("java","com.lowagie.text.pdf.PdfReader");
    pdfStamper=createObject("java", "com.lowagie.text.pdf.PdfStamper");
       
    // setup the new PDF

    newPDF=createObject("java","java.io.FileOutputStream").init(expandPath(outputPD
F));
       
    // read the input PDF
    reader=pdfReader.init(expandPath(inputPDF));
   
    // replace all local named links with actual destinations
    reader.consolidateNamedDestinations();
   
    // number of pages in the PDF (may not be needed)
    totalPages=reader.getNumberOfPages();
   
    // start process of adding extra content to original PDF document
    // (all interactive elements in the original PDF, e.g. bookmarks,
    // links and form fields stay intact)
    stamper = pdfStamper.init(reader, newPDF);
   
    // set image absolute position: required; coordinates are of float type
    image.setAbsolutePosition(xPosition, yPosition);
       
    // pdf document should of course have at least this number of pages
    pageNo = 8;
   
    /* insert image 'over' content, that is, superimpose image
      on any existing page content at the specified position. */  
    overContentByte = stamper.getOverContent(pageNo);           
    overContentByte.addImage(image);
   
    /* to superimpose existing page content(e.g. text) on the image, use instead:
*/
    // underContentByte = stamper.getUnderContent(pageNo);           
    // underContentByte.addImage(image);
   
    /* to insert the image 'over' content in even-numbered pages, use instead: */
    /* pageNo = 1;
    while (pageNo LTE totalPages) {
        if (pageNo MOD 2 EQ 0) {
            overContentByte = stamper.getOverContent(pageNo);           
            overContentByte.addImage(image);   
        }
        pageNo = pageNo + 1;
    }*/
   
    // close stamper, reader (the order important?)
    stamper.close();
    reader.close();
</cfscript>
    <p> Image successfully inserted into document
<b><cfoutput>#inputPDF#</cfoutput></b>. The result has been copied to
<b><cfoutput>#outputPDF#</cfoutput></b> (in same directory).</p>

<cfcatch type="any">
    <p>There was an error: <cfoutput>#cfcatch.message#</cfoutput></p>
</cfcatch>

</cftry>
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.