Watermark creation : position AND text alignment

Hello everyone,

I’m quite new with Aspose and probably don’t have all the tricks. And I’m kind of stuck for this one.

I want to add watermarks on an existing PDF and be able to define the position AND the text alignment.
Apparently, when the position is defined, the horizontal alignment is not taken into account.

I tried using WatermarkArtifact :

     WatermarkArtifact waWatermark = new WatermarkArtifact();        
     waWatermark.setText(ftText);        
     waWatermark.setArtifactHorizontalAlignment(hAlign);
     waWatermark.setRotation(iRotation);
     waWatermark.setOpacity(iOpacity);
     waWatermark.setPosition(new Point(leftPos, bottomPos));
     pPage.getArtifacts().add(waWatermark);

and using TextStamp :

TextStamp tStamp = new TextStamp(ftText);
tStamp.setHorizontalAlignment(hAlign);
tStamp.setRotateAngle(iRotation);
tStamp.setOpacity(iOpacity);
tStamp.setXIndent(leftPos);
tStamp.setYIndent(bottomPos);
pPage.addStamp(tStamp);

My goal is to be able to, for example, add watermarks somewhere at the bottom right of the page with right text alignment. Or in diagonal in the middle of the page with center text alignment.

Any idea how I could achieve this ?

Thanks

Jérôme

@erculije

To add watermarks to an existing PDF with specific positioning and text alignment using Aspose.PDF for Java, you can utilize the TextStamp class, which allows you to set both the position and alignment of the text.

Here’s how you can achieve this:

  1. Create a TextStamp: Initialize a TextStamp object with the desired text for the watermark.
  2. Set Position: Use the setXIndent and setYIndent methods to specify the position of the watermark on the page.
  3. Set Alignment: Use the setHorizontalAlignment and setVerticalAlignment methods to define how the text should be aligned within the stamp.

Here’s a code example demonstrating how to add a watermark at the bottom right of the page with right text alignment:

import com.aspose.pdf.Document;
import com.aspose.pdf.TextStamp;
import com.aspose.pdf.HorizontalAlignment;
import com.aspose.pdf.VerticalAlignment;

public class AddWatermark {
    public static void main(String[] args) {
        // Load the PDF document
        Document pdfDocument = new Document("input.pdf");

        // Create a TextStamp object
        TextStamp textStamp = new TextStamp("Sample Watermark");

        // Set the position of the watermark
        textStamp.setXIndent(450); // Adjust X position as needed
        textStamp.setYIndent(20);  // Adjust Y position as needed

        // Set the alignment of the text
        textStamp.setHorizontalAlignment(HorizontalAlignment.Right);
        textStamp.setVerticalAlignment(VerticalAlignment.Bottom);

        // Add the stamp to each page
        for (int i = 1; i <= pdfDocument.getPages().size(); i++) {
            pdfDocument.getPages().get_Item(i).addStamp(textStamp);
        }

        // Save the updated document
        pdfDocument.save("output.pdf");
    }
}

In this example:

  • The watermark is positioned at the bottom right of the page.
  • The text is aligned to the right and bottom, ensuring it appears correctly without overlapping other content.

You can adjust the X and Y indent values to position the watermark as needed. Additionally, you can explore other alignment options to achieve different placements, such as centering the watermark diagonally in the middle of the page.

For more detailed information on text stamp alignment and positioning, you can refer to the Aspose documentation on defining alignment for text stamp objects and adding watermarks.

If you have any further questions or need additional examples, feel free to ask!

Hi,

Well, I tried this code but after several tests I can conclude that the positioning setXIndent and setYIndent is ignored.
Whatever the values I put and even when I comment these 2 lines, the stamp is always put at the same position.

I can indeed us it to put at the bottom left, center or right of the page, but how to proceed to put it in the middle of the page, or at the top and at 2 thirds of the page’s width, with text aligned to the right ?

Thanks

Jérôme

@erculije

When you specify horizontal and vertical alignment of the stamp, its X and Y indent values are not being honored. Please try to set only X and Y Indent values and if you are still facing the issue, please share an expected output PDF for our reference so that we can further proceed to assist you accordingly.

Hello,

I understand that X and Y indent are ignored when horizontal alignment is used, and that’s my problem…
Check attached image “ex1.jpg” for what I’d like to achieve : it’s for the text (“For information only. Document generated automatically”) at the bottom right which, as you can see, is aligned to the right, the position defined is for the bottom right point of the text.
Another example with “ex2.jpg” where the text in red in the middle is aligned “center” and position in the middle of the page.

Thanks

Jérôme
ex2.jpg (80.3 KB)

ex1.jpg (163.1 KB)

@erculije

Please check below code sample that creates identical output to your images:

import com.aspose.pdf.*;
import com.aspose.pdf.facades.FormattedText;

public class AsposePDFExample {
    public static void main(String[] args) {
        // Create a new document
        Document doc = new Document();
        Page page = doc.getPages().add();

        // Create formatted text for the bottom right stamp
        FormattedText bottomrightstamptext = new FormattedText("For information only.");
        bottomrightstamptext.addNewLineText("Document generated automatically");

        // Create formatted text for the middle center stamp
        FormattedText middlecenterstamptext = new FormattedText("The document is not used by Asco, and is no longer kept up to date.");
        middlecenterstamptext.addNewLineText("If you need latest revision, contact config@asco.be");

        // Create a TextStamp object for the bottom right
        TextStamp bottomright = new TextStamp(bottomrightstamptext);
        bottomright.setHorizontalAlignment(HorizontalAlignment.Right);
        bottomright.setVerticalAlignment(VerticalAlignment.Bottom);

        // Create a TextStamp object for the middle center
        TextStamp middlecenter = new TextStamp(middlecenterstamptext);
        middlecenter.setHorizontalAlignment(HorizontalAlignment.Center);
        middlecenter.setVerticalAlignment(VerticalAlignment.Center);
        middlecenter.setRotateAngle(45);

        // Set font style for the bottom right stamp
        bottomright.getTextState().setFontStyle(FontStyles.Bold | FontStyles.Italic);

        // Add the stamps to the page
        page.addStamp(bottomright);
        page.addStamp(middlecenter);

        // Save the document
        doc.save(dataDir + "stamps.pdf");
    }
}

stamps.pdf (3.5 KB)

Thank you, @asad.ali, for your time on this.
Now I understand how the HorizontalAlignment and VeritcalAlignment work for the stamps ; it’s not how I was imagining before.
I’ll think about the best solution to use this.

Best regards

Jérôme

@erculije

Thanks for your kind feedback. Please keep using our API and feel free to let us know in case you need any kind of assistance.

1 Like