We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add image as watermark

hi, i have a pdf document, created with a stream in this way:

Aspose.Pdf.Document newDoc = new Aspose.Pdf.Document(_oPDFOutputStream);

Now i need to add an image as watermark in every page of my document

i tried:

Aspose.Pdf.ImageStamp imageStamp = new Aspose.Pdf.ImageStamp(“path to image”);
for (int j = 1; j <= newDoc.Pages.Count; j++)
{
newDoc.Pages[j].AddStamp(imageStamp);
}


BUT i cannot have image saved on phisical path, i have to pass image as system.drawing.bitmap object and imagestamp does not provide this way.
So how can i manage this in any way?

Hi, Demo-1,

Here's what I did in a case like yours.....

One of ImageStamp's constructors takes a stream. Therefore, you just need to save the Bitmap to a MemoryStream first. Here's sample code: (the private method does the real work, the rest is just setup.)

//create doc with single pg + some text (to ensure img goes underneath)
var doc = new Document();
var page = doc.Pages.Add();
page.Paragraphs.Add(new TextFragment("hello, world!"));

//make a bitmap - solid color box so you can see it!
using (var bitmap = new System.Drawing.Bitmap(550, 800))
{
using (var g = System.Drawing.Graphics.FromImage(bitmap))
{
//this fills the bitmap with light green
g.FillRectangle(System.Drawing.Brushes.LightGreen,
new System.Drawing.Rectangle(
0, 0, bitmap.Width, bitmap.Height));
}
//add bitmap to page using ImageStamp
AddBitmapImageStamp(page, bitmap);
}

//save and done
doc.Save("d:\\output.pdf");
}
}

// here's where the actual work is done
private static void AddBitmapImageStamp(Page page,
System.Drawing.Bitmap bitmap)
{
//add bitmap as stamp
using (var stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
var aStamp = new ImageStamp(stream);
page.AddStamp(aStamp);
}
}

Hope this helps,
R. Ernst

Hi Richard,


Thanks for the information.

The ImageStamp object only accepts Stream or String (path) as argument and in order to add BitMap object as stamp over PDF file, one has to convert the BitMap object to Stream object and then use it with ImageStamp.

PS, We have used Stream object as its a generic approach.