@itsathere
Thanks for contacting support.
You may add more than one watermark (i.e on top-left and center position of the PDF Page) by using TextStamp
Class in Aspose.PDF for .NET. Please check following code snippet, in order to add two watermarks in PDF file at different locations:
Document doc = new Document();
Page page = doc.Pages.Add();
Facades.FormattedText formattedText = new Facades.FormattedText("Watermark.1:One line");
formattedText.AddNewLineText("Watermark.1:Second Line");
TextStamp topleftWatermark = new TextStamp(formattedText);
topleftWatermark.VerticalAlignment = VerticalAlignment.Top;
topleftWatermark.HorizontalAlignment = HorizontalAlignment.Left;
topleftWatermark.Opacity = 0.5;
formattedText = new Facades.FormattedText("Watermark.2:One line");
formattedText.AddNewLineText("Watermark.2:Second Line");
TextStamp centerWatermark = new TextStamp(formattedText);
centerWatermark.VerticalAlignment = VerticalAlignment.Center;
centerWatermark.HorizontalAlignment = HorizontalAlignment.Center;
centerWatermark.Opacity = 0.5;
page.AddStamp(topleftWatermark);
page.AddStamp(centerWatermark);
doc.Save(dataDir + "MultipleWatermarks.pdf");
You can set YIndent
and XIndent
properties of TextStamp
, in order to position them over PDF page as per your requirement. For an instance, following code snippet will add watermark to top-left position in each iteration and below the existing watermark:
Document doc = new Document(dataDir + "MultipleWatermarks.pdf");
Page page = doc.Pages[1];
Facades.PdfContentEditor pdfContentEditor = new Facades.PdfContentEditor();
pdfContentEditor.BindPdf(doc);
int count = pdfContentEditor.GetStamps(page.Number).Count();
if (count == 0)
{
Facades.FormattedText formattedText = new Facades.FormattedText("Watermark.1:One line");
formattedText.AddNewLineText("Watermark.1:Second Line");
TextStamp topleftWatermark = new TextStamp(formattedText);
topleftWatermark.VerticalAlignment = VerticalAlignment.Top;
topleftWatermark.HorizontalAlignment = HorizontalAlignment.Left;
topleftWatermark.Opacity = 0.5;
page.AddStamp(topleftWatermark);
}
else
{
Facades.StampInfo stamp = pdfContentEditor.GetStamps(page.Number)[count - 1];
Facades.FormattedText formattedText = new Facades.FormattedText("Watermark.2:One line");
formattedText.AddNewLineText("Watermark.2:Second Line");
formattedText.AddNewLineText("Watermark.2:Second Line");
TextStamp topleftWatermark = new TextStamp(formattedText);
topleftWatermark.YIndent = stamp.Rectangle.URY - stamp.Rectangle.Height - formattedText.TextHeight;
// As there are two extra lines in water mark. 10 + 10 = 20
topleftWatermark.YIndent -= 20;
// We do not need to change it
topleftWatermark.HorizontalAlignment = HorizontalAlignment.Left;
topleftWatermark.Opacity = 0.5;
page.AddStamp(topleftWatermark);
}
doc.Save(dataDir + "MultipleWatermarks.pdf");
Please use suggested approach in order achieve your requirements and in case you face any issue, please feel free to contact us.