Watermarks in PDF document
Learn about how many ways the Groupdocs.watermark can add watermarks in PDF documents.
XObjects
When Add method of Watermarker class is called, simple XObject is added to a PDF document.
PDF Reference 1.7
An external object (commonly called an XObject) is a graphics object whose contents are defined by a self-contained content stream, separate from the content stream in which it is used. There are three types of external objects:
- An image XObject represents a sampled visual image such as a photograph.
- A form XObject is a self-contained description of an arbitrary sequence of graphics objects.
- A PostScript XObject contains a fragment of code expressed in the PostScript page description language. PostScript XObjects are no longer recommended to be used.
Image XObject and Form XObject are used by GroupDocs.Watermark API to add ImageWatermark and TextWatermark respectively. XObjects are considered as a page real content, therefore, they are not removed by Adobe Acrobat during document sanitization.
Artifacts
- The real content of a document comprises objects representing material originally introduced by the document’s author.
- Artifacts are graphics objects that are typically not part of the author’s original content but rather are generated by the PDF producer application in the course of pagination, layout, or other strictly mechanical processes. Artifacts may also be used to describe areas of the document where the author uses a graphical background, with the goal of enhancing the visual experience. In such a case, the background is not required for understanding the content.
According to artifact definition, the watermark can be represented by an artifact in a PDF document. The following example shows how artifact watermark can be added to a document with GroupDocs.Watermark using PdfArtifactWatermarkOptions.
AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddArtifactWatermark
PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
PdfArtifactWatermarkOptions options = new PdfArtifactWatermarkOptions();
// Add text watermark
TextWatermark textWatermark = new TextWatermark("This is an artifact watermark", new Font("Arial", 8));
textWatermark.HorizontalAlignment = HorizontalAlignment.Right;
watermarker.Add(textWatermark, options);
// Add image watermark
using (ImageWatermark imageWatermark = new ImageWatermark(Constants.LogoBmp))
{
watermarker.Add(imageWatermark, options);
}
watermarker.Save(Constants.OutDocumentPdf);
}
Annotations
Annotation is the third type of PDF entities by which a watermark can be represented. Use the following code snippet to add watermark annotation to a PDF document using PdfAnnotationWatermarkOptions.
AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddAnnotationWatermark
PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
PdfAnnotationWatermarkOptions options = new PdfAnnotationWatermarkOptions();
// Add text watermark
TextWatermark textWatermark = new TextWatermark("This is a annotation watermark", new Font("Arial", 8));
textWatermark.HorizontalAlignment = HorizontalAlignment.Left;
textWatermark.VerticalAlignment = VerticalAlignment.Top;
watermarker.Add(textWatermark, options);
// Add image watermark
using (ImageWatermark imageWatermark = new ImageWatermark(Constants.ProtectJpg))
{
imageWatermark.HorizontalAlignment = HorizontalAlignment.Right;
imageWatermark.VerticalAlignment = VerticalAlignment.Top;
watermarker.Add(imageWatermark, options);
}
watermarker.Save(Constants.OutDocumentPdf);
}
Print-only annotations
You can also add print only annotation watermark to the document setting PrintOnly property of PdfAnnotationWatermarkOptions. The following code demonstrates this approach.
AdvancedUsage.AddingWatermarks.AddWatermarksToPdf.PdfAddPrintOnlyAnnotationWatermark
PdfLoadOptions loadOptions = new PdfLoadOptions();
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"
using (Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions))
{
TextWatermark textWatermark = new TextWatermark("This is a print only test watermark. It won't appear in view mode.", new Font("Arial", 8));
bool isPrintOnly = true;
// Annotation will be printed, but not displayed in pdf viewing application
PdfAnnotationWatermarkOptions options = new PdfAnnotationWatermarkOptions();
options.PageIndex = 0;
options.PrintOnly = isPrintOnly;
watermarker.Add(textWatermark, options);
watermarker.Save(Constants.OutDocumentPdf);
}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples:
Free online document watermarking App
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.