How to insert a watermark image into existing .pdf

Dear all,


I am evaluating Aspose.PDF, how to insert a watermark into existing .PDF … I am trying to use codes below, but no luck:

private void createSignedPDF(string originalfolder, string filename, string signedfolder)
{
try
{
//open document
string pdffile = originalfolder + “\” + filename;
Document pdfDocument = new Document(pdffile);
//set coordinates
int lowerLeftX = 400;
int lowerLeftY = 75;
int upperRightX = 550;
int upperRightY = 100;

//get the page where image needs to be added
Page page = pdfDocument.Pages[pdfDocument.Pages.Count];
//load image into stream
FileStream imageStream = new FileStream(“aspose-logo.jpg”, FileMode.Open);
//add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
//using GSave operator: this operator saves current graphics state
page.Contents.Add(new Operator.GSave());
//create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
//using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
//using Do operator: this operator draws image
page.Contents.Add(new Operator.Do(ximage.Name));
//using GRestore operator: this operator restores graphics state
page.Contents.Add(new Operator.GRestore());

TextStamp textStamp = new TextStamp(“my text stamp goes here!”);
textStamp.Rotate = Rotation.on90;
textStamp.Background = true;
textStamp.TextState.FontSize = 5.0F;
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.FontStyle = FontStyles.Italic;
pdfDocument.Pages[pdfDocument.Pages.Count].AddStamp(textStamp);
pdfDocument.Pages[pdfDocument.Pages.Count].Watermark.Image.Save(“mywatermark.jpg”);

//save updated document
string newpdf_file = signedfolder + “\” + filename.Substring(0, filename.IndexOf(".pdf")) + “_SIGNED.pdf”;
pdfDocument.Save(newpdf_file);
imageStream.Close();
}
catch { }
finally { }
}

Hi,

Thanks for using our products.

I have tested the scenario where I have used one of my sample PDF document and have tried adding Image and Text watermark over it using the code snippet which you have shared earlier and as per my observations, both watermarks are properly appearing. I wonder that the problem seems to be related to the source PDF file which you are using. Can you please share the resource file so that we can test the scenario at our end. We are sorry for your inconvenience.

PS, I have tested the scenario with upcoming release version of Aspose.Pdf for .NET 7.3.0. Also please share some details regarding the problem you are facing.

Hi… please find .pdf as enclosed…


please help and many thanks in advance

Regards

Hi,


Thanks for sharing the resource file.

I have tested the scenario using Aspose.Pdf for .NET 7.2.0 where I have tried adding a sample Image and Text watermark (using the same code snippet which you have shared earlier) and as per my observations, both watermarks are properly appearing on resultant file. Please take a look over the third page of attached PDF file.

PS, Watermarks are appearing on third page because you have used the following code line to specify the page on which resources need to be added Page page = pdfDocument.Pages[pdfDocument.Pages.Count];
Hi ..

you wrote:
PS, Watermarks are appearing on third page because you have used the following code line to specify the page on which resources need to be added Page page = pdfDocument.Pages[pdfDocument.Pages.Count];

I did not see the watermark on 3rd page, what I saw is only an image on signature area..
is that what you mean by the watermark?

and I got "NullReferenceException" when running the snippet..
could you please help?

Thanks

Hi,


Please accept my apologies for a little confusion in my earlier reply. in your code snippet, first you have added an image to the PDF file and then have added “my text stamp goes here!” string as watermark. In my earlier post, I have indicated the image added to page contents as watermark. Please note that the Aspose.Pdf for .NET product box image is being added as normal page contents, however the watermark “my text stamp goes here!” is appearing on Bottom-Left position of the document.

During my testing, I am unable to notice any exception when tested with Aspose.Pdf for .NET 7.2.0. I have used VisualStudio 2010 project running over Windows 7 - X64 and I have specified the target platform of my application as .NET Framework 4.0. Can you please share which version of Aspose.Pdf for .NET you are using and also share some further details regarding your working environment.

We are sorry for your inconvenience.
yes, I saw "my text stamp goes here" on the bottom left of the pdf.
but I didnot see the watermark :( .. what I see is only an image on the bottom right .. is that what you meant by the watermark? ..

I am using VS 2010 Net Framework 4.0 on Windows 7 32 bits .. but I am facing the exception ..
can I send you my project (approx. 8MB)?

Hi,

Thanks for sharing the details.

In my previous post, I meant to say that the text appearing over the Bottom-Left of page is Text watermark which is added using TextStamp class. Whereas the image appearing over the Bottom-Right is not watermark image as its added to page contents using XImage object. In case you need to add an image as a watermark, you may consider using ImageStamp class. Besides this, I think you are still not comfortable with the current approach of adding Image/Text to PDF file because they are appearing as normal objects. You may consider following the approach specified over

because when using above approach, you can specify that either the Watermark will appear as background or not.

Now concerning to the exception that you are facing, please share the sample project. You may consider removing Aspose.PDF for .NET from project as it will reduce the size of archive. We are sorry for your inconvenience.

PS, Please do let us know the version of Aspose.PDF for .NET which you are using

Hi Thanks,

Please find enclosed my simple C# project for your review…
Again… what I need is to have a watermark image on the right bottom of the 3rd page of the .pdf…

Please help and I am looking forward to hear from you.

Many thanks in advance

Regards

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the sample application.

I tested your application with the latest version of Aspose.Pdf for .NET v7.2 and I am unable to reproduce the exception you are getting. I think you may be using some older version of Aspose.Pdf for .NET. I would suggest you to upgrade to the latest version of Aspose.Pdf for .NET and let us know whether it works fine for you.

Also, you may change you code regarding the image stamp with the below mentioned code to get your desired results.

Document pdfDocument = new Document(pdffile);

//create footer

ImageStamp imageStamp = new ImageStamp("aspose-logo.jpg");

//set properties of the stamp

imageStamp.BottomMargin = 10;

imageStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

imageStamp.VerticalAlignment = VerticalAlignment.Bottom;

pdfDocument.Pages[pdfDocument.Pages.Count].AddStamp(imageStamp);

You may also check the following link for details:

Adding Image in Footer of PDF File

Please feel free to contact support in case you need any further assistance.

Thank You & Best Regards,

Thanks, but how to display imageStamp on the center of the page?


please help

Thanks & Regards

please ignore it …

it resolved :slight_smile:

it01:
Thanks, but how to display imageStamp on the center of the page?
Hi,

I am glad to hear that you have been able to figure out the solution for your above requirement but just in case, you may try using the following code snippet to center align the Image stamp on PDF page.

imageStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;<o:p></o:p>

imageStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;


In case you need any further information, please feel free to contact.