Skip navigation
mjp420
Currently Being Moderated

CFPDF - adding watermark image with a transparent background (png or gif)

Jun 25, 2009 9:41 PM

Hi,

 

I'm have trouble getting an added watermark with a transparent background to display properly when adding it to a pdf using <cfpdf>.  My code below:

 

<cfpdf
    action = "addwatermark"
    source = "sample_ddx.pdf"
    image = "starburst.png"
    foreground = "yes"
    isBase64 = "yes"
    overwrite = "yes"
    pages = "1"
    showonprint = "yes"
    position = "0,0"
    opacity = "10"
    destination = "sample_ddx_startburst.pdf"
>

 

I've tried changing isBase64, foreground, etc and it always changes the transparent part of the gif or png to white.  Has anybody ever been able to get this to work?  I haven't been able to find anthing in the doc's that say it isn't supported...

 

Thanks for any info anyone might have....

 

-Michael

  • Currently Being Moderated
    Community Member
    Jun 25, 2009 11:43 PM

    Hi,

     

    Please try this,

     

    <cfpdf
            action="addwatermark"
            source="sample_ddx.pdf"   
            image="./test.jpg"
            destination="#expandpath("newpdf")#/watermarkforeground.pdf"
            overwrite="yes"
            pages ="1,3,6-8"
            rotation="45"          
            >

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 26, 2009 1:48 AM

    I ran a few tests and did not have much luck with transparent images either.  If you cannot get it to work with cfpdf, you might consider trying to add the watermark with iText (used the hood for cfdocument).  See if it makes a difference.

     

    <cfpdf

    ....
        opacity = "10"
    >

     

    I assume you just testing different settings, as "10" means totally opaque.

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 26, 2009 1:51 AM

    catchme_dileep wrote:

     

    Hi,

     

    Please try this,

     

    What is the difference between that and the original code?

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 26, 2009 3:02 PM

    ColdFusion uses iText for cfdocument. So you do not need to add jars. It is already built in. But you do need access to createObject(..).  So that might eliminate this option for you.You might also try using ddx with <Watermark> to see if the results are any different.

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 27, 2009 12:02 PM

    No, I believe the two options for ddx <Watermark> are <StyledText> and <Pdf Source="somePDFFile.pdf">.  So the image would need to be placed inside a pdf file.   Though given that CF does not support all of the ddx features I do not know if two watermarks will be allowed:

     

            http://livedocs.adobe.com/livecycle/8.2/ddxRef/001162.html#1557141

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 27, 2009 1:39 PM

    mjp420 wrote:

     

    Actually, i am able to add both watermarks.  One via ddx and one via cfpdf.  See the attached pdf that was created this way.  The 3 lines of text in the blue area in the upper right was were added with ddx.  The "new" logo in the lower left hand area was added to that pdf with cfpdf.

     

    If i could only get that added logo to allow transparency i'd be set...

     

    Thanks...

    No, what I meant was I did not know if you could do both _with ddx_.  I do not know one way or the other.  Just that the livecycle documentation suggested it might be an issue.

     

    I have had zero luck with transparency in my tests of cfpdf/ddx. The only thing that worked for me was iText.  If you are allowed to use createObject(...) try the attached.  I tested it with the pdf you posted and it definitely works on CF8.

     

    <!---
        Adaptation of iText examples:
    --->
    <cfscript>
        savedErrorMessage = "";
       
        fullPathToInputFile = ExpandPath("sample_ddx_startburst-1.pdf");
        fullPathToWatermark = "c:\myTransparentPng.png";
        fullPathToOutputFile =  ExpandPath("startburstWatermarked.pdf");

     

        try {
            // create PdfReader instance to read in source pdf
            pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(fullPathToInputFile);
            totalPages = pdfReader.getNumberOfPages();

     

            // create PdfStamper instance to create new watermarked file
            outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
            pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(pdfReader, outStream);

     

            // Read in the watermark image
            img = createObject("java", "com.lowagie.text.Image").getInstance(fullPathToWatermark);

     

            // adding content to each page
            i = 0;
            while (i LT totalPages) {
                i = i + 1;
                content = pdfStamper.getOverContent( javacast("int", i) );
                img.setAbsolutePosition(0, 0);
                content.addImage(img);
                WriteOutput("Watermarked page "& i &"<br>");
            }

     

            WriteOutput("Finished!");
        }       
        catch (java.lang.Exception e) {
            savedErrorMessage = e;
        }
        // closing PdfStamper will generate the new PDF file
        if (IsDefined("pdfStamper")) {
            pdfStamper.close();
        }
        if (IsDefined("outStream")) {
            outStream.close();
        }
    </cfscript>
       
    <!--- show any errors --->
    <cfif len(savedErrorMessage) gt 0>
        ERROR - Unable to create document
        <cfdump var="#savedErrorMessage#">
    </cfif>

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 27, 2009 2:26 PM

    I am surprised about a time increase.  I ran it with the file you posted and it was pretty quick, as the input file is not that large.  But in any event, I am glad it works for you.

     

    Cheers

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 27, 2009 4:11 PM

    BTW, Did you submit a bug report on this?  It is either a bug in the behavior or documentation.

    |
    Mark as:
  • Currently Being Moderated
    Jun 28, 2009 3:49 PM

    Hi Mike,

    This might help you out, because im not sure where would you get your image, whether its uploaded, or its already on the server. But I will asume that you already have the photo.

    Use the cfimage tag to write the photo in an empty photo variable in which you would just simply upload as it is with your file. Now, you can first read the image using cfimage into a variable then resize it, or manipulated using image() methods, finally read it with cfimage again and write it into a variable.

    example:

    <cfimage action="read" yada yada>

    now that we have read the image as object

    lets resize it:

    <cfset ImageSetAnitialising(myImage, "on")>

    <cfset ImageScaleToFit(myImage, 45, 45>

    <cfif myImage.width GTE myImage.height>

    <cfset x = 0>

    <cfset y = ceiling((myImage.width - myImage.height)/2>

    <cfelse>

    <cfset x = ceiling((myImage.height - myImage.width) / 2>

    </cfif>

    <!-- below we will reset the images size AND BORDER so make sure that if you want to

    have another color than WHITE to specify it -->

    <cfset newimg = ImageNew("", 45, 45, "rgb", "FFFFFF")>

    <cfset ImagePaste(newimg, myImage, x, y)>

    <!-- here is where we write the image to the place holder image that we have which is pro#Number#>
    <cfimage source="#newimg#" action="write" destination="images/Pro#randNum#.jpeg" overwrite="yes">

    I hope that helps

    Please let me know how that went for ya

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 28, 2009 4:25 PM

    CF_Ali wrote:

     

    Umm.. how does that help with the watermarking and transparency issues?

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jun 28, 2009 5:07 PM

    I do not know.  I am just happy when the login option works.  It seemed to malfunction a few posts back and now I cannot even go back and correct my awful typing as there is no "Edit" option on those posts ;-)

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jul 13, 2009 6:45 PM

    mjp420,

     

    BTW, this issue appears to be fixed in the CF9 beta.

     

    -Leigh

    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points