From c7bc4e3f944a655497cc95c0cbc163efc9f37dc0 Mon Sep 17 00:00:00 2001 From: Christopher Green Date: Fri, 25 Oct 2024 14:49:01 -0500 Subject: [PATCH] Run formatter on Java code --- .../JSON Payload/PDFWithAddedText.java | 169 +++++++++--------- .../Multipart Payload/PDFWithAddedText.java | 97 +++++----- 2 files changed, 134 insertions(+), 132 deletions(-) diff --git a/Java/Endpoint Examples/JSON Payload/PDFWithAddedText.java b/Java/Endpoint Examples/JSON Payload/PDFWithAddedText.java index 3b5057a..8627ee4 100644 --- a/Java/Endpoint Examples/JSON Payload/PDFWithAddedText.java +++ b/Java/Endpoint Examples/JSON Payload/PDFWithAddedText.java @@ -8,92 +8,93 @@ public class PDFWithAddedText { - // Specify the path to your file here, or as the first argument when running the program. - private static final String DEFAULT_FILE_PATH = "/path/to/file"; - - // 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 inputFile; - if (args.length > 0) { - inputFile = new File(args[0]); - } else { - inputFile = new File(DEFAULT_FILE_PATH); - } - final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); - - String uploadString = uploadFile(inputFile); - JSONObject uploadJSON = new JSONObject(uploadString); - if (uploadJSON.has("error")) { - System.out.println("Error during upload: " + uploadString); - return; - } - JSONArray fileArray = uploadJSON.getJSONArray("files"); - - JSONObject fileObject = fileArray.getJSONObject(0); - - String uploadedID = fileObject.get("id").toString(); - - String textOptions = "[{\\\"font\\\":\\\"Times New Roman\\\",\\\"max_width\\\":\\\"175\\\",\\\"opacity\\\":\\\"1\\\",\\\"page\\\":\\\"1\\\",\\\"rotation\\\":\\\"0\\\",\\\"text\\\":\\\"sample text in PDF\\\",\\\"text_color_rgb\\\":\\\"0,0,0\\\",\\\"text_size\\\":\\\"30\\\",\\\"x\\\":\\\"72\\\",\\\"y\\\":\\\"144\\\"}]"; - - String JSONString = - String.format("{\"id\":\"%s\", \"text_objects\":\"%s\"}", uploadedID, textOptions); - - final RequestBody requestBody = - RequestBody.create(JSONString, MediaType.parse("application/json")); - - Request request = - new Request.Builder() - .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) - .url("https://api.pdfrest.com/pdf-with-added-text") - .post(requestBody) - .build(); - try { - OkHttpClient client = - new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); - - Response response = client.newCall(request).execute(); - System.out.println("Processing Result code " + response.code()); - if (response.body() != null) { - System.out.println(prettyJson(response.body().string())); - } - } catch (IOException e) { - throw new RuntimeException(e); - } + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file"; + + // 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 inputFile; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); } + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); - private static String prettyJson(String json) { - // https://stackoverflow.com/a/9583835/11996393 - return new JSONObject(json).toString(4); + String uploadString = uploadFile(inputFile); + JSONObject uploadJSON = new JSONObject(uploadString); + if (uploadJSON.has("error")) { + System.out.println("Error during upload: " + uploadString); + return; } - - // This function is just a copy of the 'Upload.java' file to upload a binary file - private static String uploadFile(File inputFile) { - - final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); - - final RequestBody requestBody = - RequestBody.create(inputFile, MediaType.parse("application/pdf")); - - Request request = - new Request.Builder() - .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) - .header("Content-Filename", "File.pdf") - .url("https://api.pdfrest.com/upload") - .post(requestBody) - .build(); - try { - OkHttpClient client = new OkHttpClient().newBuilder().build(); - Response response = client.newCall(request).execute(); - System.out.println("Upload Result code " + response.code()); - if (response.body() != null) { - return response.body().string(); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return ""; + JSONArray fileArray = uploadJSON.getJSONArray("files"); + + JSONObject fileObject = fileArray.getJSONObject(0); + + String uploadedID = fileObject.get("id").toString(); + + String textOptions = + "[{\\\"font\\\":\\\"Times New Roman\\\",\\\"max_width\\\":\\\"175\\\",\\\"opacity\\\":\\\"1\\\",\\\"page\\\":\\\"1\\\",\\\"rotation\\\":\\\"0\\\",\\\"text\\\":\\\"sample text in PDF\\\",\\\"text_color_rgb\\\":\\\"0,0,0\\\",\\\"text_size\\\":\\\"30\\\",\\\"x\\\":\\\"72\\\",\\\"y\\\":\\\"144\\\"}]"; + + String JSONString = + String.format("{\"id\":\"%s\", \"text_objects\":\"%s\"}", uploadedID, textOptions); + + final RequestBody requestBody = + RequestBody.create(JSONString, MediaType.parse("application/json")); + + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/pdf-with-added-text") + .post(requestBody) + .build(); + try { + OkHttpClient client = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + + Response response = client.newCall(request).execute(); + System.out.println("Processing Result code " + response.code()); + if (response.body() != null) { + System.out.println(prettyJson(response.body().string())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String prettyJson(String json) { + // https://stackoverflow.com/a/9583835/11996393 + return new JSONObject(json).toString(4); + } + + // This function is just a copy of the 'Upload.java' file to upload a binary file + private static String uploadFile(File inputFile) { + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody requestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .header("Content-Filename", "File.pdf") + .url("https://api.pdfrest.com/upload") + .post(requestBody) + .build(); + try { + OkHttpClient client = new OkHttpClient().newBuilder().build(); + Response response = client.newCall(request).execute(); + System.out.println("Upload Result code " + response.code()); + if (response.body() != null) { + return response.body().string(); + } + } catch (IOException e) { + throw new RuntimeException(e); } + return ""; + } } diff --git a/Java/Endpoint Examples/Multipart Payload/PDFWithAddedText.java b/Java/Endpoint Examples/Multipart Payload/PDFWithAddedText.java index c4e815e..ee99e65 100644 --- a/Java/Endpoint Examples/Multipart Payload/PDFWithAddedText.java +++ b/Java/Endpoint Examples/Multipart Payload/PDFWithAddedText.java @@ -7,55 +7,56 @@ public class PDFWithAddedText { - // Specify the path to your file here, or as the first argument when running the program. - private static final String DEFAULT_FILE_PATH = "/path/to/file"; - - // 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 inputFile; - if (args.length > 0) { - inputFile = new File(args[0]); - } else { - inputFile = new File(DEFAULT_FILE_PATH); - } - - final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); - - final String text_options = "[{\"font\":\"Times New Roman\",\"max_width\":\"175\",\"opacity\":\"1\",\"page\":\"1\",\"rotation\":\"0\",\"text\":\"sample text in PDF\",\"text_color_rgb\":\"0,0,0\",\"text_size\":\"30\",\"x\":\"72\",\"y\":\"144\"}]"; - final RequestBody inputFileRequestBody = - RequestBody.create(inputFile, MediaType.parse("application/pdf")); - RequestBody requestBody = - new MultipartBody.Builder() - .setType(MultipartBody.FORM) - .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) - .addFormDataPart("text_objects", text_options) - .addFormDataPart("output", "pdfrest_added_text") - .build(); - Request request = - new Request.Builder() - .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) - .url("https://api.pdfrest.com/pdf-with-added-text") - .post(requestBody) - .build(); - try { - OkHttpClient client = - new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); - - Response response = client.newCall(request).execute(); - System.out.println("Result code " + response.code()); - if (response.body() != null) { - System.out.println(prettyJson(response.body().string())); - } - } catch (IOException e) { - throw new RuntimeException(e); - } + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file"; + + // 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 inputFile; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); } - private static String prettyJson(String json) { - // https://stackoverflow.com/a/9583835/11996393 - return new JSONObject(json).toString(4); + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final String text_options = + "[{\"font\":\"Times New Roman\",\"max_width\":\"175\",\"opacity\":\"1\",\"page\":\"1\",\"rotation\":\"0\",\"text\":\"sample text in PDF\",\"text_color_rgb\":\"0,0,0\",\"text_size\":\"30\",\"x\":\"72\",\"y\":\"144\"}]"; + final RequestBody inputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + RequestBody requestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) + .addFormDataPart("text_objects", text_options) + .addFormDataPart("output", "pdfrest_added_text") + .build(); + Request request = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/pdf-with-added-text") + .post(requestBody) + .build(); + try { + OkHttpClient client = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + + Response response = client.newCall(request).execute(); + System.out.println("Result code " + response.code()); + if (response.body() != null) { + System.out.println(prettyJson(response.body().string())); + } + } catch (IOException e) { + throw new RuntimeException(e); } + } + + private static String prettyJson(String json) { + // https://stackoverflow.com/a/9583835/11996393 + return new JSONObject(json).toString(4); + } }