Following code example removes all annotation from PDF. However, I just want to remove watermark only from the PDF. Please provide the code example. Thanks.
// Instantiate Annotator object with input PDF path
using (Annotator annotator = new Annotator("input.pdf"))
{
SaveOptions saveOptions = new SaveOptions();
saveOptions.AnnotationTypes = AnnotationType.All;
//Save the final output PDF
annotator.Save("out.pdf", saveOptions);
}
1 Like
@tahir.manzoor
You can remove a specific annotation (if you know the annotation index) as follows:
//C# code
using (Annotator annotator = new Annotator("result.pdf"))
{
annotator.Remove(0);
annotator.Save("removed.pdf");
}
For more details, please take a look at Remove annotation from document article.
If there’s still any issue, we’d recommend you to share the PDF with annotations added by GroupDocs.Annotation for .NET.
@Atir_Tahir
Please provide the generic code to remove the watermark. In the provided code, the first annotation may not be watermark. It can be any annotation. User does not know the at what index the watermark is. Please share the generic code example. thanks.
@tahir.manzoor
You can get a list of annotations from a PDF and delete the desired one as follows:
//C# code to remove watermark annotation from a PDF
int annotationId = 0;
using (Annotator annotator = new Annotator(@"D:/source.pdf"))
{
//fetch a list of annotations
List<AnnotationBase> annotations = annotator.Get();
foreach(var annotation in annotations)
{
//look for watermark annotation
if (annotation.Type == AnnotationType.Watermark)
{
//get watermark annotation ID
annotationId = annotation.Id;
}
}
//remove annotation using its ID
annotator.Remove(annotationId);
//save output PDF
annotator.Save(@"D:/removed.pdf");
}
Please take a look at this screenshot.png (25.8 KB) and these files.zip (648.7 KB).
Using annotator.Get()
, you can always get a list of annotation in a document and then look for the desired annotation, fetch it’s ID (as described in code above), delete and save it.