Skip to content

Commit bbb1d35

Browse files
Add set page boxes samples
1 parent 4601173 commit bbb1d35

13 files changed

Lines changed: 634 additions & 1 deletion

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using System.Text;
5+
6+
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
7+
{
8+
using (var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload"))
9+
{
10+
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
11+
uploadRequest.Headers.Accept.Add(new("application/json"));
12+
13+
var uploadByteArray = File.ReadAllBytes("/path/to/file");
14+
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
15+
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
16+
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", "filename.pdf");
17+
18+
19+
uploadRequest.Content = uploadByteAryContent;
20+
var uploadResponse = await httpClient.SendAsync(uploadRequest);
21+
22+
var uploadResult = await uploadResponse.Content.ReadAsStringAsync();
23+
24+
Console.WriteLine("Upload response received.");
25+
Console.WriteLine(uploadResult);
26+
27+
JObject uploadResultJson = JObject.Parse(uploadResult);
28+
var uploadedID = uploadResultJson["files"][0]["id"];
29+
using (var SetBoxesRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-page-boxes-set"))
30+
{
31+
SetBoxesRequest.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
32+
SetBoxesRequest.Headers.Accept.Add(new("application/json"));
33+
34+
SetBoxesRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");
35+
36+
var boxes_option_array = new JArray();
37+
var boxes_option1 = new JObject
38+
{
39+
["box"] = "media",
40+
["pages"] = new JArray();
41+
};
42+
var pages_option1 = new JObject
43+
{
44+
["range"] = "1",
45+
["left"] = "100",
46+
["right"] = "100",
47+
["top"] = "100",
48+
["bottom"] = "100",
49+
};
50+
((JArray)boxes_option1["pages"]).Add(pages_option1);
51+
boxes_option_array.Add(boxes_option1);
52+
53+
JObject parameterJson = new JObject
54+
{
55+
["id"] = uploadedID,
56+
["boxes"] = JsonConvert.SerializeObject(boxes_option_array),
57+
};
58+
59+
SetBoxesRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json"); ;
60+
var SetBoxesResponse = await httpClient.SendAsync(SetBoxesRequest);
61+
62+
var SetBoxesResult = await SetBoxesResponse.Content.ReadAsStringAsync();
63+
64+
Console.WriteLine("Processing response received.");
65+
Console.WriteLine(SetBoxesResult);
66+
}
67+
}
68+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System.Text;
4+
5+
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
6+
{
7+
using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf-with-page-boxes-set"))
8+
{
9+
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
10+
request.Headers.Accept.Add(new("application/json"));
11+
var multipartContent = new MultipartFormDataContent();
12+
13+
var byteArray = File.ReadAllBytes("/path/to/file");
14+
var byteAryContent = new ByteArrayContent(byteArray);
15+
multipartContent.Add(byteAryContent, "file", "file_name.pdf");
16+
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
17+
18+
var boxes_option_array = new JArray();
19+
var boxes_option1 = new JObject
20+
{
21+
["box"] = "media",
22+
["pages"] = new JArray();
23+
};
24+
var pages_option1 = new JObject
25+
{
26+
["range"] = "1",
27+
["left"] = "100",
28+
["right"] = "100",
29+
["top"] = "100",
30+
["bottom"] = "100",
31+
};
32+
((JArray)boxes_option1["pages"]).Add(pages_option1);
33+
boxes_option_array.Add(boxes_option1);
34+
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(boxes_option_array)));
35+
multipartContent.Add(byteArrayOption, "boxes");
36+
37+
38+
request.Content = multipartContent;
39+
var response = await httpClient.SendAsync(request);
40+
41+
var apiResult = await response.Content.ReadAsStringAsync();
42+
43+
Console.WriteLine("API response received.");
44+
Console.WriteLine(apiResult);
45+
}
46+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import io.github.cdimascio.dotenv.Dotenv;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
import okhttp3.*;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
public class PDFWithPageBoxesSet {
10+
11+
// Specify the path to your file here, or as the first argument when running the program.
12+
private static final String DEFAULT_FILE_PATH = "/path/to/file";
13+
14+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
15+
// You can also put the environment variable in a .env file.
16+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
17+
18+
public static void main(String[] args) {
19+
File inputFile;
20+
if (args.length > 0) {
21+
inputFile = new File(args[0]);
22+
} else {
23+
inputFile = new File(DEFAULT_FILE_PATH);
24+
}
25+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
26+
27+
String uploadString = uploadFile(inputFile);
28+
JSONObject uploadJSON = new JSONObject(uploadString);
29+
if (uploadJSON.has("error")) {
30+
System.out.println("Error during upload: " + uploadString);
31+
return;
32+
}
33+
JSONArray fileArray = uploadJSON.getJSONArray("files");
34+
35+
JSONObject fileObject = fileArray.getJSONObject(0);
36+
37+
String uploadedID = fileObject.get("id").toString();
38+
39+
JSONObject page = new JSONObject();
40+
page.put("range", "1");
41+
page.put("left", 100);
42+
page.put("top", 100);
43+
page.put("bottom", 100);
44+
page.put("right", 100);
45+
46+
JSONArray pagesArray = new JSONArray();
47+
pagesArray.put(page);
48+
49+
JSONObject box = new JSONObject();
50+
box.put("box", "media");
51+
box.put("pages", pagesArray);
52+
53+
JSONArray boxesArray = new JSONArray();
54+
boxesArray.put(box);
55+
56+
JSONObject boxOptionsObject = new JSONObject();
57+
boxOptionsObject.put("boxes", boxesArray);
58+
59+
JSONObject requestJson = new JSONObject();
60+
requestJson.put("id", uploadedID);
61+
requestJson.put("boxes", boxOptionsObject.toString());
62+
requestJson.put("output", "exampleout.pdf");
63+
64+
65+
final RequestBody requestBody =
66+
RequestBody.create(requestJson.toString(), MediaType.parse("application/json"));
67+
68+
Request request =
69+
new Request.Builder()
70+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
71+
.url("https://api.pdfrest.com/pdf-with-page-boxes-set")
72+
.post(requestBody)
73+
.build();
74+
try {
75+
OkHttpClient client =
76+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
77+
78+
Response response = client.newCall(request).execute();
79+
System.out.println("Processing Result code " + response.code());
80+
if (response.body() != null) {
81+
System.out.println(prettyJson(response.body().string()));
82+
}
83+
} catch (IOException e) {
84+
throw new RuntimeException(e);
85+
}
86+
}
87+
88+
private static String prettyJson(String json) {
89+
// https://stackoverflow.com/a/9583835/11996393
90+
return new JSONObject(json).toString(4);
91+
}
92+
93+
// This function is just a copy of the 'Upload.java' file to upload a binary file
94+
private static String uploadFile(File inputFile) {
95+
96+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
97+
98+
final RequestBody requestBody =
99+
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
100+
101+
Request request =
102+
new Request.Builder()
103+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
104+
.header("Content-Filename", "File.pdf")
105+
.url("https://api.pdfrest.com/upload")
106+
.post(requestBody)
107+
.build();
108+
try {
109+
OkHttpClient client = new OkHttpClient().newBuilder().build();
110+
Response response = client.newCall(request).execute();
111+
System.out.println("Upload Result code " + response.code());
112+
if (response.body() != null) {
113+
return response.body().string();
114+
}
115+
} catch (IOException e) {
116+
throw new RuntimeException(e);
117+
}
118+
return "";
119+
}
120+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import io.github.cdimascio.dotenv.Dotenv;
2+
import java.io.File;
3+
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
5+
import okhttp3.*;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
9+
public class PDFWithPageBoxesSet {
10+
11+
// Specify the path to your file here, or as the first argument when running the program.
12+
private static final String DEFAULT_FILE_PATH = "/path/to/file";
13+
14+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
15+
// You can also put the environment variable in a .env file.
16+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
17+
18+
public static void main(String[] args) {
19+
File inputFile;
20+
if (args.length > 0) {
21+
inputFile = new File(args[0]);
22+
} else {
23+
inputFile = new File(DEFAULT_FILE_PATH);
24+
}
25+
26+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
27+
28+
JSONObject page = new JSONObject();
29+
page.put("range", "1");
30+
page.put("left", 100);
31+
page.put("top", 100);
32+
page.put("bottom", 100);
33+
page.put("right", 100);
34+
35+
JSONArray pagesArray = new JSONArray();
36+
pagesArray.put(page);
37+
38+
JSONObject box = new JSONObject();
39+
box.put("box", "media");
40+
box.put("pages", pagesArray);
41+
42+
JSONArray boxesArray = new JSONArray();
43+
boxesArray.put(box);
44+
45+
JSONObject boxOptionsObject = new JSONObject();
46+
boxOptionsObject.put("boxes", boxesArray);
47+
final RequestBody inputFileRequestBody =
48+
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
49+
RequestBody requestBody =
50+
new MultipartBody.Builder()
51+
.setType(MultipartBody.FORM)
52+
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
53+
.addFormDataPart("boxes", boxOptionsObject.toString())
54+
.addFormDataPart("output", "example_out.pdf")
55+
.build();
56+
Request request =
57+
new Request.Builder()
58+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
59+
.url("https://api.pdfrest.com/pdf-with-page-boxes-set")
60+
.post(requestBody)
61+
.build();
62+
try {
63+
OkHttpClient client =
64+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
65+
66+
Response response = client.newCall(request).execute();
67+
System.out.println("Result code " + response.code());
68+
if (response.body() != null) {
69+
System.out.println(prettyJson(response.body().string()));
70+
}
71+
} catch (IOException e) {
72+
throw new RuntimeException(e);
73+
}
74+
}
75+
76+
private static String prettyJson(String json) {
77+
// https://stackoverflow.com/a/9583835/11996393
78+
return new JSONObject(json).toString(4);
79+
}
80+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var axios = require("axios");
2+
var FormData = require("form-data");
3+
var fs = require("fs");
4+
5+
var upload_data = fs.createReadStream("/path/to/file");
6+
7+
var upload_config = {
8+
method: "post",
9+
maxBodyLength: Infinity,
10+
url: "https://api.pdfrest.com/upload",
11+
headers: {
12+
"Api-Key": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
13+
"Content-Filename": "filename.pdf",
14+
"Content-Type": "application/octet-stream",
15+
},
16+
data: upload_data, // set the data to be sent with the request
17+
};
18+
19+
// send request and handle response or error
20+
axios(upload_config)
21+
.then(function (upload_response) {
22+
console.log(JSON.stringify(upload_response.data));
23+
var uploaded_id = upload_response.data.files[0].id;
24+
25+
const boxOptions = {
26+
boxes: [
27+
{
28+
box: "media",
29+
pages: [
30+
{
31+
range: "1",
32+
left: 100,
33+
top: 100,
34+
bottom: 100,
35+
right: 100,
36+
},
37+
],
38+
},
39+
],
40+
};
41+
42+
var boxes_config = {
43+
method: "post",
44+
maxBodyLength: Infinity,
45+
url: "https://api.pdfrest.com/pdf-with-page-boxes-set",
46+
headers: {
47+
"Api-Key": "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
48+
"Content-Type": "application/json",
49+
},
50+
data: {
51+
id: uploaded_id,
52+
boxes: JSON.stringify(boxOptions),
53+
}, // set the data to be sent with the request
54+
};
55+
56+
// send request and handle response or error
57+
axios(boxes_config)
58+
.then(function (boxes_response) {
59+
console.log(JSON.stringify(boxes_response.data));
60+
})
61+
.catch(function (error) {
62+
console.log(error);
63+
});
64+
})
65+
.catch(function (error) {
66+
console.log(error);
67+
});

0 commit comments

Comments
 (0)