Hi
We have purchased Aspose.Total license recently. We are trying to add image as watermark to all pages in pdf. But we want to add watermark to full page covering all areas of page. We are using watermarkartificat class to add image as watermark . But it is not covering entire code.
Can you please help us?
Thanks
Krushna
To add an image as a full-page watermark in a PDF using Aspose.PDF, you can utilize the WatermarkArtifact
class effectively. However, to ensure that the watermark covers the entire page, you need to set the appropriate properties for the watermark, such as its position and size.
Here’s a sample code snippet demonstrating how to achieve this:
using Aspose.Pdf;
using Aspose.Pdf.Facades;
public void AddFullPageWatermark(string pdfPath, string watermarkImagePath)
{
// Load the PDF document
Document pdfDocument = new Document(pdfPath);
// Create a watermark artifact
ImageStamp watermark = new ImageStamp(watermarkImagePath);
// Set the properties of the watermark
watermark.Background = true; // Set to true to place it behind the content
watermark.Opacity = 0.5; // Set the opacity of the watermark
watermark.Rotate = 0; // Set rotation if needed
// Set the position and size to cover the entire page
foreach (Page page in pdfDocument.Pages)
{
watermark.SetOrigin(0, 0); // Set the origin to the bottom-left corner
watermark.Width = page.Rect.Width; // Set width to page width
watermark.Height = page.Rect.Height; // Set height to page height
page.AddStamp(watermark); // Add the watermark to the page
}
// Save the updated PDF document
pdfDocument.Save("WatermarkedDocument.pdf");
}
In this code:
- The
ImageStamp
class is used to create a watermark from an image. - The
SetOrigin
method positions the watermark at the bottom-left corner of the page. - The width and height of the watermark are set to match the dimensions of the page, ensuring it covers the entire area.
Make sure to adjust the Opacity
and Rotate
properties as needed to fit your design requirements. This approach will effectively add a full-page watermark to each page of your PDF document.
If you need further assistance or have specific requirements, feel free to ask!
no such properties exists such as setorigin, width and height to imagestamp
Is it possible if you could please provide your sample PDF and watermark image along with some expected output or screenshot? We will try to produce it in our environment and share the code snippet with you.
Hi Asad,
test.pdf (1.0 MB)
watermarked_test.pdf (3.2 MB)
I am attaching sample file before and after watermark image added. You will observe watermark is not covering entire page
Thanks
Krushna
Please check below code sample along with the attached output generated by it and let us know if it fulfils your requirements:
Document doc = new Document(dataDir + "test.pdf");
ImageStamp stamp = new ImageStamp(dataDir + "watermark.png");
stamp.HorizontalAlignment = HorizontalAlignment.Center;
stamp.VerticalAlignment = VerticalAlignment.Center;
stamp.Background = true;
stamp.Width = doc.Pages[1].GetPageRect(true).Width;
stamp.Height = doc.Pages[1].GetPageRect(true).Height;
doc.Pages[1].AddStamp(stamp);
doc.Save(dataDir + "output.pdf");
output.pdf (1.3 MB)