Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions DotNET/Endpoint Examples/JSON Payload/pdf-with-page-boxes-set.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
{
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
uploadRequest.Headers.Accept.Add(new("application/json"));

var uploadByteArray = File.ReadAllBytes("/path/to/file");
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");


uploadRequest.Content = uploadByteAryContent;
var uploadResponse = await httpClient.SendAsync(uploadRequest);

var uploadResult = await uploadResponse.Content.ReadAsStringAsync();

Console.WriteLine("Upload response received.");
Console.WriteLine(uploadResult);

JObject uploadResultJson = JObject.Parse(uploadResult);
var uploadedID = uploadResultJson["files"][0]["id"];
using (var SetBoxesRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-page-boxes-set"))
{
SetBoxesRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
SetBoxesRequest.Headers.Accept.Add(new("application/json"));

SetBoxesRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");

var boxOptions = new JObject
{
["boxes"] = new JArray
{
new JObject
{
["box"] = "media",
["pages"] = new JArray
{
new JObject
{
["range"] = "1",
["left"] = 100,
["top"] = 100,
["bottom"] = 100,
["right"] = 100
}
}
}
}
};

JObject parameterJson = new JObject
{
["id"] = uploadedID,
["boxes"] = boxOptions.ToString(Formatting.None),
};

SetBoxesRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
var SetBoxesResponse = await httpClient.SendAsync(SetBoxesRequest);

var SetBoxesResult = await SetBoxesResponse.Content.ReadAsStringAsync();

Console.WriteLine("Processing response received.");
Console.WriteLine(SetBoxesResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf-with-page-boxes-set"))
{
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
request.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();

var byteArray = File.ReadAllBytes("/path/to/file");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name.pdf");
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

var boxOptions = new JObject
{
["boxes"] = new JArray
{
new JObject
{
["box"] = "media",
["pages"] = new JArray
{
new JObject
{
["range"] = "1",
["left"] = 100,
["top"] = 100,
["bottom"] = 100,
["right"] = 100
}
}
}
}
};


var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(boxOptions.ToString(Formatting.None)));
multipartContent.Add(byteArrayOption, "boxes");


request.Content = multipartContent;
var response = await httpClient.SendAsync(request);

var apiResult = await response.Content.ReadAsStringAsync();

Console.WriteLine("API response received.");
Console.WriteLine(apiResult);
}
}
119 changes: 119 additions & 0 deletions Java/Endpoint Examples/JSON Payload/PDFWithPageBoxesSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;

public class PDFWithPageBoxesSet {

// 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();

JSONObject page = new JSONObject();
page.put("range", "1");
page.put("left", 100);
page.put("top", 100);
page.put("bottom", 100);
page.put("right", 100);

JSONArray pagesArray = new JSONArray();
pagesArray.put(page);

JSONObject box = new JSONObject();
box.put("box", "media");
box.put("pages", pagesArray);

JSONArray boxesArray = new JSONArray();
boxesArray.put(box);

JSONObject boxOptionsObject = new JSONObject();
boxOptionsObject.put("boxes", boxesArray);

JSONObject requestJson = new JSONObject();
requestJson.put("id", uploadedID);
requestJson.put("boxes", boxOptionsObject.toString());
requestJson.put("output", "exampleout.pdf");

final RequestBody requestBody =
RequestBody.create(requestJson.toString(), 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-page-boxes-set")
.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 "";
}
}
80 changes: 80 additions & 0 deletions Java/Endpoint Examples/Multipart Payload/PDFWithPageBoxesSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;

public class PDFWithPageBoxesSet {

// 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();

JSONObject page = new JSONObject();
page.put("range", "1");
page.put("left", 100);
page.put("top", 100);
page.put("bottom", 100);
page.put("right", 100);

JSONArray pagesArray = new JSONArray();
pagesArray.put(page);

JSONObject box = new JSONObject();
box.put("box", "media");
box.put("pages", pagesArray);

JSONArray boxesArray = new JSONArray();
boxesArray.put(box);

JSONObject boxOptionsObject = new JSONObject();
boxOptionsObject.put("boxes", boxesArray);
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("boxes", boxOptionsObject.toString())
.addFormDataPart("output", "example_out.pdf")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf-with-page-boxes-set")
.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var axios = require("axios");
var FormData = require("form-data");
var fs = require("fs");

var upload_data = fs.createReadStream("/path/to/file");

var upload_config = {
method: "post",
maxBodyLength: Infinity,
url: "https://api.pdfrest.com/upload",
headers: {
"Api-Key": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
"Content-Filename": "filename.pdf",
"Content-Type": "application/octet-stream",
},
data: upload_data, // set the data to be sent with the request
};

// send request and handle response or error
axios(upload_config)
.then(function (upload_response) {
console.log(JSON.stringify(upload_response.data));
var uploaded_id = upload_response.data.files[0].id;

const boxOptions = {
boxes: [
{
box: "media",
pages: [
{
range: "1",
left: 100,
top: 100,
bottom: 100,
right: 100,
},
],
},
],
};

var boxes_config = {
method: "post",
maxBodyLength: Infinity,
url: "https://api.pdfrest.com/pdf-with-page-boxes-set",
headers: {
"Api-Key": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
"Content-Type": "application/json",
},
data: {
id: uploaded_id,
boxes: JSON.stringify(boxOptions),
}, // set the data to be sent with the request
};

// send request and handle response or error
axios(boxes_config)
.then(function (boxes_response) {
console.log(JSON.stringify(boxes_response.data));
})
.catch(function (error) {
console.log(error);
});
})
.catch(function (error) {
console.log(error);
});
Loading