How to remove PDF annotations including comments in a document in Java

Sometimes you just need your annotations to take a hike. Whether it’s for a public final draft, to improve compatibility, or simplify a format conversion, this seemingly simple task can certainly be painful to implement. Luckily, we have an API function that’s about to make your life much easier.

To get our API rolling, our library has to be compiled. And to do that, we are going to require a couple of references in our POM file.

First reference

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Second reference

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.34</version>
</dependency>
</dependencies>

Next in line is calling the editPdfRemoveAllAnnotations function.

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.EditPdfApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");EditPdfApi apiInstance = new EditPdfApi();File inputFile = new File("/path/to/file"); // File | Input file to perform the operation on.try {byte[] result = apiInstance.editPdfRemoveAllAnnotations(inputFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditPdfApi#editPdfRemoveAllAnnotations");e.printStackTrace();}

Once our file has been inputted, all of its annotations will be stripped away and the cleaned up version will be returned to us. Not bad, right?

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store