Watermarks are one of the ways to add security to your documents that contain highly confidential information or can be used to verify if a PDF document is a copy or original or from the authorized company. These are similar to stamps but cannot be moved or changed like stamps. For more watermark information, see PDF specification 2.0 (Section 12.5.6.22).
DsPdf allows you to add text watermarks, image watermarks, and watermark annotations. Refer to the following sections to add watermarks to a PDF document:
DsPdf provides TextLayout class that enables you to add a text watermark to the PDF document using CreateTextLayout method of GcGraphics class. Refer to the following example code to add a text watermark to the PDF document:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. var doc = new GcPdfDocument(); // Load PDF document. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf")); doc.Load(fs); // Add text watermark to all pages. foreach (var page in doc.Pages) { var g = page.Graphics; // Configure text layout to draw the 'watermark'. var tl = g.CreateTextLayout(); // Add the text. tl.Append("Document Solutions"); // Set the font of the text. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")); // Set font size. tl.DefaultFormat.FontSize = g.Resolution; // Set color and transparency. tl.DefaultFormat.ForeColor = Color.FromArgb(128, Color.Yellow); tl.DefaultFormat.GlyphAdvanceFactor = 1.5f; tl.PerformLayout(); // Set rotation angle (radians). Append the text from left/bottom to right/top corners of the page. var angle = -Math.Asin(g.CanvasSize.Width / g.CanvasSize.Height); // Set page center. var center = new PointF(g.CanvasSize.Width / 2, g.CanvasSize.Height / 2); // Set additional offset from text size. var delta = new PointF( (float)((tl.ContentWidth * Math.Cos(angle) - tl.ContentHeight * Math.Sin(angle)) / 2), (float)((tl.ContentWidth * Math.Sin(angle) + tl.ContentHeight * Math.Cos(angle)) / 2)); /* Draw watermark text diagonally in the center of the page. Apply matrix transforms from last to first. */ g.Transform = System.Numerics.Matrix3x2.CreateRotation((float)angle) * System.Numerics.Matrix3x2.CreateTranslation(center.X - delta.X, center.Y - delta.Y); // Draw the text. g.DrawTextLayout(tl, PointF.Empty); g.Transform = System.Numerics.Matrix3x2.Identity; } // Save the PDF document. doc.Save("AddTextWatermark.pdf"); |
DsPdf provides DrawImage method of GcGraphics class that enables you to add an image watermark to the PDF document. Refer to the following example code to add an image watermark to the PDF document:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF document. using var fs = File.OpenRead("TimeSheet.pdf"); doc.Load(fs); // Add image to the PDF document as watermark. foreach (var page in doc.Pages) { var g = page.Graphics; // Load image. var image = GrapeCity.Documents.Drawing.Image.FromFile("lotus.png"); var imageRc = new RectangleF(30F, 0F, 550, 1000); // Draw image. g.DrawImage(image, imageRc, null, ImageAlign.StretchImage, 0.2f); } // Save PDF document. doc.Save("ImageWatermark.pdf"); |
DsPdf provides WatermarkAnnotation class that allows you to add watermarks easily to PDF documents, along with additional properties to enhance the watermark. You can add both image and text watermarks to the document using this class and its properties.
Refer to the following example code to add a watermark annotation to the PDF document:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF document. using var fs = File.OpenRead("TimeSheet.pdf"); doc.Load(fs); // Add Watermark Annotation to all the pages. foreach (var page in doc.Pages) { // Set text format. TextFormat tf = new TextFormat() { Font = StandardFonts.HelveticaBold, FontSize = 50, ForeColor = Color.FromArgb(128, Color.Yellow) }; // Set watermark annotation. var watermark = new WatermarkAnnotation() { Name = "WaterMark Sample", Rect = new RectangleF(30F, 0F, 550, 700), TextFormat = tf, Text = "Document Solutions", }; // Add watermark to page. page.Annotations.Add(watermark); } // Save PDF document. doc.Save("AddWatermarkAnnotation.pdf"); |
DsPdf provides Clear and RemoveAt methods that enable you to remove watermark annotations from a PDF document. Clear method removes all the annotations from a page, whereas RemoveAt method removes a particular annotation by specifying its index value. Refer to the following example code to remove a watermark annotation from the PDF document:
C# |
Copy Code
|
---|---|
// Initialize GcPdfDocument. GcPdfDocument doc = new GcPdfDocument(); // Load PDF document. using var fs = File.OpenRead("ImageWatermark.pdf"); doc.Load(fs); // Check for watermark annotation and remove it. foreach (var page in doc.Pages) { for (int i = page.Annotations.Count - 1; i >= 0; i--) { if (page.Annotations[i] is WatermarkAnnotation) { page.Annotations.RemoveAt(i); } } } // Or // Remove all annotations. foreach (var page in doc.Pages) { page.Annotations.Clear(); } // Save PDF document. doc.Save("DeleteWatermarkAnnotation.pdf"); |