0

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

EDIT:

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);
    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.

ppaweluz
  • 1
  • 1
  • @KJ IMHO I do not agree, there is always a way to add a watermark. I am able to do it for a word document, and I found a way to do it in pdf but not in a way I desire, so there must be a way to make it works as expected. It must be not selectable IMHO. Thank you for your input – ppaweluz Jul 30 '21 at 15:15
  • 1
    I would propose adding the watermark using a pattern. Most current pdf viewers don't support selecting text drawn via patterns. – mkl Jul 30 '21 at 15:36
  • @mkl what do you mean by "adding a watermark using a pattern" ? Can you please point me to some code snippet, any API, or something. I think I have seen almost all Aspose PDF watermark related pages, and I have not seen anyone mentioning term "pattern" – ppaweluz Aug 02 '21 at 05:57
  • Unfortunately i don't know the Aspose API. For Itext 5 it is explained [here](https://stackoverflow.com/a/20361261/1729265) and for Itext 7 [here](https://stackoverflow.com/a/60051511/1729265). Maybe you can find something similar for Aspose. – mkl Aug 02 '21 at 07:31

0 Answers0