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?