Menu

How to Manipulate/Remove Existing Watermark in PDF

Help
OnSite
2014-10-21
2014-10-27
  • OnSite

    OnSite - 2014-10-21

    Hi All,

    I am trying to remove the Existing Watermark content, for that first of all i am fetching the Container data like below.

    PDForm form;
    ICOSContainer cont = form.cosGetStream().getContainer();

    and i can print this also, but i can't change it/Delete it, Please help me in this case.

     
  • Elfi Heck

    Elfi Heck - 2014-10-21

    Let's assume that you have a legal reason for wanting to remove a watermark.

    A watermark is usually a text or an image added to a form or page's content stream. What it is exactly or how it is inserted can vary in different PDFs. Therefore there's no "one-size-fits-all" method for manipulating them.

    To learn about content streams read first the relevant section(s) in the PDF reference (now ISO 32000). It's available on the web, just search for "adobe pdf reference". You should always have a copy of that document ready when dealing with PDF internals.

    To get at the content stream of a PDPage or PDForm object in jpodlib, call getContentStream(). cosGetStream() will get you only the low level object, but you'll probably want a CSContent object created from it. After you've done what you want with that you can put it back into the page or form with setContentStream().

    See also the examples (also the one's from the jpodrenderer project) for what you can do with content streams.

     
  • OnSite

    OnSite - 2014-10-22

    Hi Elfi Heck,

    Thanks for your reply, i have added watermark to the empty pdf using [b]watermark.java[/b] present in content folder in jPodlib. i think you know the way it added.
    Now, i want to clarify that this watermark is added from jPodlib(It was an empty PDF before adding watermark) and now want to remove using jPodlib only because we don't want to use another lib.
    Please let me know the straight forward code to remove it.
    and Please clarify that, can we remove that watermark which is added using jPodlib, if not why is it so???? , if yes then how can it be done and what the steps to follow.

    Attached code for your reference, you have give 2 arguments in cmd prompt to run this, i have used eclipse.

     

    Last edit: OnSite 2014-10-22
  • OnSite

    OnSite - 2014-10-22

    Hi Elfi Heck,

    Please find the attched PDF,

    step 1:
    inupt.pdf used for watermark.java(which is a example to add watermark in jPodlib) as input file, result is stored as output.pdf which is also attached.

    step 2:
    output.pdf using as input to Removechanges1.java which is attached in above post, i want to remove the watermark "Test PDF"(which is added in above step) and store it as removechanges1.pdf.

    Please let me know if you need any info.

     
  • Elfi Heck

    Elfi Heck - 2014-10-22

    Yes, the watermark added by the example code can be removed again.

    If you look at the code creating it you will see that the page contents are converted to an array and then the watermark stream is added to the content array. Removing the watermark is as simple as removing the last array element.

    COSArray contentArray = (COSArray) pdPage.cosGetContents();
    contentArray.remove(contentArray.size() - 1);
    
     
    • OnSite

      OnSite - 2014-10-22

      Hi Elfi Heck,

      Thanks, that piece of code works well and good.

      Let's elaborate my doubt, while testing your code. what i understood from this piece of code is contentArray.remove(contentArray.size() - 1); will remove last indexed data.

      so, let me say i have added watermark 5 times using watermark.java okay, if i use the above code it'll remove only one watermark out of 5 (I have tested it) Then how to remove all at once, my ques. is can't we identify all the watermarks/or Any one of them particularly to remove/Manipulate them at a time. is it possible????

      if i keep your suggested functions in loop it'll delete all the data.

      Here are the attached .java files i have tried out.

       

      Last edit: OnSite 2014-10-27
  • Elfi Heck

    Elfi Heck - 2014-10-22

    Let me repeat my earlier advice on reading up on content streams in the pdf reference.

    To not remove all the content on the page you would have to first get the stream in question then check its bytes if they look like a watermark and only remove it if this is the case. To make this check easier you could already prepare for it when creating the watermark by including some kind of marker that you can recognize later. For example, you could give the form inside the watermark a unique name that's easy to recognize

    creator.doXObject(COSName.create("ThisIsMyWatermark"), form);
    

    then look for this in your remove loop.

     
  • OnSite

    OnSite - 2014-10-27
     

    Last edit: OnSite 2014-10-27
  • OnSite

    OnSite - 2014-10-27

    Hi Elfi Heck,

    i tried this in adding watermark.java, so that it'll add an Marker for watermark content that we are adding.

    creator.doXObject(COSName.create("WatermarkName"), form);
    

    After this in another program i am trying to Fetch the Marker and print it as below (I'll delete once i get that Watermark content).

    COSObject object1 = (COSObject) page.cosGetField(COSName.constant("WatermarkName"));
    System.out.println(object1.toString());
    

    After that i am trying to print it but it returns null, i am using same Correct PDF.

    i tried below code also,

    CSContent cont = (CSContent) page.getContentStream();
    System.out.println(cont.toString());
    

    As a result i am able to see the Marker we added but can't access that Watermark content.

    i am able to add doXObject but i am not able to remove watermark using that ID.

     

    Last edit: OnSite 2014-10-27
  • OnSite

    OnSite - 2014-10-27

    This is the scenario,
    in 1st stage i have PDF with some content, 'll add watermark and after that i may add plenty of data continuously.
    in 2nd stage i want to remove/modify that watermark's content with something else, which is entirely different.

    i want to do this entire thing using jPod, because i have already added a watermark and marker to track it.
    now i couldn't get that doXObject to delete/manipulate, please help or piece of code would be better.

     
  • Elfi Heck

    Elfi Heck - 2014-10-27

    See above answer. What you're describing is surely possible with jPod, and it's actually not hard to do, but I cannot write all the code for you. That's beyond what this forum is for.

    You can contact support@intarsys.de if you want to hire one of us to help you.

     
  • OnSite

    OnSite - 2014-10-27

    Hi Elfi Heck,

    Thanks, 'll work on it.

     

Log in to post a comment.