In following code below i am reading a file , inserting a watermark in it and save it , but watermark not creates in output pdf file. i am using a licenced Aspose.pdf 12.0.0.0 and c# language for .net
// Set the license for Aspose.PDF
Aspose.Pdf.License license_pdf = new Aspose.Pdf.License();
license_pdf.SetLicense(“Aspose.Total.lic”); // Make sure the license file is in the correct location
string pdfFilePath = @"D:/Tasks/1_OBT 2.0/TKTs/CR_807_1655/sample.pdf"; // Path to your input PDF file
string pdfoutputPath = @"D:/Tasks/1_OBT 2.0/TKTs/CR_807_1655/sample_output.pdf"; // Path to save the output PDF
// Read the PDF file into a MemoryStream
MemoryStream inputPdfStream = new MemoryStream(File.ReadAllBytes(pdfFilePath));
// Load the PDF from MemoryStream
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inputPdfStream);
// Create the watermark (TextStamp)
TextStamp textStamp = new TextStamp("Confidential and Proprietary")
{
Background = true, // Ensure the watermark is placed in the background
Opacity = 0.7, // Set the opacity level of the watermark (0 = fully transparent, 1 = opaque)
HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center, // Horizontal alignment of watermark
VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center, // Vertical alignment of watermark
//Rotate = Rotation.On90 // Optionally, rotate the watermark (e.g., 90 degrees)
};
// Add the watermark to each page in the document
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
page.AddStamp(textStamp);
}
// Save the modified PDF to a new MemoryStream
MemoryStream outputStream = new MemoryStream();
pdfDocument.Save(outputStream);
// Reset the position of the output stream for future use
outputStream.Position = 0;
// Write the bytes from the MemoryStream to the specified output file
File.WriteAllBytes(pdfoutputPath, outputStream.ToArray());
Response.Write("Watermark added successfully! The file is saved at: " + pdfoutputPath);