How to Get Document Type Information using Java
Document conversion and document recognition go hand in hand. With our Get Document Type Information API, you can automatically detect the contents of any given file — even without a file extension present — by auto-detecting a file’s contents. It’s capable of detecting more than 100 image file formats, Office file formats, PDFs, and more. Below, I’ve included a JSON response model for your reference:
{
"Successful": true,
"DetectedFileExtension": "string",
"DetectedMimeType": "string",
"PageCount": 0,
"Author": "string",
"DateModified": "2022-10-17T15:25:53.830Z",
"AlternateFileTypeCandidates": [
{
"Probability": 0,
"DetectedFileExtension": "string",
"DetectedMimeType": "string"
}
]
}
With the Java code examples provided below in this article, you can easily structure an API call & incorporate this API into your application. The best part? It’s completely free to use — all you need to do is register a free account on our website and use your free-tier Cloudmersive API key to authenticate the service (free-tier accounts come with a limit of 800 API calls per month: great for small-scale projects & startups).
Before calling the API function, we first need to install the API client. We can start by first adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s add a reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
With installation complete, we can turn our attention to the controller and add the following imports:
// 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.ConvertDocumentApi;
Last but not least, let’s call the function, including our API key and input file path in their respective fields (indicated by the code comments):
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth 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");ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
AutodetectGetInfoResult result = apiInstance.convertDocumentAutodetectGetInfo(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentAutodetectGetInfo");
e.printStackTrace();
}