How to remove PDF annotations including comments in a document in Python
PDF annotations are certainly useful during the creation process, but it is usually an important step to strip them out before presenting it to a client or the public. For an efficient solution in Python, it’s going to take some serious time. If you’d rather get things done quickly, I’ve got a nice method you can use.
The fastest way to remove PDF annotations is through an API, so let’s install its client:
pip install cloudmersive-convert-api-client
Now instance the API, with which edit_pdf_remove_all_annotations can be called, as below:
from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Remove all PDF annotations, including comments in the documentapi_response = api_instance.edit_pdf_remove_all_annotations(input_file)pprint(api_response)except ApiException as e:print("Exception when calling EditPdfApi->edit_pdf_remove_all_annotations: %s\n" % e)
And that’s it for this solution — pretty simple. There are also similar functions in this library that will allow removing a specific single annotation, adding annotations, and retrieving annotations.