Skip to content

Commit

Permalink
Run mvn spotless:apply
Browse files Browse the repository at this point in the history
  • Loading branch information
datalogics-jacksonm committed Jul 29, 2024
1 parent 7a7cacc commit 27a85ea
Showing 1 changed file with 81 additions and 81 deletions.
162 changes: 81 additions & 81 deletions Java/Complex Flow Examples/OcrWithExtractText.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,91 +16,91 @@

public class OcrWithExtractText {

// Specify the path to your PDF file here, or as the first argument when running the program.
private static final String DEFAULT_PDF_FILE_PATH = "/path/to/file.pdf";

// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

public static void main(String[] args) {
File pdfFile;
if (args.length > 0) {
pdfFile = new File(args[0]);
} else {
pdfFile = new File(DEFAULT_PDF_FILE_PATH);
// Specify the path to your PDF file here, or as the first argument when running the program.
private static final String DEFAULT_PDF_FILE_PATH = "/path/to/file.pdf";

// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

public static void main(String[] args) {
File pdfFile;
if (args.length > 0) {
pdfFile = new File(args[0]);
} else {
pdfFile = new File(DEFAULT_PDF_FILE_PATH);
}

final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

final RequestBody pdfFileRequestBody =
RequestBody.create(pdfFile, MediaType.parse("application/pdf"));
RequestBody ocrRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", pdfFile.getName(), pdfFileRequestBody)
.addFormDataPart("output", "example_pdf-with-ocr-text_out")
.build();
Request ocrRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf-with-ocr-text")
.post(ocrRequestBody)
.build();
try {
OkHttpClient ocrClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response ocrResponse = ocrClient.newCall(ocrRequest).execute();

System.out.println("Response status code: " + ocrResponse.code());
if (ocrResponse.body() != null) {
String ocrResponseString = ocrResponse.body().string();

JSONObject ocrJSON = new JSONObject(ocrResponseString);
if (ocrJSON.has("error")) {
System.out.println("Error during OCR call: " + ocrResponseString);
return;
}

final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

final RequestBody pdfFileRequestBody =
RequestBody.create(pdfFile, MediaType.parse("application/pdf"));
RequestBody ocrRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", pdfFile.getName(), pdfFileRequestBody)
.addFormDataPart("output", "example_pdf-with-ocr-text_out")
.build();
Request ocrRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf-with-ocr-text")
.post(ocrRequestBody)
.build();
String ocrPDFID = ocrJSON.get("outputId").toString();
System.out.println("Got the output ID: " + ocrPDFID);

RequestBody extractRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", ocrPDFID)
.build();
Request extractRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/extracted-text")
.post(extractRequestBody)
.build();
try {
OkHttpClient ocrClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response ocrResponse = ocrClient.newCall(ocrRequest).execute();

System.out.println("Response status code: " + ocrResponse.code());
if (ocrResponse.body() != null) {
String ocrResponseString = ocrResponse.body().string();

JSONObject ocrJSON = new JSONObject(ocrResponseString);
if (ocrJSON.has("error")) {
System.out.println("Error during OCR call: " + ocrResponseString);
return;
}

String ocrPDFID = ocrJSON.get("outputId").toString();
System.out.println("Got the output ID: " + ocrPDFID);

RequestBody extractRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", ocrPDFID)
.build();
Request extractRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/extracted-text")
.post(extractRequestBody)
.build();
try {
OkHttpClient extractClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response extractResponse = extractClient.newCall(extractRequest).execute();

System.out.println("Response status code: " + extractResponse.code());
if (extractResponse.body() != null) {
String extractResponseString = extractResponse.body().string();

JSONObject extractJSON = new JSONObject(extractResponseString);
if (extractJSON.has("error")) {
System.out.println("Error during text extraction call: " + extractResponseString);
return;
}

System.out.println(extractJSON.getString("fullText"));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
OkHttpClient extractClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response extractResponse = extractClient.newCall(extractRequest).execute();

System.out.println("Response status code: " + extractResponse.code());
if (extractResponse.body() != null) {
String extractResponseString = extractResponse.body().string();

JSONObject extractJSON = new JSONObject(extractResponseString);
if (extractJSON.has("error")) {
System.out.println("Error during text extraction call: " + extractResponseString);
return;
}

System.out.println(extractJSON.getString("fullText"));
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

0 comments on commit 27a85ea

Please sign in to comment.