Skip to content

Instantly share code, notes, and snippets.

How to Convert PDF to Text File Programmatically in Java
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// How to Convert PDF Document to TXT File in Java using REST API
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.pdf");
settings.setFormat("txt");
TxtConvertOptions convertOptions = new TxtConvertOptions();
convertOptions.setFromPage(3);
convertOptions.setPagesCount(5);
settings.setConvertOptions(convertOptions);
settings.setOutputPath("java-testing/output-sample-file.txt");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document converted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Download File from the Cloud Storage in Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
DownloadFileRequest request = new DownloadFileRequest("java-testing\\output-sample-file.txt", MyStorage, null);
File response = apiInstance.downloadFile(request);
System.err.println("Expected response type is File: " + response.length());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}

You can convert PDF document to Text file online programmatically on the cloud. In this article, you will learn how to convert PDF to Text file Programmatically in Java using REST API.

The following topics are covered in this article:

  1. Java PDF into Text Conversion REST API and SDK Installation
  2. How to Convert PDF Document to TXT File in Java using REST API
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Upload File to Cloud Storage using Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
File fileStream = new File("H:\\groupdocs-cloud-data\\input-sample-file.pdf");
UploadFileRequest request = new UploadFileRequest("java-testing\\input-sample-file.pdf", fileStream, MyStorage);
FilesUploadResult response = apiInstance.uploadFile(request);
System.out.println("Expected response type is FilesUploadResult: " + response.getUploaded().size());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
String ClientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String MyStorage = "test-internal-storage";
Configuration configuration = new Configuration(ClientId, ClientSecret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment