We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Pdf watermark problem

hey i am using the following code for watermark of pdf


/************************************************************************
*
* Function Name : setWatermarkPdf()
* Description : This function changes the Watermark of the pdf document
* according to the value of the Watermark of PrintParams Object
*
*/
static com.aspose.pdf.Document setWatermarkPdf(com.aspose.pdf.Document pdf1, String watermark)
{
if (!watermark.equals(""))
{
com.aspose.pdf.TextStamp textStamp = new com.aspose.pdf.TextStamp(watermark);
// Giving the look of a watermark
textStamp = WatermarkShapeSet(textStamp);

// Adding the textStamp on all the pages of the pdf document
for (int Page_counter = 1 ; Page_counter <= pdf1.getPages().size() ; Page_counter++)
{
pdf1.getPages().get_Item(Page_counter).addStamp(textStamp);
}

}
return pdf1;
}

/

*
* Function Name : WatermarkShapeSet()
Description : It changes the watermark related shaped accordingly to give
* it the required look of a watermark
*
*************************************************************************/
private static TextStamp WatermarkShapeSet(TextStamp textStamp)
{
// PdfWatermarkValues object is required to access needed constant values for constructing watermark
PdfWatermarkValues PWV = new PdfWatermarkValues();

textStamp.setBackground(true);
textStamp.setXIndent(PWV.textStampXIndent);
textStamp.setYIndent(PWV.textStampYIndent );
textStamp.setRotate(com.aspose.pdf.Rotation.on90);
new com.aspose.pdf.FontRepository();
textStamp.getTextState().setFont(FontRepository.findFont(PWV.textStampFont));
textStamp.getTextState().setFontSize(PWV.textStampFontSize);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getGray());

return textStamp;
}



and its giving the results attached… the original pdf and after watermark pdf with watermark text as “Hello Dude” is attached.
Please can you help to make it look better like a proper watermark, transparent grey in color placed in the middle.
Thanks

hey i am using the following code for watermark of pdf


/************************************************************************
*
* Function Name : setWatermarkPdf()
* Description : This function changes the Watermark of the pdf document
* according to the value of the Watermark of PrintParams Object
*
*/
static com.aspose.pdf.Document setWatermarkPdf(com.aspose.pdf.Document pdf1, String watermark)
{
if (!watermark.equals(""))
{
com.aspose.pdf.TextStamp textStamp = new com.aspose.pdf.TextStamp(watermark);
// Giving the look of a watermark
textStamp = WatermarkShapeSet(textStamp);

// Adding the textStamp on all the pages of the pdf document
for (int Page_counter = 1 ; Page_counter <= pdf1.getPages().size() ; Page_counter++)
{
pdf1.getPages().get_Item(Page_counter).addStamp(textStamp);
}

}
return pdf1;
}

/

*
* Function Name : WatermarkShapeSet()
Description : It changes the watermark related shaped accordingly to give
* it the required look of a watermark
*
*************************************************************************/
private static TextStamp WatermarkShapeSet(TextStamp textStamp)
{
// PdfWatermarkValues object is required to access needed constant values for constructing watermark
PdfWatermarkValues PWV = new PdfWatermarkValues();

textStamp.setBackground(true);
textStamp.setXIndent(PWV.textStampXIndent);
textStamp.setYIndent(PWV.textStampYIndent );
textStamp.setRotate(com.aspose.pdf.Rotation.on90);
new com.aspose.pdf.FontRepository();
textStamp.getTextState().setFont(FontRepository.findFont(PWV.textStampFont));
textStamp.getTextState().setFontSize(PWV.textStampFontSize);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getGray());

return textStamp;
}



and its giving the results attached… the original pdf and after watermark pdf with watermark text as “Hello Dude” is attached.
Please can you help to make it look bette rlike a proper watermark, transparent grey in color placed in the middle.
Thanks

hey i am using the following code for watermark of pdf


/************************************************************************
*
* Function Name : setWatermarkPdf()
* Description : This function changes the Watermark of the pdf document
* according to the value of the Watermark of PrintParams Object
*
*/
static com.aspose.pdf.Document setWatermarkPdf(com.aspose.pdf.Document pdf1, String watermark)
{
if (!watermark.equals(""))
{
com.aspose.pdf.TextStamp textStamp = new com.aspose.pdf.TextStamp(watermark);
// Giving the look of a watermark
textStamp = WatermarkShapeSet(textStamp);

// Adding the textStamp on all the pages of the pdf document
for (int Page_counter = 1 ; Page_counter <= pdf1.getPages().size() ; Page_counter++)
{
pdf1.getPages().get_Item(Page_counter).addStamp(textStamp);
}

}
return pdf1;
}

/

*
* Function Name : WatermarkShapeSet()
Description : It changes the watermark related shaped accordingly to give
* it the required look of a watermark
*
*************************************************************************/
private static TextStamp WatermarkShapeSet(TextStamp textStamp)
{
// PdfWatermarkValues object is required to access needed constant values for constructing watermark
PdfWatermarkValues PWV = new PdfWatermarkValues();

textStamp.setBackground(true);
textStamp.setXIndent(PWV.textStampXIndent);
textStamp.setYIndent(PWV.textStampYIndent );
textStamp.setRotate(com.aspose.pdf.Rotation.on90);
new com.aspose.pdf.FontRepository();
textStamp.getTextState().setFont(FontRepository.findFont(PWV.textStampFont));
textStamp.getTextState().setFontSize(PWV.textStampFontSize);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getGray());

return textStamp;
}



and its giving the results attached… the original pdf and after watermark pdf with watermark text as “Hello Dude” is attached.
Please can you help to make it look better like a proper watermark, transparent grey in color placed in the middle.
Thanks

Hi Hafsa,


Thanks for your inquiry. We will appreciate it if you please share a sample PDF document with intended Watermark. So we will look into it and guide you accordingly.

Best Regards,

The real document is test.pdf
and the After Watermark.pdf is also attached
The watermark text ="Hello Dude"
You can put any…
Means it will depend on user
Just need it in the center, rotated and transparent grey shade. Thankyou

Hi Hafsa,


Thanks for sharing the resource files.

In order to center Align the Watermark object, please specify the Vertical and Horizontal alignment as Center and specify the RotateAngle for Stamp object as 45. Please take a look over following code snippet and for your reference, I have also attached the resultant file generated over my end.

[Java]

private static TextStamp
WatermarkShapeSet(TextStamp textStamp)<o:p></o:p>

{

// PdfWatermarkValues object is required to access needed constant values for constructing watermark

//PdfWatermarkValues PWV = new PdfWatermarkValues();

textStamp.setBackground(true);

textStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Center);

// textStamp.setXIndent(PWV.textStampXIndent);

// textStamp.setYIndent(PWV.textStampYIndent );

textStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

textStamp.setRotateAngle(45);

new com.aspose.pdf.FontRepository();

textStamp.getTextState().setFont(FontRepository.findFont("Arial"));

textStamp.getTextState().setFontSize(20);

textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);

textStamp.getTextState().setForegroundColor(com.aspose.pdf.Color.getGray());

return textStamp;

}

I have tried the code and its working, but the problem is that it is appearing before the images … Is it any way that it can be at front and transparent grey in color. please suggest.

Hafsa101:
I have tried the code and its working, but the problem is that it is appearing before the images ... Is it any way that it can be at front and transparent grey in color. please suggest.
Hi Hafsa,

Please try using textStamp.setBackground(false); to display TextStamp over the front of page contents. See attached output.