You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: File-Formats/PDF/Working-with-Watermarks.md
+164Lines changed: 164 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -372,3 +372,167 @@ You can download a complete working sample from [GitHub](https://github.com/Sync
372
372
373
373
The following screenshot shows the output of adding image watermark to an existing PDF document.
374
374
<imgsrc="Watermark_images/Watermark_img4.jpg"alt="Image watermark in an existing PDF"width="100%"Height="Auto"/>
375
+
376
+
377
+
### Adding Watermark Annotation
378
+
379
+
A watermark annotation is used to represent graphics that are expected to be printed at a fixed size and position on a page, regardless of the dimensions of the printed page. [PdfWatermarkAnnotation](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfWatermarkAnnotation.html) can be used.
380
+
The following code example explains how to add a watermark annotation in the PDF document
381
+
382
+
{% tabs %}
383
+
{% highlight c# tabtitle="C# [Cross-platform]" %}
384
+
385
+
//Load the PDF document
386
+
FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);
387
+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
388
+
//Get the page
389
+
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;
390
+
391
+
//Creates PDF watermark annotation
392
+
PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 100, 200, 50));
393
+
//Sets properties to the annotation
394
+
watermark.Opacity = 0.5f;
395
+
//Create the appearance of watermark
396
+
watermark.Appearance.Normal.Graphics.DrawString("Watermark Text", new PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Red, new RectangleF(0, 0, 200, 50), new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf");
412
+
//Get the page
413
+
PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage;
414
+
415
+
//Creates PDF watermark annotation
416
+
PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 100, 200, 50));
417
+
//Sets properties to the annotation
418
+
watermark.Opacity = 0.5f;
419
+
//Create the appearance of watermark
420
+
watermark.Appearance.Normal.Graphics.DrawString("Watermark Text", new PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Red, new RectangleF(0, 0, 200, 50), new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
Dim loadedDocument As New PdfLoadedDocument("input.pdf")
434
+
'Get the page
435
+
Dim lpage As PdfLoadedPage = TryCast(loadedDocument.Pages(0),PdfLoadedPage)
436
+
437
+
'Creates PDF watermark annotation
438
+
Dim watermark As New PdfWatermarkAnnotation(New RectangleF(100, 100, 200, 50))
439
+
watermark.Opacity = 0.5f;
440
+
'Creates the appearance of watermark
441
+
watermark.Appearance.Normal.Graphics.DrawString("Watermark Text", New PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Red, New RectangleF(0, 0, 200, 50), New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle))
442
+
'Adds annotation to the page
443
+
lpage.Annotations.Add(watermark)
444
+
445
+
'Saves the document to disk.
446
+
loadedDocument.Save("WatermarkAnnotation.pdf")
447
+
loadedDocument.Close(True)
448
+
449
+
{% endhighlight %}
450
+
451
+
{% endtabs %}
452
+
453
+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Annotation/Add-watermark-annotation-in-the-PDF-document).
454
+
455
+
456
+
### Removing Watermark Annotation
457
+
458
+
You can remove the Watermark annotation from the annotation collection, represented by the [PdfLoadedAnnotationCollection](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedAnnotationCollection.html) of the loaded page. The following code illustrates this.
459
+
460
+
{% tabs %}
461
+
{% highlight c# tabtitle="C# [Cross-platform]" %}
462
+
463
+
//Load the PDF document
464
+
FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);
465
+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
466
+
// Iterate through the annotations collection and remove PdfLoadedWatermark annotations
467
+
foreach (PdfPageBase page in loadedDocument.Pages)
468
+
{
469
+
for (int i = page.Annotations.Count - 1; i >= 0; i--)
470
+
{
471
+
// Check if the annotation is a PdfLoadedWatermarkAnnotation
472
+
if (page.Annotations[i] is PdfLoadedWatermarkAnnotation)
Dim loadedDocument As New PdfLoadedDocument("input.pdf")
516
+
' Iterate through the annotations collection and remove PdfLoadedWatermark annotations
517
+
For Each page As PdfPageBase In loadedDocument.Pages
518
+
Dim i As Integer = page.Annotations.Count - 1
519
+
While i >= 0
520
+
' Check if the annotation is a PdfLoadedWatermarkAnnotation
521
+
If TypeOf page.Annotations(i) Is PdfLoadedWatermarkAnnotation Then
522
+
' Remove the PdfLoadedWatermarkAnnotation
523
+
page.Annotations.RemoveAt(i)
524
+
End If
525
+
i -= 1
526
+
End While
527
+
Next
528
+
529
+
'Saves the document to disk.
530
+
loadedDocument.Save("WatermarkAnnotation.pdf")
531
+
loadedDocument.Close(True)
532
+
533
+
{% endhighlight %}
534
+
535
+
{% endtabs %}
536
+
537
+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Watermark/Removing-watermark-annotation-in-PDF-document).
0 commit comments