Whether support add image watermark to existing pdf stream?

i want to put image watermark to existing pdf stream for pdf's every page, whether has method can support it? my programing language is dotnet, thanks

@Owen_Sun

Thank you for contacting support.

You may Add Image Stamp in PDF File and feel free to contact us if you need any further assistance.

now has one question is that if the pdf or ppt file has own background image, then image stamp can't show it in pdf file after i has put image stamp to pdf file, code as below:
                        byte[] byteArrDOC = ObjectToByteArray(outputforpdf);
                        Document pdfDocumentbyteArrDOC = new Document(new MemoryStream(byteArrDOC));
                        string docWaterMarkFileDir = Server.MapPath("~/Content/Images/watermark.jpg");
                        ImageStamp imageStamp = new ImageStamp(docWaterMarkFileDir);
                        imageStamp.Background = true;
                        //set different values
                        imageStamp.Height = 100;
                        imageStamp.Width = 100; //100 in the bottom
                        imageStamp.Opacity = 0.5;

                        //imageStamp.XIndent = 100;// in the bottom 
                        //imageStamp.YIndent = 100;// in the bottom

                        // in the top
                        imageStamp.TopMargin = 10;
                        imageStamp.VerticalAlignment = VerticalAlignment.Top;
                        imageStamp.HorizontalAlignment = HorizontalAlignment.Center;

                        foreach (Page pageItem in pdfDocumentbyteArrDOC.Pages)
                        {
                            // whole page add watermark for fill with the whole screen

                            //assuming margins as zero
                            //pageItem.PageInfo.Margin.Top = 0;
                            //pageItem.PageInfo.Margin.Bottom = 0;
                            //pageItem.PageInfo.Margin.Left = 0;
                            //pageItem.PageInfo.Margin.Right = 0;
                            pageItem.AddStamp(imageStamp);
                            //for (double y = 0; y < pageItem.PageInfo.Height; y = y + imageStamp.Height)
                            //{
                            //    for (double x = 0; x < pageItem.PageInfo.Width; x = x + imageStamp.Width)
                            //    {
                            //        imageStamp.XIndent = x;
                            //        imageStamp.YIndent = y;
                            //        pageItem.AddStamp(imageStamp);
                            //    }
                            //}
                        }
                        //save generated PDF document
                        pdfDocumentbyteArrDOC.Save(outputforpdf);

what method can let watermark's image show, but if i  set imageStamp.Background=false, watermark image stamp can show, pdf's content will be hided, what method can i keep pdf file and show image stamp simultaneously? thanks

@Owen_Sun

Would you please share your source file, generated file and expected output so that we may investigate further.

code as below:
var data = FileProvider.DownloadFile(FileDownMess);
 MemoryStream fs = new MemoryStream();
MemoryStream outputforpdf = new MemoryStream();
data[0].MemoryStream.Position = 0;
 fs.Write(data[0].MemoryStream.ToArray(), 0, data[0].MemoryStream.ToArray().Length);

outputforpdf = fs;
 pdfDocumentbyteArrDOC = new Document(new MemoryStream(byteArrDOC));
                        
                            // Whether the stamp is background
                            string pdfWaterMarkFileDir = Server.MapPath("~/Content/Images/watermark.jpg");
                            imageStamp = new ImageStamp(pdfWaterMarkFileDir);
                            //imageStamp.Height = 100;
                            //imageStamp.Width = 100;
                            imageStamp.Background = true;
                            imageStamp.Opacity = 0.5;

                            foreach (Page pageItem in pdfDocumentbyteArrDOC.Pages)
                            {
                                // whole page add watermark for fill with the whole screen

                                //assuming margins as zero
                                pageItem.PageInfo.Margin.Top = 0;
                                pageItem.PageInfo.Margin.Bottom = 0;
                                pageItem.PageInfo.Margin.Left = 0;
                                pageItem.PageInfo.Margin.Right = 0;
                                
                                //work code
                                pageItem.AddStamp(imageStamp);
                            }
                            pdfDocumentbyteArrDOC.Save(outputforpdf);

  private byte[] ObjectToByteArray(Object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(ms, obj);
                return ms.ToArray();
            }
        }

this is my code, i mainly through memorystream to finish the function, hope that you can give me an advise, thanks

@Farhan.Raza
add my original pdf file as below:
Node.js开发指南.pdf (7.9 MB)

add my watermark file as below:
watermark.jpg (299.8 KB)

i want to let watermark file not be overlapped by original pdf file and show the watermark image in pdf file

@Owen_Sun

Thank you for sharing requested data.

We have attached generated file for your kind reference. Would you please further elaborate your requirements or concerns with screenshots, or share a PDF document as expected output so that we may investigate further. NodeJS_19.8.pdf

@Farhan.Raza
your current NodeJS_19.8.pdf file is the effect that i want, any code can implement this effect? thanks

i also try below code to implement the effect, but some part stamp of my watermark still be overlapped. not meet my need:
var data = FileProvider.DownloadFile(FileDownMess);
MemoryStream fs = new MemoryStream();
data[0].MemoryStream.Position = 0;
fs.Write(data[0].MemoryStream.ToArray(), 0, data[0].MemoryStream.ToArray().Length);
Aspose.Words.Document doc11 = new Aspose.Words.Document(fs);
string watermarkText = “Aspose.Words for .NET”;

                        // Create a watermark shape. This will be a WordArt shape.
                        // You are free to try other shape types as watermarks.
                        Shape watermark = new Shape(doc11, Aspose.Words.Drawing.ShapeType.TextPlainText);

                        // Set up the text of the watermark.
                        watermark.TextPath.Text = watermarkText;
                        watermark.TextPath.FontFamily = "Arial";
                        watermark.Width = 500;
                        watermark.Height = 100;

                        // Text will be directed from the bottom-left to the top-right corner.
                        watermark.Rotation = -40;

                        // Remove the following two lines if you need a solid black text.
                        watermark.Fill.Color = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark
                        watermark.StrokeColor = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark

                        // Place the watermark in the page center.
                        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
                        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
                        watermark.WrapType = WrapType.None;
                        watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
                        watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;

                        // Create a new paragraph and append the watermark to this paragraph.
                        Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc11);
                        watermarkPara.AppendChild(watermark);

                        // Insert the watermark into all headers of each document section.
                        foreach (Section sect in doc11.Sections)
                        {
                            // There could be up to three different headers in each section, since we want
                            // the watermark to appear on all pages, insert into all headers.
                            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
                            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
                            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
                        }
                        doc11.Save(outputforpdf, Aspose.Words.SaveFormat.Pdf);

static void insertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];

        if (header == null)
        {
            // There is no header of the specified type in the current section, create it.
            header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
            sect.HeadersFooters.Add(header);
        }

        // Insert a clone of the watermark into the header.
        header.AppendChild(watermarkPara.Clone(true));
    }

@Owen_Sun

We have used below code to generate the PDF document shared above, please ensure using Aspose.PDF for .NET 19.8.

var pdfDocumentbyteArrDOC = new Document(dataDir + "Node.js开发指南.pdf");

// Whether the stamp is background
string pdfWaterMarkFileDir = dataDir + "watermark.jpg";
var imageStamp = new ImageStamp(pdfWaterMarkFileDir);
//imageStamp.Height = 100;
//imageStamp.Width = 100;
imageStamp.Background = true;
imageStamp.Opacity = 0.5;

foreach (Page pageItem in pdfDocumentbyteArrDOC.Pages)
{
    // whole page add watermark for fill with the whole screen

    //assuming margins as zero
    pageItem.PageInfo.Margin.Top = 0;
    pageItem.PageInfo.Margin.Bottom = 0;
    pageItem.PageInfo.Margin.Left = 0;
    pageItem.PageInfo.Margin.Right = 0;

    //work code
    pageItem.AddStamp(imageStamp);
}
pdfDocumentbyteArrDOC.Save(dataDir + "NodeJS_19.8.pdf");

@Farhan.Raza
thanks, now i use Aspose.PDF for .Net 19.3, i will use 19.8 to try it

i have tried it, some pdf that own pdf don’t have background image can work, but some pdf that have strong background image can’t work normally, such as previous pdf “Node.js开发指南.pdf”
work screenshot as below:
aspose.pdf effect work.jpg (29.0 KB)
not work screenshot as below:
aspose.pdf effect.jpg (36.3 KB)

by the way, i don’t have license for aspose.pdf.dll, i use trial version, does it matter? thanks

by the way, whether have method to check watermark has added in pdf file? thanks

@Owen_Sun

Please note that Evaluation Version Limitations apply if a license is not set. In case you do not have a valid license, you may apply for a free 30 days temporary license. So please try setting the license first and then share your kind feedback with us if issue persists.

Moreover, you may check existing watermark with ArtifactSubtype property as explained in Working with Existing Watermarks.

@Farhan.Raza

check existing watermark with ArtifactSubtype property also need Aspose.PDF for .Net 19.8 version? i use 19.3 and valid license and it can’t check watermark although my pdf has watermark in it

@Owen_Sun

Please upgrade to latest version which includes more features and bug fixes. You may try with free evaluation license for testing, as explained above.

@Farhan.Raza
i try to apply for temporary license, but i need to add the Aspose.PDF to product cart for online order, what method i can get the free temporary license, thanks

@Owen_Sun

The details for getting a temporary license are mentioned here. You can apply for it on step 5 of 8.