How to remove existing watermark from output pdf document

@asad.ali I am working with @sarathisathish90 i can able to get the watermark text on all the pdf pages with the below code snippet. How can we remove applied watermark text for all pages on the output pdf document ?

Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Temp\in.docx");

using (MemoryStream watermarkImageStream = new MemoryStream())
{
    foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
    {
        if (s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
        {
            // Render the watermark to graphics, since ShapeRenderer.Save sets background of the rendered image to white
            // and we need a transparent background.
            ShapeRenderer renderer = s.GetShapeRenderer();
            Size imageSize = renderer.GetSizeInPixels(1, 96);
            using (SkiaSharp.SKBitmap bmp = new SkiaSharp.SKBitmap(imageSize.Width, imageSize.Height))
            using (SkiaSharp.SKCanvas g = new SkiaSharp.SKCanvas(bmp))
            {
                renderer.RenderToSize(g, 0, 0, imageSize.Width, imageSize.Height);
                bmp.Encode(watermarkImageStream, SkiaSharp.SKEncodedImageFormat.Png, 100);
            }

            break;
        }
    }
    watermarkImageStream.Position = 0;

    doc.Watermark.Remove();
    doc.Save(@"C:\Temp\out.pdf");

    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"C:\Temp\out.pdf");

    // Add watermark on all pages
    foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
    {
        Aspose.Pdf.ImageStamp stamp = new Aspose.Pdf.ImageStamp(watermarkImageStream);
        stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        double scale = Math.Min(page.PageInfo.Width / stamp.Width, page.PageInfo.Height / stamp.Height);
        if (scale < 1)
        {
            stamp.Width *= scale;
            stamp.Height *= scale;
        }

        page.AddStamp(stamp);
    }

    // Save updated document
    pdfDocument.Save(@"C:\\Temp\\out_pdf.pdf");
}

out_pdf.pdf (542.0 KB)

I am attaching the output pdf document can you suggest me how to remove existing watermark text from this pdf document ?

Hi @asad.ali
i can able to get the watermark text on all the pdf pages with the below code snippet. How can we remove applied watermark text for all pages on the output pdf document ?

Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Temp\in.docx");

using (MemoryStream watermarkImageStream = new MemoryStream())
{
    foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
    {
        if (s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
        {
            // Render the watermark to graphics, since ShapeRenderer.Save sets background of the rendered image to white
            // and we need a transparent background.
            ShapeRenderer renderer = s.GetShapeRenderer();
            Size imageSize = renderer.GetSizeInPixels(1, 96);
            using (SkiaSharp.SKBitmap bmp = new SkiaSharp.SKBitmap(imageSize.Width, imageSize.Height))
            using (SkiaSharp.SKCanvas g = new SkiaSharp.SKCanvas(bmp))
            {
                renderer.RenderToSize(g, 0, 0, imageSize.Width, imageSize.Height);
                bmp.Encode(watermarkImageStream, SkiaSharp.SKEncodedImageFormat.Png, 100);
            }

            break;
        }
    }
    watermarkImageStream.Position = 0;

    doc.Watermark.Remove();
    doc.Save(@"C:\Temp\out.pdf");

    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"C:\Temp\out.pdf");

    // Add watermark on all pages
    foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
    {
        Aspose.Pdf.ImageStamp stamp = new Aspose.Pdf.ImageStamp(watermarkImageStream);
        stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
        double scale = Math.Min(page.PageInfo.Width / stamp.Width, page.PageInfo.Height / stamp.Height);
        if (scale < 1)
        {
            stamp.Width *= scale;
            stamp.Height *= scale;
        }

        page.AddStamp(stamp);
    }

    // Save updated document
    pdfDocument.Save(@"C:\\Temp\\out_pdf.pdf");
}

out_pdf.pdf (542.0 KB)
I am attaching the output pdf document can you suggest me how to remove existing watermark text from this pdf document ?

@Sandhya_Rani

If you want to remove the watermark from the PDF using Aspose.PDF, please note that it depends upon how watermark was added inside PDF. A watermark in the PDF can be present in form of stamp, artifact, image object as well as drawing. Therefore, different methods can be adopted to remove PDF watermarks. For example, below code snippet removes Stamps and Artifacts. You can also specify the Artifact type to remove particular type:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "out_pdf.pdf");

Facades.PdfContentEditor editor3 = new Facades.PdfContentEditor(doc);

// Removing all existing header and footer
foreach (var page in doc.Pages)
{
    var result = editor3.GetStamps(page.Number);

    foreach (var stampInfo in result)
    {
        editor3.DeleteStampById(page.Number, stampInfo.StampId);
        //if (stampInfo.StampId == 1)
        //{
        //    editor3.DeleteStampById(page.Number, stampInfo.StampId);
        //}
        //if (stampInfo.StampId == 2)
        //{
        //    editor3.DeleteStampById(page.Number, stampInfo.StampId);
        //}
    }

    if (page.Artifacts[1].Subtype == Aspose.Pdf.Artifact.ArtifactSubtype.Watermark)
        doc.Pages[1].Artifacts.Delete(doc.Pages[1].Artifacts[1]);

    foreach(var artifact in page.Artifacts)
    {
        if(artifact.Subtype == Artifact.ArtifactSubtype.Watermark)
        {

        }
        page.Artifacts.Delete(artifact);
    }
}

//save result pdf to file
doc.Save(dataDir + "remove_watermark.pdf", Aspose.Pdf.SaveFormat.Pdf);

remove_watermark.pdf (540.7 KB)

If you use below line of code, you will be able to remove image object added as watermark:

page.Resources.Images.Clear();

remove_image.pdf (200.4 KB)

We would recommend that you remove any kind of watermark in the DOCX using Aspose.Words before generating the output PDF. In case you need further assistance, please let us know.