Tutorial: How to Delete Annotations from Documents
Learning Objectives
In this tutorial, you’ll learn how to:
- Remove specific annotations from documents by their IDs
- Create a workflow for annotation management
- Implement annotation deletion in different programming languages
- Handle annotation deletion responses and error scenarios
What is Annotation Deletion?
Annotation deletion allows you to remove one or more specific annotations from a document while preserving others. This is useful for cleaning up documents, implementing approval workflows, or removing outdated feedback.
Prerequisites
Before starting this tutorial, ensure you have:
- A GroupDocs.Annotation Cloud account (or get a free trial
- Your Client ID and Client Secret credentials
- A development environment for your preferred language
- An annotated document uploaded to your GroupDocs.Annotation Cloud storage
- Knowledge of annotation IDs you want to delete (using the Extract Annotations API)
Implementation Steps
Let’s walk through the process of deleting specific annotations from a document:
1. Authentication
First, authenticate with the GroupDocs.Annotation Cloud API:
// Get JWT token
curl -v "https://api.groupdocs.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Save the received JWT token for subsequent API calls.
2. Identify Annotation IDs to Delete
Before deleting annotations, you need to know their IDs. You can get these by using the Extract Annotations endpoint:
// Get annotation IDs
curl -v "https://api.groupdocs.cloud/v2.0/annotation/extract" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d "{ \"FilePath\": \"annotated-document.docx\"}"
From the response, note the IDs of the annotations you want to delete.
3. Delete Specific Annotations
Use the POST /annotation/remove
endpoint to delete the annotations:
// Delete annotations
curl -v "https://api.groupdocs.cloud/v2.0/annotation/remove" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d "{
\"FileInfo\": {
\"FilePath\": \"annotated-document.docx\"
},
\"AnnotationIds\": [1, 2, 3],
\"OutputPath\": \"output/cleaned-document.docx\"
}"
4. Download the Updated Document
After successful deletion, download your updated document using the File API.
Try It Yourself
Now, let’s implement annotation deletion in different programming languages.
C# Example
// For complete examples, visit: https://github.com/groupdocs-annotation-cloud/groupdocs-annotation-cloud-dotnet-samples
string MyAppKey = "YOUR_APP_KEY"; // Get AppKey and AppSID from https://dashboard.groupdocs.cloud
string MyAppSid = "YOUR_APP_SID";
var configuration = new Configuration(MyAppSid, MyAppKey);
var apiInstance = new AnnotateApi(configuration);
var fileInfo = new FileInfo { FilePath = "annotated-document.docx" };
// First, extract annotations to get their IDs
var extractedAnnotations = apiInstance.Extract(new ExtractRequest(fileInfo));
Console.WriteLine("Found annotations: " + extractedAnnotations.Count);
// For this example, let's delete the first 2 annotations
var annotationIdsToDelete = extractedAnnotations
.Take(2)
.Select(a => a.Id)
.ToList();
Console.WriteLine("Deleting annotation IDs: " + string.Join(", ", annotationIdsToDelete));
// Create delete options
var options = new RemoveOptions
{
FileInfo = fileInfo,
AnnotationIds = annotationIdsToDelete,
OutputPath = "output/cleaned-document.docx"
};
// Delete the annotations
var link = apiInstance.RemoveAnnotations(new RemoveAnnotationsRequest(options));
Console.WriteLine("Annotations deleted, updated document saved at: " + link.Title);
Java Example
// For complete examples, visit: https://github.com/groupdocs-annotation-cloud/groupdocs-annotation-cloud-java-samples
String clientId = "YOUR_CLIENT_ID"; // Get ClientId and ClientSecret from https://dashboard.groupdocs.cloud
String clientSecret = "YOUR_CLIENT_SECRET";
Configuration configuration = new Configuration(clientId, clientSecret);
AnnotateApi apiInstance = new AnnotateApi(configuration);
// Create file info object
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("annotated-document.docx");
// First, extract annotations to get their IDs
List<AnnotationInfo> extractedAnnotations = apiInstance.extract(new ExtractRequest(fileInfo));
System.out.println("Found annotations: " + extractedAnnotations.size());
// For this example, let's delete the first 2 annotations
List<Integer> annotationIdsToDelete = new ArrayList<>();
for (int i = 0; i < Math.min(2, extractedAnnotations.size()); i++) {
annotationIdsToDelete.add(extractedAnnotations.get(i).getId());
}
System.out.println("Deleting annotation IDs: " + String.join(", ",
annotationIdsToDelete.stream().map(String::valueOf).collect(Collectors.toList())));
// Create delete options
RemoveOptions options = new RemoveOptions();
options.setFileInfo(fileInfo);
options.setAnnotationIds(annotationIdsToDelete);
options.setOutputPath("output/cleaned-document.docx");
// Delete the annotations
AnnotationApiLink link = apiInstance.removeAnnotations(new RemoveAnnotationsRequest(options));
System.out.println("Annotations deleted, updated document saved at: " + link.getTitle());
Python Example
# For complete examples, visit: https://github.com/groupdocs-annotation-cloud/groupdocs-annotation-cloud-python-samples
import groupdocs_annotation_cloud
app_sid = "YOUR_APP_SID" # Get AppKey and AppSID from https://dashboard.groupdocs.cloud
app_key = "YOUR_APP_KEY"
api = groupdocs_annotation_cloud.AnnotateApi.from_keys(app_sid, app_key)
# Set up file info for the annotated document
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "annotated-document.docx"
# First, extract annotations to get their IDs
extracted_annotations = api.extract(groupdocs_annotation_cloud.ExtractRequest(file_info))
print(f"Found annotations: {len(extracted_annotations)}")
# For this example, let's delete the first 2 annotations
annotation_ids_to_delete = [ann.id for ann in extracted_annotations[:2]]
print(f"Deleting annotation IDs: {', '.join(map(str, annotation_ids_to_delete))}")
# Create delete options
options = groupdocs_annotation_cloud.RemoveOptions()
options.file_info = file_info
options.annotation_ids = annotation_ids_to_delete
options.output_path = "output/cleaned-document.docx"
# Delete the annotations
request = groupdocs_annotation_cloud.RemoveAnnotationsRequest(options)
result = api.remove_annotations(request)
print(f"Annotations deleted, updated document saved at: {result.title}")
Advanced Annotation Deletion Workflows
Here are some common workflow patterns for annotation deletion:
1. Approval-Based Cleanup
Delete annotations after they’ve been processed in an approval workflow:
# Pseudocode for approval workflow
for annotation in extracted_annotations:
if annotation.text.startswith("[APPROVED]"):
# Process approval logic here
# Delete the approval annotation after processing
annotation_ids_to_delete.append(annotation.id)
# Delete processed annotations
delete_annotations(annotation_ids_to_delete)
2. Batched Deletion
For documents with many annotations, you may want to delete them in batches:
// Pseudocode for batch deletion
List<int> allAnnotationIds = extractedAnnotations.Select(a => a.Id).ToList();
int batchSize = 10;
for (int i = 0; i < allAnnotationIds.Count; i += batchSize)
{
List<int> batch = allAnnotationIds
.Skip(i)
.Take(batchSize)
.ToList();
// Delete the current batch
DeleteAnnotations(batch);
}
3. Selective Deletion by User
Delete annotations from specific users:
// Pseudocode for user-specific deletion
String userToRemove = "John Doe";
List<Integer> userAnnotationIds = new ArrayList<>();
for (AnnotationInfo annotation : extractedAnnotations) {
if (userToRemove.equals(annotation.getCreatorName())) {
userAnnotationIds.add(annotation.getId());
}
}
// Delete all annotations from this user
DeleteAnnotations(userAnnotationIds);
4. Deletion by Annotation Type
Delete annotations of a specific type:
# Pseudocode for type-specific deletion
annotation_type_to_remove = "TextHighlight"
type_annotation_ids = []
for annotation in extracted_annotations:
if annotation.type == annotation_type_to_remove:
type_annotation_ids.append(annotation.id)
# Delete all annotations of this type
delete_annotations(type_annotation_ids)
Troubleshooting Tips
- Invalid Annotation IDs: Make sure the annotation IDs you’re trying to delete actually exist in the document
- Document Not Found: Verify that the input file path is correct
- Permission Issues: Ensure you have write permissions for the output file path
- No Changes: If the document isn’t changed, ensure that the annotation IDs are correct and that the document contains those annotations
What You’ve Learned
In this tutorial, you’ve learned how to:
- Extract annotation IDs from documents
- Delete specific annotations based on their IDs
- Create efficient annotation deletion workflows
- Handle annotation deletion in different programming languages
Further Practice
To enhance your understanding of annotation deletion, try these exercises:
- Create a UI that displays annotations and allows selective deletion
- Implement a time-based cleanup system that removes annotations older than a specific date
- Build a workflow that automatically cleans up resolved comment threads
- Develop a system that archives annotations before deleting them from documents
Next Steps
Continue your learning journey with these related tutorials: