PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){// Initialize search criteriaImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");PdfContentpdfContent=watermarker.GetContent<PdfContent>();PossibleWatermarkCollectionpossibleWatermarks=pdfContent.Pages[0].Search(imageSearchCriteria.Or(textSearchCriteria));// Remove all found watermarksfor(inti=possibleWatermarks.Count-1;i>=0;i--){possibleWatermarks.RemoveAt(i);}watermarker.Save("document.pdf");}
Searchmethod searches watermarks of all mentioned types, but in some cases, it’s necessary to analyze only one class of PDF entities. The following articles specifically deal with different types of the watermark objects in a PDF document.
Working with XObjects
Extracting information about all XObjects in PDF document
Using GroupDocs.Watermark for .NET, you can extract information about all the XObjects in a PDF document. Following code sample performs this functionality.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfPagepageinpdfContent.Pages){foreach(PdfXObjectxObjectinpage.XObjects){if(xObject.Image!=null){Console.WriteLine(xObject.Image.Width);Console.WriteLine(xObject.Image.Height);Console.WriteLine(xObject.Image.GetBytes().Length);}Console.WriteLine(xObject.Text);Console.WriteLine(xObject.X);Console.WriteLine(xObject.Y);Console.WriteLine(xObject.Width);Console.WriteLine(xObject.Height);Console.WriteLine(xObject.RotateAngle);}}}
Removing a particular XObject
You can also remove an XObject from a page using GroupDocs.Watermark. Following code sample removes an XObject from a particular page.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Remove XObject by indexpdfContent.Pages[0].XObjects.RemoveAt(0);// Remove XObject by referencepdfContent.Pages[0].XObjects.Remove(pdfContent.Pages[0].XObjects[0]);watermarker.Save("document.pdf");}
Removing XObjects containing text with particular text formatting
You can also find and remove all XObjects containing text with a particular formatting from a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfPagepageinpdfContent.Pages){for(inti=page.XObjects.Count-1;i>=0;i--){foreach(FormattedTextFragmentfragmentinpage.XObjects[i].FormattedTextFragments){if(fragment.ForegroundColor==Color.Red){page.XObjects.RemoveAt(i);break;}}}}watermarker.Save("document.pdf");}
Adding watermark to all image XObjects
GroupDocs.Watermark API allows you to add watermark to all image XObjects in a PDF document. Following code sample serves this purpose.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Initialize image or text watermarkTextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.VerticalAlignment=VerticalAlignment.Center;watermark.RotateAngle=45;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=1;foreach(PdfPagepageinpdfContent.Pages){foreach(PdfXObjectxObjectinpage.XObjects){if(xObject.Image!=null){// Add watermark to the imagexObject.Image.Add(watermark);}}}watermarker.Save("document.pdf");}
Replacing text for particular XObjects
GroupDocs.Watermark allows you to edit and replace the text of the particular XObject. You can also replace XObject’s text with formatting as shown in the below code samples.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfXObjectxObjectinpdfContent.Pages[0].XObjects){// Replace textif(xObject.Text.Contains("Test")){xObject.Text="Passed";}}// Save documentwatermarker.Save("document.pdf");}
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfXObjectxObjectinpdfContent.Pages[0].XObjects){// Replace textif(xObject.Text.Contains("Test")){xObject.FormattedTextFragments.Clear();xObject.FormattedTextFragments.Add("Passed",newFont("Calibri",19,FontStyle.Bold),Color.Red,Color.Aqua);}}// Save documentwatermarker.Save("document.pdf");}
Replacing image for particular XObjects
Using GroupDocs.Watermark, you can also replace the image of a particular XObject. GroupDocs.Watermark allows you to loop through all the XObjects of a particular page and you can replace the image of particular XObjects using some condition as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Replace imageforeach(PdfXObjectxObjectinpdfContent.Pages[0].XObjects){if(xObject.Image!=null){xObject.Image=newPdfWatermarkableImage(File.ReadAllBytes("test.png"));}}// Save documentwatermarker.Save("document.pdf");}
Working with artifacts
Extracting information about all artifacts in PDF document
GroupDocs.Watermark enables you to extract the information about the artifacts in a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfPagepageinpdfContent.Pages){foreach(PdfArtifactartifactinpage.Artifacts){Console.WriteLine(artifact.ArtifactType);Console.WriteLine(artifact.ArtifactSubtype);if(artifact.Image!=null){Console.WriteLine(artifact.Image.Width);Console.WriteLine(artifact.Image.Height);Console.WriteLine(artifact.Image.GetBytes().Length);}Console.WriteLine(artifact.Text);Console.WriteLine(artifact.Opacity);Console.WriteLine(artifact.X);Console.WriteLine(artifact.Y);Console.WriteLine(artifact.Width);Console.WriteLine(artifact.Height);Console.WriteLine(artifact.RotateAngle);}}}
Removing a particular artifact
Following code sample shows how to remove an artifact from a particular page of the PDF document.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Remove Artifact by indexpdfContent.Pages[0].Artifacts.RemoveAt(0);// Remove Artifact by referencepdfContent.Pages[0].Artifacts.Remove(pdfContent.Pages[0].Artifacts[0]);watermarker.Save("document.pdf");}
Removing artifacts containing text with particular text formatting
You can also find and remove all artifacts containing text with a particular formatting from a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfPagepageinpdfContent.Pages){for(inti=page.Artifacts.Count-1;i>=0;i--){foreach(FormattedTextFragmentfragmentinpage.Artifacts[i].FormattedTextFragments){if(fragment.Font.Size>42){page.Artifacts.RemoveAt(i);break;}}}}watermarker.Save("document.pdf");}
Adding watermark to all image artifacts
GroupDocs.Watermark API also provides the feature of adding watermark to all image artifacts in a PDF document. Following code sample adds watermark to all image artifacts.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Initialize image or text watermarkTextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.VerticalAlignment=VerticalAlignment.Center;watermark.RotateAngle=45;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=1;foreach(PdfPagepageinpdfContent.Pages){foreach(PdfArtifactartifactinpage.Artifacts){if(artifact.Image!=null){// Add watermark to the imageartifact.Image.Add(watermark);}}}watermarker.Save("document.pdf");}
Replacing text for particular artifacts
GroupDocs.Watermark allows you to edit and replace the text of the particular artifacts. You can also replace artifact’s text with formatting as shown in the below code samples.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfArtifactartifactinpdfContent.Pages[0].Artifacts){// Replace textif(artifact.Text.Contains("Test")){artifact.Text="Passed";}}// Save documentwatermarker.Save("document.pdf");}
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfArtifactartifactinpdfContent.Pages[0].Artifacts){// Replace textif(artifact.Text.Contains("Test")){artifact.FormattedTextFragments.Clear();artifact.FormattedTextFragments.Add("Passed",newFont("Calibri",19,FontStyle.Bold),Color.Red,Color.Aqua);}}// Save documentwatermarker.Save("document.pdf");}
Replacing image for particular artifacts
Using GroupDocs.Watermark, you can also replace the image of a particular artifact. GroupDocs.Watermark allows you to loop through all the artifacts of a particular page and you can replace the image of particular artifacts using some condition as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Replace imageforeach(PdfArtifactartifactinpdfContent.Pages[0].Artifacts){if(artifact.Image!=null){artifact.Image=newPdfWatermarkableImage(File.ReadAllBytes("test.png"));}}// Save documentwatermarker.Save("document.pdf");}
Working with annotations
Extracting information about all annotations in PDF document
You can also extract information about all the annotations in a PDF document using GroupDocs.Watermark as shown in below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfPagepageinpdfContent.Pages){foreach(PdfAnnotationannotationinpage.Annotations){Console.WriteLine(annotation.AnnotationType);if(annotation.Image!=null){Console.WriteLine(annotation.Image.Width);Console.WriteLine(annotation.Image.Height);Console.WriteLine(annotation.Image.GetBytes().Length);}Console.WriteLine(annotation.Text);Console.WriteLine(annotation.X);Console.WriteLine(annotation.Y);Console.WriteLine(annotation.Width);Console.WriteLine(annotation.Height);Console.WriteLine(annotation.RotateAngle);}}}
Removing a particular annotation
Following code sample can be used to remove a particular annotation from a PDF document.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Remove Annotation by indexpdfContent.Pages[0].Annotations.RemoveAt(0);// Remove Annotation by referencepdfContent.Pages[0].Annotations.Remove(pdfContent.Pages[0].Annotations[0]);watermarker.Save("document.pdf");}
Removing annotations containing text with particular text formatting
You can also find and remove all annotations containing text with a particular formatting from a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfPagepageinpdfContent.Pages){for(inti=page.Annotations.Count-1;i>=0;i--){foreach(FormattedTextFragmentfragmentinpage.Annotations[i].FormattedTextFragments){if(fragment.Font.FamilyName=="Verdana"){page.Annotations.RemoveAt(i);break;}}}}watermarker.Save("document.pdf");}
Adding watermark to all image annotations
Similar to the other types, the watermark can be added to image annotations in PDF documents as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Initialize image or text watermarkTextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.VerticalAlignment=VerticalAlignment.Center;watermark.RotateAngle=45;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=1;foreach(PdfPagepageinpdfContent.Pages){foreach(PdfAnnotationannotationinpage.Annotations){if(annotation.Image!=null){// Add watermark to the imageannotation.Image.Add(watermark);}}}watermarker.Save("document.pdf");}
Replacing text for particular annotations
GroupDocs.Watermark allows you to edit and replace the text of the particular annotations. You can also replace annotation’s text with formatting as shown in the below code samples.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfAnnotationannotationinpdfContent.Pages[0].Annotations){// Replace textif(annotation.Text.Contains("Test")){annotation.Text="Passed";}}// Save documentwatermarker.Save("document.pdf");}
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfAnnotationannotationinpdfContent.Pages[0].Annotations){// Replace textif(annotation.Text.Contains("Test")){annotation.FormattedTextFragments.Clear();annotation.FormattedTextFragments.Add("Passed",newFont("Calibri",19,FontStyle.Bold),Color.Red,Color.Aqua);}}// Save documentwatermarker.Save("document.pdf");}
Replacing image for particular annotations
Using GroupDocs.Watermark, you can also replace the image of a particular annotation. GroupDocs.Watermark allows you to loop through all the annotations of a particular page and you can replace the image of particular annotations using some condition as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Replace imageforeach(PdfAnnotationannotationinpdfContent.Pages[0].Annotations){if(annotation.Image!=null){annotation.Image=newPdfWatermarkableImage(File.ReadAllBytes("test.png"));}}// Save documentwatermarker.Save("document.pdf");}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.