From f23ec64e816b3593f940036880467b6a2639b6f2 Mon Sep 17 00:00:00 2001 From: Srihariharan Date: Thu, 4 Apr 2024 10:08:27 +0530 Subject: [PATCH 1/4] 880343: Need to update the PDF section in the UG documentation. --- File-Formats/PDF/Working-with-Watermarks.md | 164 ++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/File-Formats/PDF/Working-with-Watermarks.md b/File-Formats/PDF/Working-with-Watermarks.md index 7d2717768..19e4554bf 100644 --- a/File-Formats/PDF/Working-with-Watermarks.md +++ b/File-Formats/PDF/Working-with-Watermarks.md @@ -372,3 +372,167 @@ You can download a complete working sample from [GitHub](https://github.com/Sync The following screenshot shows the output of adding image watermark to an existing PDF document. Image watermark in an existing PDF + + +### Adding Watermark Annotation + +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. +The following code example explains how to add a watermark annotation in the PDF document + +{% tabs %} +{% highlight c# tabtitle="C# [Cross-platform]" %} + +//Load the PDF document +FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read); +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(50, 100, 100, 50)); +//Sets properties to the annotation +watermark.Opacity = 0.5f; +//Create the appearance of watermark +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)); +//Adds the annotation to page +lpage.Annotations.Add(watermark); + +//Save the document into stream +MemoryStream stream = new MemoryStream(); +loadedDocument.Save(stream); +//Close the document +loadedDocument.Close(true); + +{% endhighlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} + +//Load the existing PDF document +PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf"); +//Get the page +PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; + +//Creates PDF watermark annotation +PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(50, 100, 100, 50)); +//Sets properties to the annotation +watermark.Opacity = 0.5f; +//Create the appearance of watermark +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)); +//Adds the annotation to page +lpage.Annotations.Add(watermark); + +//Saves the document to disk. +loadedDocument.Save("WatermarkAnnotation.pdf"); +loadedDocument.Close(true); + +{% endhighlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} + +'Load the existing PDF document + Dim loadedDocument As New PdfLoadedDocument("input.pdf") +'Get the page +Dim lpage As PdfLoadedPage = TryCast(loadedDocument.Pages(0),PdfLoadedPage) + +'Creates PDF watermark annotation +Dim watermark As New PdfWatermarkAnnotation(New RectangleF(50, 100, 100, 50)) +watermark.Opacity = 0.5f; +'Creates the appearance of watermark +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)) +'Adds annotation to the page +lpage.Annotations.Add(watermark) + +'Saves the document to disk. +loadedDocument.Save("WatermarkAnnotation.pdf") +loadedDocument.Close(True) + +{% endhighlight %} + +{% endtabs %} + +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). + + +### Removing Watermark Annotation + +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. + +{% tabs %} +{% highlight c# tabtitle="C# [Cross-platform]" %} + + //Load the PDF document + FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); + // Iterate through the annotations collection and remove PdfLoadedWatermark annotations + foreach (PdfPageBase page in loadedDocument.Pages) + { + for (int i = page.Annotations.Count - 1; i >= 0; i--) + { + // Check if the annotation is a PdfLoadedWatermarkAnnotation + if (page.Annotations[i] is PdfLoadedWatermarkAnnotation) + { + // Remove the PdfLoadedWatermarkAnnotation + page.Annotations.RemoveAt(i); + } + } + } + + //Save the document into stream + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + //Close the document + loadedDocument.Close(true); + +{% endhighlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} + + //Load the existing PDF document + PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf"); + // Iterate through the annotations collection and remove PdfLoadedWatermark annotations + foreach (PdfPageBase page in loadedDocument.Pages) + { + for (int i = page.Annotations.Count - 1; i >= 0; i--) + { + // Check if the annotation is a PdfLoadedWatermarkAnnotation + if (page.Annotations[i] is PdfLoadedWatermarkAnnotation) + { + // Remove the PdfLoadedWatermarkAnnotation + page.Annotations.RemoveAt(i); + } + } + } + + //Saves the document to disk. + loadedDocument.Save("WatermarkAnnotation.pdf"); + loadedDocument.Close(true); + +{% endhighlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} + + 'Load the existing PDF document + Dim loadedDocument As New PdfLoadedDocument("input.pdf") + ' Iterate through the annotations collection and remove PdfLoadedWatermark annotations + For Each page As PdfPageBase In loadedDocument.Pages + Dim i As Integer = page.Annotations.Count - 1 + While i >= 0 + ' Check if the annotation is a PdfLoadedWatermarkAnnotation + If TypeOf page.Annotations(i) Is PdfLoadedWatermarkAnnotation Then + ' Remove the PdfLoadedWatermarkAnnotation + page.Annotations.RemoveAt(i) + End If + i -= 1 + End While + Next + + 'Saves the document to disk. + loadedDocument.Save("WatermarkAnnotation.pdf") + loadedDocument.Close(True) + +{% endhighlight %} + +{% endtabs %} + +You can download a complete working sample from [GitHub](). + From f277c30ac22af500d1d9c0a757fbfac7b41efacd Mon Sep 17 00:00:00 2001 From: Srihariharan Date: Thu, 4 Apr 2024 16:08:32 +0530 Subject: [PATCH 2/4] 880343: Update github sample location. --- File-Formats/PDF/Working-with-Watermarks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/File-Formats/PDF/Working-with-Watermarks.md b/File-Formats/PDF/Working-with-Watermarks.md index 19e4554bf..743550ac3 100644 --- a/File-Formats/PDF/Working-with-Watermarks.md +++ b/File-Formats/PDF/Working-with-Watermarks.md @@ -534,5 +534,5 @@ You can remove the Watermark annotation from the annotation collection, represen {% endtabs %} -You can download a complete working sample from [GitHub](). +You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Watermark/Removing-watermark-annotation-in-PDF-document). From e4460f1bee34fe9ead7e069f414be1b37dd1cf12 Mon Sep 17 00:00:00 2001 From: Srihariharan Date: Thu, 4 Apr 2024 16:36:33 +0530 Subject: [PATCH 3/4] 880343: Update proper bounds value. --- File-Formats/PDF/Working-with-Watermarks.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/File-Formats/PDF/Working-with-Watermarks.md b/File-Formats/PDF/Working-with-Watermarks.md index 743550ac3..37a26bedc 100644 --- a/File-Formats/PDF/Working-with-Watermarks.md +++ b/File-Formats/PDF/Working-with-Watermarks.md @@ -389,7 +389,7 @@ PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; //Creates PDF watermark annotation -PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(50, 100, 100, 50)); +PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(0, 0, 200, 50)); //Sets properties to the annotation watermark.Opacity = 0.5f; //Create the appearance of watermark @@ -413,7 +413,7 @@ PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf"); PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; //Creates PDF watermark annotation -PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(50, 100, 100, 50)); +PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(0, 0, 200, 50)); //Sets properties to the annotation watermark.Opacity = 0.5f; //Create the appearance of watermark @@ -435,7 +435,7 @@ loadedDocument.Close(true); Dim lpage As PdfLoadedPage = TryCast(loadedDocument.Pages(0),PdfLoadedPage) 'Creates PDF watermark annotation -Dim watermark As New PdfWatermarkAnnotation(New RectangleF(50, 100, 100, 50)) +Dim watermark As New PdfWatermarkAnnotation(New RectangleF(0, 0, 200, 50)) watermark.Opacity = 0.5f; 'Creates the appearance of watermark 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)) From 57d114392a0359162b9feb4cbd5172d50583f65f Mon Sep 17 00:00:00 2001 From: Srihariharan Date: Thu, 4 Apr 2024 17:20:25 +0530 Subject: [PATCH 4/4] 880343: Add proper bounds value. --- File-Formats/PDF/Working-with-Watermarks.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/File-Formats/PDF/Working-with-Watermarks.md b/File-Formats/PDF/Working-with-Watermarks.md index 37a26bedc..871efb119 100644 --- a/File-Formats/PDF/Working-with-Watermarks.md +++ b/File-Formats/PDF/Working-with-Watermarks.md @@ -389,7 +389,7 @@ PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; //Creates PDF watermark annotation -PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(0, 0, 200, 50)); +PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 100, 200, 50)); //Sets properties to the annotation watermark.Opacity = 0.5f; //Create the appearance of watermark @@ -413,7 +413,7 @@ PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf"); PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; //Creates PDF watermark annotation -PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(0, 0, 200, 50)); +PdfWatermarkAnnotation watermark = new PdfWatermarkAnnotation(new RectangleF(100, 100, 200, 50)); //Sets properties to the annotation watermark.Opacity = 0.5f; //Create the appearance of watermark @@ -435,7 +435,7 @@ loadedDocument.Close(true); Dim lpage As PdfLoadedPage = TryCast(loadedDocument.Pages(0),PdfLoadedPage) 'Creates PDF watermark annotation -Dim watermark As New PdfWatermarkAnnotation(New RectangleF(0, 0, 200, 50)) +Dim watermark As New PdfWatermarkAnnotation(New RectangleF(100, 100, 200, 50)) watermark.Opacity = 0.5f; 'Creates the appearance of watermark 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))