A watermark refers to a design, pattern, or text applied to a digital or physical document, image, or video to signify its ownership or authenticity. Typically, watermarks are transparent and strategically placed so as not to disrupt the content while still being visible enough to indicate the source of the material.
This advantageous feature can be utilized in various contexts, including branding and marketing, safeguarding against unauthorized usage, identifying and attributing the source of content, and ensuring document security, authenticity, and verification.
The Syncfusion .NET PDF Library supports adding image or text watermarks to a PDF document to indicate the authenticity or ownership of the document. The library provides APIs for programmatically adding watermarks to PDF documents, with options for customizing the watermark’s position, size, and opacity.
This article will cover creating a watermark for a PDF file using the Syncfusion .NET PDF Library.
Agenda:
Text watermarks are a practical technique for maintaining document security, intellectual property protection, and branding initiatives.
Follow these steps to add a text watermark to a PDF file using the Syncfusion PDF Library:
The following code example adds a text watermark in a PDF document using C#.
//Load the PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream); //Get the page. PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; //Creates PDF watermark annotation. PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 300, 400, 400)); //Sets properties to the annotation. watermark.Opacity = 0.5f; //Create the appearance of watermark. watermark.Appearance.Normal.Graphics.DrawString("Imported using Essential PDF", new PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Red, new RectangleF(50, 50, 250, 50), new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)); //Adds the annotation to page. lpage.Annotations.Add(watermark); FileStream stream = new FileStream("Output.pdf", FileMode.Create); //Save the modified document to file. loadedDocument.Save(stream); //Close the PDF document. loadedDocument.Close(true); stream.Close();
Execute this code example to get a text watermarked PDF like the following screenshot.
Image watermarks can be useful for protecting your intellectual property, establishing your brand identity, adding security features to your documents, and indicating confidentiality.
Follow these steps to add an image watermark to a PDF file using the Syncfusion PDF Library:
The following code example adds an image watermark in a PDF document using C#.
//Load the PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Get the page. PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; //Creates PDF watermark annotation. PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 300, 400, 400)); //Sets properties to the annotation. watermark.Opacity = 0.5f; //Create the appearance of watermark. FileStream imageStream = new FileStream(“Image.jpg”, FileMode.Open, FileAccess.Read); PdfImage image = new PdfBitmap(imageStream); watermark.Appearance.Normal.Graphics.DrawImage(image, new PointF(0, 0)); //Adds the annotation to page. lpage.Annotations.Add(watermark); FileStream stream = new FileStream("output.pdf", FileMode.Create); //Save the modified document to file. loadedDocument.Save(stream); //Close the PDF document. loadedDocument.Close(true); stream.Close();
Execute this code example to get an image watermarked PDF document like the following screenshot.
Removing a watermark from a PDF document can be useful in cases where the watermark is no longer needed or when the PDF file needs to be shared without the watermark.
Follow these steps to remove a watermark from a PDF file using the Syncfusion PDF Library:
The following code example removes a watermark from a PDF using C#.
//Load the PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); for (int i = 0; i < loadedDocument.Pages.Count; i++) { //Get the page. PdfLoadedPage lpage = loadedDocument.Pages[i] as PdfLoadedPage; //Gets the annotation collection. PdfLoadedAnnotationCollection annotations = lpage.Annotations; for (int j = 0; j < annotations.Count; j++) { //Gets the annotation. PdfLoadedAnnotation annotation = lpage.Annotations[j] as PdfLoadedAnnotation; if (annotation != null && annotation is PdfLoadedWatermarkAnnotation) { //Removes the first annotation. annotations.RemoveAt(j); } } } FileStream stream = new FileStream("output.pdf", FileMode.Create); //Save the modified document to file. loadedDocument.Save(stream); //Close the PDF document. loadedDocument.Close(true); stream.Close();
Execute this code example to remove a watermark from a PDF document like in the following screenshot.
You can find examples for doing this in the GitHub repository.
Thanks for reading! In this blog post, we have learned how to add image and text watermarks in a C# .NET console application using our Syncfusion PDF Library. Try out these methods and leave your feedback in the comments section of this blog post!
In the PDF Library, there are other features such as form authentication, headers, footers, tables of contents, and automatic bookmark hierarchy based on heading style. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.
For existing customers, the new version of Essential Studio® is available for download from the License and Downloads page. If you are not a Syncfusion customer, try our 30-day free trial to check out our available features.
For questions, contact us through our support forum, support portal, or feedback portal. We are delighted to assist you!