Count Words in PDF Document with Java REST API

This short tutorial guides how to count words in PDF document with Java REST API. You will learn to automatically count words in PDF file with Java REST API using the Java-based Cloud SDK. There is no need to install any third-party tool for counting words in the PDF.

Prerequisite

Steps to Count Words on PDF with Java REST Interface

  1. Instantiate the PdfApi using the client ID and secret for counting words
  2. Upload the source PDF file with the name into the cloud storage for processing
  3. Call the GetWordsPerPage() method using the uploaded source PDF file
  4. Parse the API response and use the List in the WordsPerPage object
  5. Display the page number and number of words on it from the list

These steps describe how to develop an application for PDF word count with Java Low Code API. Load the source PDF file into the memory stream, upload it to the cloud storage with a specific name, and call the GetWordsPerPage() method for this file. Finally, parse the API response and iterate through the items in the WordsPerPage.List for displaying the number of words per page.

Code to Count Number of Words in PDF with Java Low Code API

import com.aspose.pdf.cloud.sdk.ApiException;
import com.aspose.pdf.cloud.sdk.Configuration;
import com.aspose.pdf.cloud.sdk.api.PdfApi;
import com.aspose.pdf.cloud.sdk.model.FileUploadResponse;
import com.aspose.pdf.cloud.sdk.model.WordCountResponse;
import com.aspose.pdf.cloud.sdk.model.PageWordCount;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PdfTasks {
public static void countWords() throws ApiException, IOException {
String clientId = "ClientID";
String clientSecret = "ClientSecret";
Configuration configuration = new Configuration(clientId, clientSecret);
PdfApi pdfApi = new PdfApi(configuration);
String fileName = "sample.pdf";
String storage = "";
String folder = "";
try {
// Upload the PDF file to Aspose Cloud storage
File pdfFile = new File(fileName);
try (FileInputStream fileInputStream = new FileInputStream(pdfFile)) {
FileUploadResponse uploadResult = pdfApi.uploadFile(fileName, fileInputStream);
System.out.println("File uploaded: " + uploadResult.getStatus());
}
// Get words count per page from the PDF document
WordCountResponse apiResponse = pdfApi.getWordsPerPage(fileName, storage, folder);
if (apiResponse != null && apiResponse.getStatus().equals("OK")) {
for (PageWordCount pageWordCount : apiResponse.getWordsPerPage().getList()) {
System.out.println("Page Number: " + pageWordCount.getPageNumber() +
" | Total Words: " + pageWordCount.getCount());
}
}
} catch (ApiException | IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
countWords();
} catch (ApiException | IOException e) {
e.printStackTrace();
}
}
}

This code demonstrates how to get word count on PDF document with Java-based API. You may set the storage name while uploading the PDF file and use the same name while calling the GetWordsPerPage() to refer to the uploaded PDF file. Load the input PDF file from the disk or some other source such as a database or any other memory stream.

This article has guided us to get word count from PDF with Java REST Interface. If you want to create a fillable PDF, refer to the following article: Create a Fillable PDF with Java REST API.

 English