Add watermark text

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Merger for Java (v5)  /  Add watermark text

DynamicPDF Merger for Java (v5) Forum

 Apr 01 2011 11:14 AM
We are working on requirement to add watermark text to an existing PDF before rendering it to the user in a web browser.

Can you point us to the API / sample documentation to achieve this ?

Thanks
 Apr 01 2011 1:24 PM
Posted by a ceTe Software moderator
Hello ,

Yes, you can add the images or text to the PDF file as a background with some transparency using our product without any problem.

You can use the BackgroundImage page element if you want to add any image as background. You can refer to the help documentation at: http://www.DynamicPDF.com/Support/Java_Help_Documentation_11_01/api-ref/com/cete/dynamicpdf/pageelements/BackgroundImage.html.


You will have to use the TransparencyGroup class in order to give some transparency for the watermark text you are adding. You will have to set the Transparency value in the constructor(0.0 to 1.0) depending on your requirement. Please refer the documentation on TransparencyGroup at: http://www.DynamicPDF.com/Support/Java_Help_Documentation_11_01/api-ref/com/cete/dynamicpdf/pageelements/TransparencyGroup.html.


You can also use the Template element which adds the elements to all the pages of the document. You can add any page element to the template and then add the template to the document. You can use the StampTemplate if you want to add the template in the foreground. You can refer to the help documentation at: http://www.DynamicPDF.com/Support/Java_Help_Documentation_11_01/user-manual/Templates.html

Thanks,
ceTe Software Support Team.
 Apr 01 2011 4:44 PM
Thanks for your reply.

Per our requirement, we need to add a textual watermark as opposed to an image watermark (as highlighted in your reply).

Can you direct us to the associated API / sample code ?

Thanks
 Apr 01 2011 4:51 PM
Posted by a ceTe Software moderator
Hello,

For textual watermark, just use a Label page element instead of a BackgroundImage page element. Here is an example that uses a label inside a TransparencyGroup page element:

http://www.DynamicPDF.com/Support/Java_Help_Documentation_11_01/api-examples/TransparencyGroup.html

Thanks,
ceTe Software Support Team.
 May 09 2011 4:30 PM
Hello

We tried to use referenced example to model our application needs -(add watermark text to an existing PDF)

Here is code snippet -

MergeDocument        doc                =        new MergeDocument( new PdfDocument(data) );
java.util.Iterator itr        =        doc.getPages().getIterator();

Page page;
while(itr.hasNext())
{
        page        =        (Page)itr.next();
        Label label        =        
                new Label("Test for Watermark",
                        (page.getDimensions().getWidth()/2),
                        (page.getDimensions().getHeight()/2),
                        200,
                        30,
                        Font.getHelveticaBold(),
                        30,
                        TextAlign.CENTER,
                        new RgbColor(224,224,224));

        label.setAngle(-30);
        page.getElements().add( label );
}


The issue with this code is that the generated PDF has the Label (watermark) text overlayed on top of original PDF content instead of being under the original PDF content.

Is there a way to create a true watermark on the PDF document similar to the one produced by Microsoft Word ?

Thanks
 May 10 2011 9:16 AM
Posted by a ceTe Software moderator
Hello,

Any new content added to an existing page will be added over the existing content, since you are adding the label directly to the page it will be added on top of the existing content. Please use a Template and add the label to the template and apply the template to the document, this will add the watermark text underneath the existing content giving the effect of true watermark. Once the document template is set it will add the label to all the pages in the document, so you do not need to loop through the pages.

    MergeDocument document =  new MergeDocument( new PdfDocument(data) );
    Template template = new Template();
    template.getElements().add(new Label(“Watermark Text”, 0, 0, 200, 12));
    document.setTemplate(template);
    document.draw(“path”);

Thanks,
ceTe Software Support Team.
 Sep 28 2015 10:28 AM
Hi,

I am using template class to add watermark Text/Image but its overlapping to the content.I want the Watermark to be set in the background and in the center of the document

please suggest in this
 Sep 28 2015 12:56 PM
Posted by a ceTe Software moderator
Hello,

Yes, you can add image in background using BackgroundImage page element. You can refer to the documentation on BackgroundImage class here. Also you can specify the vertical and horizontal alignment for it using AnchorGroup. Below is the code sample for it.

            Document document = new Document();
            Page page = new Page();
            document.getPages().add(page);
            String logoImagePath = "Path of image file";
            Image image = new Image(logoImagePath, 50, 50);
            float width=page.getDimensions().getBody().getWidth();
            float height=page.getDimensions().getBody().getHeight();
            AnchorGroup anchorGroupObj = new AnchorGroup(width, height,Align.CENTER, VAlign.CENTER);
            BackgroundImage backGrundImageObj = new BackgroundImage(logoImagePath);
            anchorGroupObj.add(backGrundImageObj);
            Template watermarkTemplate = new Template();
            watermarkTemplate.getElements().add(anchorGroupObj);
            document.setTemplate(watermarkTemplate);
            document.draw("C:\\Temp\\MyDocument_Java.pdf");

Thanks,
ceTe Software Support Team.
 Dec 30 2015 5:59 AM
mm

All times are US Eastern Standard time. The time now is 5:11 PM.