Free Support Forum - aspose.com

How to apply water mark text on existing pdf file saved on disk

how to apply water mark text on exisiting pdf file which was saved on the disk........

i have a question after writing byte array to pdf then we have to apply water mark or before writing byte array to pdf then we have to apply watermark?

Hi,

There are two approaches to deal with Watermarks when using Aspose.Pdf for .NET. Please visit the following link to use the new DOM API approach for Working with Stamps and Watermarks

Or you may also try using Aspose.Pdf.Facades namespace (formerly these classes were present in Aspose.Pdf.Kit for .NET) to deal with watermark. For more information, please visit Working with Stamps and Watermarks

In case you encounter any issue or you have any further query, please feel free to contact.

let me explain clearly actually iam converting word to pdf and getting steam in byte .......

using filestream i wrote tht byte array to pdf...

FileStream fileStream = new FileStream(filePaths[0].ToString (), FileMode.Create, FileAccess.Write, FileShare.Write);

fileStream.Write(arrFootNoteData, 0, arrFootNoteData.Length);

fileStream.Close();

for watermark i wrote this code not working....

//Aspose.Pdf.Pdf Printpreview = new Aspose.Pdf.Pdf();

//Aspose.Pdf.Section sec1 = Printpreview.Sections.Add();

//Aspose.Pdf.Text text = new Aspose.Pdf.Text("Preview");

//text.TextInfo.Alignment = Aspose.Pdf.AlignmentType.Center;

//text.TextInfo.FontSize = 40.0f;

//text.RotatingAngle = 45.0f;

//text.TextInfo.Color = new Aspose.Pdf.Color("gray");

//Aspose.Pdf.FloatingBox watermark = new Aspose.Pdf.FloatingBox(500, 200);

//watermark.BoxVerticalPositioning = Aspose.Pdf.BoxVerticalPositioningType.Margin;

//watermark.BoxVerticalAlignment = Aspose.Pdf.BoxVerticalAlignmentType.Top;

//watermark.ZIndex = -1;

//watermark.BoxHorizontalPositioning = Aspose.Pdf.BoxHorizontalPositioningType.Margin;

//watermark.Paragraphs.Add(text);

//Printpreview.Watermarks.Add(watermark);

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

For your existing document, you may need to use the TextStamp instead of FloatingBox (used for new document creation). I think you can use the byte[] to add the text stamp / bookmark and then save that using Aspose.Pdf. Please see the following sample code as per your requirement.

//Commented your code of writing the file stream from Byte[]

//FileStream fileStream = new FileStream(filePaths[0].ToString(), FileMode.Create, FileAccess.Write, FileShare.Write);

//fileStream.Write(arrFootNoteData, 0, arrFootNoteData.Length);

//fileStream.Close();

//open document

Document pdfDocument = new Document(new MemoryStream(arrfootNoteData));

//create text stamp

TextStamp textStamp = new TextStamp("Sample Stamp");

//set whether stamp is background

textStamp.Background = true;

//set origin

textStamp.XIndent = 100;

textStamp.YIndent = 100;

//rotate stamp

textStamp.Rotate = Rotation.on90;

//set text properties

textStamp.TextState.Font = FontRepository.FindFont("Arial");

textStamp.TextState.FontSize = 14.0F;

textStamp.TextState.FontStyle = FontStyles.Bold;

textStamp.TextState.FontStyle = FontStyles.Italic;

textStamp.TextState.ForegroundColor = System.Drawing.Color.Aqua;

//add stamp to particular page

pdfDocument.Pages[1].AddStamp(textStamp);

//save output document

pdfDocument.Save(filePaths[0].ToString());

If you need any further assistance, please feel free to contact support.

Thank You & Best Regards,

for text stamp we need any dll ?iam getting error at text stamp…

Hi,

TextStamp class belongs to Aspose.Pdf namespace. Please make sure you have included Aspose.Pdf namespace in your code.

Please feel free to contact support incase of any other issue / query.

Thank You & Best Regards,

Hi,

i did tht by adding aspose.pdf namespace in the code...but iam not getting Textstamp in intellisense of aspose.pdf....y like tht..thts y i posted tht question in forum..i tried it before wht u said....

Hi,

In order to test the scenario, I just have included the reference of Aspose.Pdf for .NET 6.2.0 in my VisualStudio 2005 solution and have provided the reference of Aspose.Pdf for .NET API using following code line and I am able to see TextStamp class in Intellisense. Can you please share which version of VisualStudio you are using ? We apologize for your inconvenience.

PS, Please take a look over the attached image of my working environment, showing TextStamp class in Intellisense.

iam using visual studio 2008 .....and iam able to see text in intellisense but not text stamp y like tht?

even iam not able to see rotation,font repository and font styles in intellisense....

Hi,

Please make sure you are using the new Merged API of Aspose.Pdf for .NET. Please download the latest release version of Aspose.Pdf for .NET 6.2.0 and try using it. In case the problem still persists, please feel free to contact. We apologize for this inconvenience.

is there any other way to add byte array and watermark to newly created pdf on the disk....

Hi,

Instead of using TextStamp, you may also try using PdfFileStamp class of Aspose.Pdf.Facades namespace to add watermark to existing PDF documents. But in order to use PdfFileStamp calss, you need to provide the reference of Aspose.Pdf.Facades namespace using import Aspose.Pdf.Facades; code line and this namespace is present in new Merged API. Please download Aspose.Pdf for .NET 6.2.0 and visit the following link for more information on Working with Stamps and Watermarks

can u pls let me know wht is this .NET 6.2.0....actually iam using .NET 3.5 version with VS2008....

i tried this below code...but water mark is not visible on pdf ..only stearm is writing to pdf...if i commented tht stream part only water mark is visible..i want both i mean stream+watermark in newly created pdf.....can u pls check my code and modify if possible

byte[] pdfBytes = File.ReadAllBytes(@"C:\Defect.PDF");

//Commented your code of writing the file stream from Byte[]

Aspose.Pdf.Pdf Printpreview = new Aspose.Pdf.Pdf();

Aspose.Pdf.Pdf Mark = InsertWaterMarkInPDF(Printpreview);

FileStream fileStream = new FileStream(@"c:\Mark.pdf", FileMode.Create, FileAccess.Write, FileShare.Write);

fileStream.Write(pdfBytes, 0, pdfBytes.Length);

fileStream.Close();

----------function call------------

private static Aspose.Pdf.Pdf InsertWaterMarkInPDF(Aspose.Pdf.Pdf pdf)

{

// Create a section object

Aspose.Pdf.Section sec1 = pdf.Sections.Add();

//text watermark

Aspose.Pdf.Text text3 = new Aspose.Pdf.Text("Draft");

// set the rotating angle value for the text

text3.RotatingAngle = 45;

Aspose.Pdf.FloatingBox watermark3 = new Aspose.Pdf.FloatingBox(108, 80);

watermark3.Left = 200;

watermark3.Top = 400;

// add text to paragraphs collection of FloatingBox

watermark3.Paragraphs.Add(text3);

// add the floating box to watermarks collection of Pdf object

pdf.Watermarks.Add(watermark3);

return pdf;

}

i tried by installing latest version which u gave for .net 6.2.0.0.....iam getting textstamp in intellisense but not pages in intesllisense..i mean pdfDocument.pages[1].AddStamp(textStamp);..iam not getting in intellisense

pls check my below code

byte[] pdfBytes = File.ReadAllBytes(@"C:\Part1.PDF");

Document pdfDocument = new Document(new MemoryStream(pdfBytes));

/create text stamp

Aspose.Pdf.TextStamp textStamp = new Aspose.Pdf.TextStamp("Sample Stamp");

//set whether stamp is background

textStamp.Background = true;

//set origin

textStamp.XIndent = 100;

textStamp.YIndent = 100;

//rotate stamp

textStamp.Rotate = Aspose.Pdf.Rotation.on90;

//set text properties

textStamp.TextState.Font = FontRepository.FindFont("Arial");

textStamp.TextState.FontSize = 14.0F;

textStamp.TextState.FontStyle = FontStyles.Bold;

textStamp.TextState.FontStyle = FontStyles.Italic;

textStamp.TextState.ForegroundColor = System.Drawing.Color.Aqua;

//add stamp to particular page

pdfDocument.pages[1].AddStamp(textStamp);..not getting pages in intellisense?

//save output document

pdfDocument.Save(@"c:\test\Mark.pdf");

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

This is very strange. I have included the Aspose.Pdf namespace in my project and it works without any problem and shows Document.Pages in the intellisense. Please create a sample application and post it here. This way we will be able to identify the issue soon.

Thank You & Best Regards,

please find the attachement on sample code of text stamp…

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

Thank you for sharing the sample application.

The issue regarding Pages Collection not appearing in intellisense is due to the following line of code in your application.

Aspose.Words.Document pdfDocument = new Aspose.Words.Document(new MemoryStream(pdfBytes));

You are opening a Pdf file, so you should use Aspose.Pdf. Document Class instead of Aspose.Words. Document Class. Please replace the above line of code with the following line of code and your code will work fine without any issue.

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(new MemoryStream(pdfBytes));

If you still face any problem, please feel free to contact support.

Thank You & Best Regards,

Hi,

Great it works fine....Actually your reply makes me so happy..i Really appreciate each and every one work in Aspose team..Thank you very much ....Thanks to One and all a lot......

have a good day...:)

I have a question to ask u.....Actually we did tht code by Newer version of aspose.pdf 6.2.0.0............Is there any possible way to do watermark code with older version of Aspose.pdf.....

Actualy we are using Aspose.pdf older version with license...so any other way to do this water mark coe with older version of Aspose.pdf....

how can i replace this given below line in older version of aspose.pdf

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(new MemoryStream(pdfBytes));

and i think there is a feasibility to do water mark code in older version of aspose.pdf..can't we manipulate the below code the way we did for newer version of aspose.pdf

Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Text text = new Aspose.Pdf.Text("DO NOT COPY");

text.TextInfo.Alignment = Aspose.Pdf.AlignmentType.Center;

text.TextInfo.FontSize = 40.0f;

text.RotatingAngle = 45.0f;

text.TextInfo.Color = new Aspose.Pdf.Color("gray");

Aspose.Pdf.FloatingBox watermark = new Aspose.Pdf.FloatingBox(500, 200);

watermark.BoxVerticalPositioning = Aspose.Pdf.BoxVerticalPositioningType.Margin;

watermark.BoxVerticalAlignment = Aspose.Pdf.BoxVerticalAlignmentType.Top;

watermark.ZIndex = -1;

watermark.BoxHorizontalPositioning = Aspose.Pdf.BoxHorizontalPositioningType.Margin;

watermark.Paragraphs.Add(text);

pdf1.Watermarks.Add(watermark);

pdf1.Save(@"c:\output.pdf");

deepi:
Is there any possible way to do watermark code with older version of Aspose.pdf.....Actualy we are using Aspose.pdf older version with license...so any other way to do this water mark coe with older version of Aspose.pdf....
how can i replace this given below line in older version of aspose.pdf

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(new MemoryStream(pdfBytes));

Hi,

Document class was introduced in Merged API release of Aspose.Pdf for .NET and its available since the version 6.0.0. I am afraid this class is not available in versions older than 6.0.0

deepi:
i think there is a feasibility to do water mark code in older version of aspose.pdf..can't we manipulate the below code the way we did for newer version of aspose.pdf

Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Text text = new Aspose.Pdf.Text("DO NOT COPY");

text.TextInfo.Alignment = Aspose.Pdf.AlignmentType.Center;

text.TextInfo.FontSize = 40.0f;

text.RotatingAngle = 45.0f;

text.TextInfo.Color = new Aspose.Pdf.Color("gray");

Aspose.Pdf.FloatingBox watermark = new Aspose.Pdf.FloatingBox(500, 200);

watermark.BoxVerticalPositioning = Aspose.Pdf.BoxVerticalPositioningType.Margin;

watermark.BoxVerticalAlignment = Aspose.Pdf.BoxVerticalAlignmentType.Top;

watermark.ZIndex = -1;

watermark.BoxHorizontalPositioning = Aspose.Pdf.BoxHorizontalPositioningType.Margin;

watermark.Paragraphs.Add(text);

pdf1.Watermarks.Add(watermark);

pdf1.Save(@"c:\output.pdf");


The above specified code is valid, when creating a PDF document from scratch using pre-6.x version of Aspose.Pdf for .NET. Nevertheless, you can still use the above specified code in Aspose.Pdf for .NET 6.2.0 but it requires a little modification. All the classes of pre-6.x release are now available under Aspose.Pdf.Generator namespace. For more information, please visit Migration from Aspose.Pdf Pre-6.x

Now getting back to your original requirement, as far as I remember you need to add watermark to an existing PDF document and it can be accomplished with new Merged API or older version of Aspose.Pdf.Kit for .NET (please note that Aspose.Pdf.kit for .NET has been merged into Aspose.Pdf for .NET and all its classes are now available under Aspose.Pdf.Facades namespace). Please upgrade your license subscription and try using the new Merged API. In case you still face any further query, please feel free to contact.