Free Support Forum - aspose.com

Add multi-line watermark on header or top-left in PDF using C# with Aspose.PDF for .NET

I had added a same question in Aspose.Words to add watermark on .docx file but I need that code of watermark in Aspose.PDF too. Actually, I need to add multiple watermark on top-left and in centre too. On each request user will add one watermark to pdf file. If position is top-left then It should not overlap watermark label, every request will add new watermark on below last added watermark.

for more you can follow this

Thanks
Satyendra Kumar

@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.

I am able to see watermark on first page only. If it’s watermark it should appear on every page.I am attaching sample file.

PDFConversion.zip (1.8 MB)

Thanks

I have modified changes and it’s working fine on all pages.

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(filePath);

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                Aspose.Pdf.Page page = doc.Pages[i + 1];
                Aspose.Pdf.Facades.PdfContentEditor pdfContentEditor = new Aspose.Pdf.Facades.PdfContentEditor();
                pdfContentEditor.BindPdf(doc);

                int count = pdfContentEditor.GetStamps(page.Number).Count();

                if (count == 0)
                {
                    Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText("Watermark.1:One line");
                    formattedText.AddNewLineText("Watermark.1:Second Line");
                    Aspose.Pdf.TextStamp topleftWatermark = new Aspose.Pdf.TextStamp(formattedText);
                    topleftWatermark.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
                    topleftWatermark.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                    topleftWatermark.Opacity = 0.5;
                    page.AddStamp(topleftWatermark);
                }
                else
                {
                    Aspose.Pdf.Facades.StampInfo stamp = pdfContentEditor.GetStamps(page.Number)[count - 1];
                    Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText("Watermark.2:One line");
                    formattedText.AddNewLineText("Watermark.2:Second Line");
                    formattedText.AddNewLineText("Watermark.2:Second Line");
                    Aspose.Pdf.TextStamp topleftWatermark = new Aspose.Pdf.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 = Aspose.Pdf.HorizontalAlignment.Left;
                    topleftWatermark.Opacity = 0.5;
                    page.AddStamp(topleftWatermark);
                }
            }

            //save output document
            outputFilePath = outputPath + "watermark_out_" + DateTime.Now.Ticks + ".pdf";
            doc.Save(outputFilePath);

But now, it’s watermark label is overlapping on each request. Please check attached sampleSample.zip (187.3 KB)

In attached document watermark_out_636613844693924538.pdf is first output and watermark_out_636613852993873275.pdf is second output

@itsathere

Thanks for getting back to us.

We apologize for the confusion due to earlier shared code snippet. i.e.:

 // As there are two extra lines in water mark. 10 + 10 = 20
topleftWatermark.YIndent += 20;

In above line of code the value of YIndent should be subtracted instead of increment as follows:

// As there are two extra lines in water mark. 10 + 10 = 20
topleftWatermark.YIndent -= 20; 

We have also updated the code snippet shared by us earlier. You may please change the line of code accordingly, in order to get correct output. In case of any further assistance, please feel free to contact us.