Free Support Forum - aspose.com

Add visible watermark to print only

Hello,


I need to add a watermark that will be visible when printed but will be hidden on the screen.

Today i use the iText library to create this watermark but i don’t found it in class Layer of Aspose.

I didn’t find any help on Operator, OperatorSelector and ShowText classes used by the class Layer.

By advance, thank you for your help.

Patrick

Hi Patrick,


Thanks for contacting support.

In order to accomplish your requirements, please try using following code snippet.

[Java]

com.aspose.pdf.Document doc = new Document();<o:p></o:p>

doc.getPages().add();

FreeTextAnnotation annotation = new FreeTextAnnotation(doc.getPages().get_Item(1), new com.aspose.pdf.Rectangle(50, 600, 250, 650), new com.aspose.pdf.DefaultAppearance("Helvetica", 16, java.awt.Color.RED));

annotation.setContents("ABCDEFG");

annotation.getCharacteristics().setBorder(java.awt.Color.RED);

annotation.setFlags(AnnotationFlags.Print | AnnotationFlags.NoView);

doc.getPages().get_Item(1).getAnnotations().add(annotation);

doc.save("c:/Invisible_Annotation.pdf");

Hi,

The solution proposed above does not work in my environment. See attached file.

Moreover, it does not fit my need.

Below you will find the code to generate the expected watermark when printing

public static void addWatermark(InputStream inputStream, OutputStream outputStream) throws IOException{
//TODO : permettre de cacher le watermark à l'écran
//TODO : non trouvé dans Aspose
//Texte
FormattedText formattedText = new FormattedText(watermark.getTexte(),
new FontColor(255, 0, 0),
FontStyle.TimesRoman, EncodingType.Winansi, false, 12);
//Stamp
TextStamp watermarkStamp = new TextStamp(formattedText);
watermarkStamp.setHorizontalAlignment(HorizontalAlignment.Center);
watermarkStamp.setVerticalAlignment(VerticalAlignment.Center);

//Page par page suivant orientation
Document pdf = new Document(inputStream);
PageCollection pages = pdf.getPages();
for(int p =1; p<=pages.size(); p++){
Page page = pages.get_Item(p);
Rectangle pageInfo = page.getRect();
double h = pageInfo.getHeight();
double w = pageInfo.getWidth();
double angle = 0;
if(page.getRotate()==0){
angle = Math.atan(h/w);
}else{
angle = Math.atan(w/h);
}
//Slash
watermarkStamp.setRotateAngle(Math.toDegrees(angle));
page.addStamp(watermarkStamp);
}
pdf.save(outputStream);
outputStream.close();
inputStream.close();
}

By advance, thank you for your help,

Patrick

Hi Patrick,


I have noticed that you are using FormattedText from legacy com.aspose.pdf.facades package and and using TextStamp object to place text as stamp inside PDF document. However please note that TextStamp class does not provide a support for setting flags so you may not be able to make Text invisible when viewing it and visible in a printout.

You can also use FreeTextAnnotation object and specify its HorizontalAlignment and VerticalAlignment alignment information.

[Java]

//Page par page suivant
orientation
<o:p></o:p>

Document pdf = new Document();

pdf.getPages().add();

FreeTextAnnotation annotation = new FreeTextAnnotation(pdf.getPages().get_Item(1), new com.aspose.pdf.Rectangle(50, 600, 250, 650), new com.aspose.pdf.DefaultAppearance("Helvetica", 16, java.awt.Color.RED));

annotation.setContents("ABCDEFG");

annotation.getCharacteristics().setBorder(java.awt.Color.RED);

annotation.setFlags(AnnotationFlags.Print | AnnotationFlags.NoView);

annotation.setHorizontalAlignment(HorizontalAlignment.Center);

annotation.setVerticalAlignment(VerticalAlignment.Center);

pdf.getPages().get_Item(1).getAnnotations().add(annotation);

pdf.save("c:/pdftest/WaterMarkVisible.pdf");