Watermark is not inserting in output PDF file using Aspose.pdf 12.0.0.0 and c#

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);

@lookatabhishek

It seems that you are having trouble inserting a watermark into your PDF file using Aspose.PDF version 12.0.0.0 in C#. Based on your provided code, there are a few things to check and consider to ensure that the watermark is correctly added to the output PDF.

  1. License Setup: Ensure that the license file is correctly set up and accessible. If the license is not valid or not found, the library may not function as expected. You can verify this by checking if other features of Aspose.PDF are working correctly.

  2. Watermark Properties: You have set the Background property of the TextStamp to true, which is correct for placing the watermark behind the content. However, ensure that the Opacity is set appropriately. You have it set to 0.7, which should be visible, but you can try adjusting it to see if it makes a difference.

  3. Saving the Document: You are saving the modified PDF to a MemoryStream and then writing it to a file. This approach is generally correct, but ensure that the output path is valid and that you have write permissions to that location.

  4. Check Output PDF: After running your code, open the output PDF file in a PDF viewer that supports displaying watermarks. Some viewers may not show watermarks correctly, so testing with different viewers can help.

  5. Alternative Method: If you continue to face issues, consider using a different approach to add the watermark. For example, you can add the watermark to each page without using a loop by utilizing the AddStamp method directly on the document object, if supported in your version.

Here’s a simplified example of how you might add a watermark:

// Create the watermark (TextStamp)
TextStamp textStamp = new TextStamp("Confidential and Proprietary")
{
    Background = true,
    Opacity = 0.7,
    HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center,
    VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center,
};

// Add the watermark to each page in the document
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
    page.AddStamp(textStamp);
}

// Save the modified PDF
pdfDocument.Save(pdfoutputPath);

If the problem persists, consider checking the Aspose forums for similar issues or updates related to your version of the library. You may also want to look into upgrading to a newer version of Aspose.PDF, as there may have been bug fixes or improvements related to watermarking in later releases.

If you need further assistance, feel free to ask!