Add watermark/stamp to the PDF file - not visible but selectable

Hi,

I am facing problem with adding the watermark to the PDF file. My goal is to have not selectable, visible watermark in my pdf file.
I am using TextStamp to add watermark to my pdf file, but it is not visible. Funny thing is that I can select this text, copy it to notepad and there it is. I know that to fix that, I have to use setBacground(false), then I see my watermark, but I can still select it, and copy. I do not want to have ability to select watermark to the user. Then I use setDraw(true), and true - I can not longer select this watermark but it looks very ugly. Is there any fix for that?. I just want to have a watermark in the background that is not selectable. I have tried with TextStamp and with WatermarkArtifact and still nothing - to be honest I do not know what is the suggested solution for that, I have tried them both. Here is a snipped of my code :

  private byte[] addWatermarkToPdf(byte[] data) {
    com.aspose.pdf.Document pdfDocument = null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
      pdfDocument = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
      TextStamp textStamp = new TextStamp("Watermark text");
      textStamp.setBackground(true);
      textStamp.setWidth(558.35);
      textStamp.setHeight(93.05);
      textStamp.setRotateAngle(-315);
      textStamp.setOpacity(0.5);
      textStamp.getTextState().setFont(new FontRepository().openFont("C:\\myFont.ttf"));
      textStamp.getTextState().setFontSize(14.0F);
      textStamp.getTextState().setForegroundColor(Color.fromArgb(222, 222, 222));
      textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
      textStamp.setVerticalAlignment(VerticalAlignment.Center);
      for (int Page_counter = 1; Page_counter <= pdfDocument.getPages().size(); Page_counter++) {
        pdfDocument.getPages().get_Item(Page_counter).addStamp(textStamp);
      }
    } catch (Exception e) {
      log.error("Error while adding watermark to pdf", e);
      return output.toByteArray();
    }
    pdfDocument.save(output, SaveFormat.Pdf);
    return output.toByteArray();
  }

I am using Aspose.Total for Java v 17.10

cheers,
Pawel

I have also tried those approaches, and all of them do not work as I want:

Approach 2

  public byte[] addWaterMarkAnnotation3(byte[] data) {
    com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
    FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
    WatermarkArtifact artifact = new WatermarkArtifact();
    artifact.setText(formattedText);
    artifact.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
    artifact.setArtifactVerticalAlignment(VerticalAlignment.Center);
    artifact.setRotation(45);
    artifact.setOpacity(0.5);
    artifact.setBackground(true);
    doc.getPages().get_Item(1).getArtifacts().add(artifact);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    doc.save(out);
    return out.toByteArray();
  }

Approach 2

  public byte[] addWaterMarkAnnotation4(byte[] data) {
    com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
    TextStamp textStamp = new TextStamp("Watermark");
    textStamp.setXIndent(25);
    textStamp.setYIndent(400);
    textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
    textStamp.getTextState().setFontSize(72.0F);
    textStamp.getTextState().setFontStyle(FontStyles.Italic);
    textStamp.getTextState().setForegroundColor(Color.fromArgb(222, 222, 222));
    textStamp.setOpacity(50);
    textStamp.setStampId(123456);
    textStamp.setBackground(true);
    doc.getPages().get_Item(1).addStamp(textStamp);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    doc.save(out);
    return out.toByteArray();
  }

Approach 3

  public byte[] addWaterMarkAnnotation5(byte[] data) {
    PdfFileStamp fileStamp = new PdfFileStamp();
    fileStamp.bindPdf(new ByteArrayInputStream(data));
    FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
    fileStamp.setStartingNumber(1);
    final Stamp stamp = new Stamp();
    stamp.setRotation(100f);
    stamp.setBackground(true);
    stamp.setOpacity(0.5f);
    stamp.bindLogo(formattedText);
    fileStamp.addStamp(stamp);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    fileStamp.save(out);
    fileStamp.close();
    return out.toByteArray();
  }

Approach 4

public byte[] addWaterMarkAnnotation7(byte[] data) {
    PdfFileStamp fileStamp = new PdfFileStamp();
    com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
    fileStamp.bindPdf(doc);
    FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
    final Stamp stamp = new Stamp();
    stamp.bindLogo(formattedText);
    stamp.setOrigin(200,200);
    stamp.setRotation(100f);
    stamp.setBackground(true);
    stamp.setOpacity(0.5f);
    fileStamp.addStamp(stamp);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    fileStamp.save(out);
    fileStamp.close();
    return out.toByteArray();
  }

Only working solution that I fount, but I am not able to rotate it and make its size in the way I want to is this:

  public byte[] addWaterMarkAnnotation(byte[] data) {
  // it works in background!!!!!!
    com.aspose.pdf.Document document = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
    Page page = document.getPages().get_Item(1);
    final Rectangle rect = new Rectangle(558.35, 93.05, 400, 600);
    rect.rotate(3); // tried different values
    WatermarkAnnotation wa = new WatermarkAnnotation(page, rect);
    page.getAnnotations().add(wa);
    TextState ts = new TextState();
    ts.setFont(new FontRepository().openFont("W:\\myFont.ttf"));
    ts.setFontSize(14.0F);
    ts.setForegroundColor(Color.fromArgb(222, 222, 222));
    ts.setHorizontalAlignment(HorizontalAlignment.Center);
    wa.setOpacity(0.8);
    wa.setTextAndState(new String[] {"Watermark"}, ts);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    document.save(out);
    System.out.println(wa.getCharacteristics().getRotate()); // shows me value 0
    return out.toByteArray();
  }

Maybe someone can help me or figure out how to make it work in other way.

@paweluz

A ticket with ID PDFJAVA-40734 has been created in our issue tracking system to further investigate the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed. Moreover, please share the expected output PDF file for our investigations.

The issues you have found earlier (filed as PDFJAVA-40734) have been fixed in Aspose.PDF for Java 23.1.