Skip to content

Commit e815cc5

Browse files
Merge pull request #2020 from syncfusion-content/880343
880343: Need to update the PDF section in the UG documentation.
2 parents fa6245c + 57d1143 commit e815cc5

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

File-Formats/PDF/Working-with-Watermarks.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,167 @@ You can download a complete working sample from [GitHub](https://github.com/Sync
372372

373373
The following screenshot shows the output of adding image watermark to an existing PDF document.
374374
<img src="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));
397+
//Adds the annotation to page
398+
lpage.Annotations.Add(watermark);
399+
400+
//Save the document into stream
401+
MemoryStream stream = new MemoryStream();
402+
loadedDocument.Save(stream);
403+
//Close the document
404+
loadedDocument.Close(true);
405+
406+
{% endhighlight %}
407+
408+
{% highlight c# tabtitle="C# [Windows-specific]" %}
409+
410+
//Load the existing PDF document
411+
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));
421+
//Adds the annotation to page
422+
lpage.Annotations.Add(watermark);
423+
424+
//Saves the document to disk.
425+
loadedDocument.Save("WatermarkAnnotation.pdf");
426+
loadedDocument.Close(true);
427+
428+
{% endhighlight %}
429+
430+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
431+
432+
'Load the existing PDF document
433+
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)
473+
{
474+
// Remove the PdfLoadedWatermarkAnnotation
475+
page.Annotations.RemoveAt(i);
476+
}
477+
}
478+
}
479+
480+
//Save the document into stream
481+
MemoryStream stream = new MemoryStream();
482+
loadedDocument.Save(stream);
483+
//Close the document
484+
loadedDocument.Close(true);
485+
486+
{% endhighlight %}
487+
488+
{% highlight c# tabtitle="C# [Windows-specific]" %}
489+
490+
//Load the existing PDF document
491+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf");
492+
// Iterate through the annotations collection and remove PdfLoadedWatermark annotations
493+
foreach (PdfPageBase page in loadedDocument.Pages)
494+
{
495+
for (int i = page.Annotations.Count - 1; i >= 0; i--)
496+
{
497+
// Check if the annotation is a PdfLoadedWatermarkAnnotation
498+
if (page.Annotations[i] is PdfLoadedWatermarkAnnotation)
499+
{
500+
// Remove the PdfLoadedWatermarkAnnotation
501+
page.Annotations.RemoveAt(i);
502+
}
503+
}
504+
}
505+
506+
//Saves the document to disk.
507+
loadedDocument.Save("WatermarkAnnotation.pdf");
508+
loadedDocument.Close(true);
509+
510+
{% endhighlight %}
511+
512+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
513+
514+
'Load the existing PDF document
515+
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).
538+

0 commit comments

Comments
 (0)