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:
- Add a text watermark to a PDF using C#
- Add an image watermark to a PDF using C#
- Remove a watermark from a PDF using C#
Experience a leap in PDF technology with Syncfusion's PDF Library, shaping the future of digital document processing.
Getting started with app creation
- Create a new C# .NET console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET standard applications from NuGet.org.
Add a text watermark to a PDF using C#
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:
- Use the PdfLoadedDocument class to load an existing PDF document.
- Then, get the page reference (where you want to include the watermark text) in the PdfLoadedPage object.
- Create an instance of the PdfWatermarkAnnotation class and customize the location, size, and opacity.
- Include the watermark text using the DrawString method.
- Add the watermark annotation to the annotations collection.
- Finally, save the PDF file using the Save method.
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.
Unleash the full potential of Syncfusion's PDF Library! Explore our advanced resources and empower your apps with cutting-edge functionalities.
Add image watermark to a PDF using C#
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:
- Use the PdfLoadedDocument class to load an existing PDF document.
- Then, get the page reference (where you want to include the watermark image) in the PdfLoadedPage object.
- Create an instance of the PdfWatermarkAnnotation class.
- Customize the size, location, and opacity of the watermark instance.
- Load the image from a file to the PdfBitmap object.
- Draw the image on the graphics of the watermark instance using the DrawImage method.
- Add the watermark annotation to the annotation collection of the page.
- Finally, save the PDF file using the Save method.
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.
See a world of document processing possibilities in Syncfusion's PDF Library as we unveil its features in interactive demonstrations.
Remove a watermark from a PDF using C#
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:
- Use the PdfLoadedDocument class to load an existing PDF document.
- Get the reference for the page (where you want to remove the watermark) in the PdfLoadedPage object.
- Get the watermark annotation using the PdfLoadedAnnotation class.
- Remove the watermark by passing the index value to the RemoveAt method.
- Finally, save the PDF file using the Save method.
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.
GitHub reference
You can find examples for doing this in the GitHub repository.
Syncfusion’s high-performance PDF Library allows you to create PDF documents from scratch without Adobe dependencies.
Conclusion
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!
Related blogs
- How to Convert an Image to PDF in ASP.NET Core
- Easy Ways to Redact PDFs Using C#
- 7 Ways to Compress PDF Files in C#, VB.NET
- Digitally Sign and Verify Signatures in PDF Files Using C#
- Optical Character Recognition in PDF Using Tesseract Open-Source Engine