Skip to content

Commit 0203f61

Browse files
[WIP] Add signed-pdf samples
1 parent b066939 commit 0203f61

10 files changed

Lines changed: 731 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using System.Text;
5+
6+
const string ApiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
7+
using var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") };
8+
string[] filePaths =
9+
{
10+
"/path/to/input.pdf",
11+
"/path/to/credentials.pfx",
12+
"/path/to/passphrase.txt",
13+
"/path/to/logo.png"
14+
};
15+
string[] ids = new string[filePaths.Length];
16+
for (int fileIdx = 0; fileIdx < filePaths.Length; fileIdx++)
17+
{
18+
var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload");
19+
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", ApiKey);
20+
uploadRequest.Headers.Accept.Add(new("application/json"));
21+
22+
var uploadByteArray = File.ReadAllBytes(filePaths[fileIdx]);
23+
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
24+
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
25+
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", Path.GetFileName(filePaths[fileIdx]));
26+
27+
28+
uploadRequest.Content = uploadByteAryContent;
29+
var uploadResponse = await httpClient.SendAsync(uploadRequest);
30+
31+
var uploadResult = await uploadResponse.Content.ReadAsStringAsync();
32+
33+
Console.WriteLine("Upload response received.");
34+
Console.WriteLine(uploadResult);
35+
36+
JObject uploadResultJson = JObject.Parse(uploadResult);
37+
ids[fileIdx] = uploadResultJson["files"][0]["id"].ToString();
38+
}
39+
40+
41+
using var signedPdfRequest = new HttpRequestMessage(HttpMethod.Post, "signed-pdf");
42+
signedPdfRequest.Headers.TryAddWithoutValidation("Api-Key", ApiKey);
43+
signedPdfRequest.Headers.Accept.Add(new("application/json"));
44+
45+
signedPdfRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");
46+
47+
var signatureConfiguration = new JObject
48+
{
49+
["type"] = "new",
50+
["name"] = "esignature",
51+
["logo_opacity"] = "0.5",
52+
["location"] = new JObject
53+
{
54+
["bottom_left"] = new JObject
55+
{
56+
["x"] = "0",
57+
["y"] = "0"
58+
},
59+
["top_right"] = new JObject
60+
{
61+
["x"] = "216",
62+
["y"] = "72"
63+
},
64+
["page"] = "1"
65+
},
66+
["display"] = new JObject
67+
{
68+
["include_distinguished_name"] = "true",
69+
["include_datetime"] = "true",
70+
["contact"] = "My contact information",
71+
["location"] = "My signing location",
72+
["name"] = "John Doe",
73+
["reason"] = "My reason for signing"
74+
}
75+
};
76+
77+
JObject parameterJson = new JObject
78+
{
79+
["id"] = ids[0],
80+
["pfx_credential_id"] = ids[1],
81+
["pfx_passphrase_id"] = ids[2],
82+
["logo_id"] = ids[3],
83+
["signature_configuration"] = signatureConfiguration.ToString(Formatting.None),
84+
};
85+
signedPdfRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json");
86+
var signedPdfResponse = await httpClient.SendAsync(signedPdfRequest);
87+
88+
var signedPdfResult = await signedPdfResponse.Content.ReadAsStringAsync();
89+
90+
Console.WriteLine("Processing response received.");
91+
Console.WriteLine(signedPdfResult);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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, "signed-pdf"))
8+
{
9+
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
10+
request.Headers.Accept.Add(new("application/json"));
11+
var multipartContent = new MultipartFormDataContent();
12+
13+
var inputByteArray = File.ReadAllBytes("/path/to/input.pdf");
14+
var inputByteArrayContent = new ByteArrayContent(inputByteArray);
15+
multipartContent.Add(inputByteArrayContent, "file", "input.pdf");
16+
inputByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
17+
18+
var credByteArray = File.ReadAllBytes("/path/to/credentials.pfx");
19+
var credByteArrayContent = new ByteArrayContent(credByteArray);
20+
multipartContent.Add(credByteArrayContent, "pfx_credential_file", "credentials.pfx");
21+
credByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "application/x-pkcs12");
22+
23+
var passphraseByteArray = File.ReadAllBytes("/path/to/passphrase.txt");
24+
var passphraseByteArrayContent = new ByteArrayContent(passphraseByteArray);
25+
multipartContent.Add(passphraseByteArrayContent, "pfx_passphrase_file", "passphrase.txt");
26+
passphraseByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "text/plain");
27+
28+
var logoByteArray = File.ReadAllBytes("/path/to/logo.jpg");
29+
var logoByteArrayContent = new ByteArrayContent(logoByteArray);
30+
multipartContent.Add(logoByteArrayContent, "logo_file", "logo.jpg");
31+
logoByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "image/jpeg");
32+
33+
var signatureConfiguration = new JObject
34+
{
35+
["type"] = "new",
36+
["name"] = "esignature",
37+
["logo_opacity"] = "0.5",
38+
["location"] = new JObject
39+
{
40+
["bottom_left"] = new JObject
41+
{
42+
["x"] = "0",
43+
["y"] = "0"
44+
},
45+
["top_right"] = new JObject
46+
{
47+
["x"] = "216",
48+
["y"] = "72"
49+
},
50+
["page"] = "1"
51+
},
52+
["display"] = new JObject
53+
{
54+
["include_distinguished_name"] = "true",
55+
["include_datetime"] = "true",
56+
["contact"] = "My contact information",
57+
["location"] = "My signing location",
58+
["name"] = "John Doe",
59+
["reason"] = "My reason for signing"
60+
}
61+
};
62+
63+
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(signatureConfiguration.ToString(Formatting.None)));
64+
multipartContent.Add(byteArrayOption, "signature_configuration");
65+
66+
request.Content = multipartContent;
67+
var response = await httpClient.SendAsync(request);
68+
69+
var apiResult = await response.Content.ReadAsStringAsync();
70+
71+
Console.WriteLine("API response received.");
72+
Console.WriteLine(apiResult);
73+
}
74+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 SignedPDF {
10+
11+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
12+
// You can also put the environment variable in a .env file.
13+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
14+
15+
public static void main(String[] args) {
16+
File inputFile, credentialFile, passphraseFile, logoFile;
17+
if (args.length > 3) {
18+
inputFile = new File(args[0]);
19+
credentialFile = new File(args[1]);
20+
passphraseFile = new File(args[2]);
21+
logoFile = new File(args[3]);
22+
} else {
23+
inputFile = new File("/path/to/input.pdf");
24+
credentialFile = new File("/path/to/credentials.pfx");
25+
passphraseFile = new File("/path/to/passphrase.txt");
26+
logoFile = new File("/path/to/logo.png");
27+
}
28+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
29+
30+
String inputId = uploadFile(inputFile);
31+
String credentialId = uploadFile(credentialFile);
32+
String passphraseId = uploadFile(passphraseFile);
33+
String logoId = uploadFile(logoFile);
34+
35+
JSONObject bottomLeft = new JSONObject();
36+
bottomLeft.put("x", "0");
37+
bottomLeft.put("y", "0");
38+
39+
JSONObject topRight = new JSONObject();
40+
topRight.put("x", "216");
41+
topRight.put("y", "72");
42+
43+
JSONObject location = new JSONObject();
44+
location.put("bottom_left", bottomLeft);
45+
location.put("top_right", topRight);
46+
location.put("page", "1");
47+
48+
JSONObject display = new JSONObject();
49+
display.put("include_distinguished_name", "true");
50+
display.put("include_datetime", "true");
51+
display.put("contact", "My contact information");
52+
display.put("location", "My signing location");
53+
display.put("name", "John Doe");
54+
display.put("reason", "My reason for signing");
55+
56+
JSONObject signatureConfig = new JSONObject();
57+
signatureConfig.put("type", "new");
58+
signatureConfig.put("name", "esignature");
59+
signatureConfig.put("logo_opacity", "0.5");
60+
signatureConfig.put("location", location);
61+
signatureConfig.put("display", display);
62+
63+
JSONObject requestJson = new JSONObject();
64+
requestJson.put("id", inputId);
65+
requestJson.put("pfx_credential_id", credentialId);
66+
requestJson.put("pfx_passphrase_id", passphraseId);
67+
requestJson.put("logo_id", logoId);
68+
requestJson.put("signature_configuration", signatureConfig.toString());
69+
70+
final RequestBody requestBody =
71+
RequestBody.create(requestJson.toString(), MediaType.parse("application/json"));
72+
73+
Request request =
74+
new Request.Builder()
75+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
76+
.url("https://api.pdfrest.com/signed-pdf")
77+
.post(requestBody)
78+
.build();
79+
try {
80+
OkHttpClient client =
81+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
82+
83+
Response response = client.newCall(request).execute();
84+
System.out.println("Processing Result code " + response.code());
85+
if (response.body() != null) {
86+
System.out.println(prettyJson(response.body().string()));
87+
}
88+
} catch (IOException e) {
89+
throw new RuntimeException(e);
90+
}
91+
}
92+
93+
private static String prettyJson(String json) {
94+
// https://stackoverflow.com/a/9583835/11996393
95+
return new JSONObject(json).toString(4);
96+
}
97+
98+
private static String uploadFile(File inputFile) {
99+
100+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
101+
102+
final RequestBody requestBody =
103+
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
104+
105+
Request request =
106+
new Request.Builder()
107+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
108+
.header("Content-Filename", inputFile.getName())
109+
.url("https://api.pdfrest.com/upload")
110+
.post(requestBody)
111+
.build();
112+
try {
113+
OkHttpClient client = new OkHttpClient().newBuilder().build();
114+
Response response = client.newCall(request).execute();
115+
System.out.println("Upload Result code " + response.code());
116+
if (response.body() != null) {
117+
String inputUploadString = response.body().string();
118+
JSONObject inputUploadJSON = new JSONObject(inputUploadString);
119+
if (inputUploadJSON.has("error")) {
120+
System.out.println("Error during upload: " + inputUploadString);
121+
return "";
122+
}
123+
JSONArray fileArray = inputUploadJSON.getJSONArray("files");
124+
125+
JSONObject fileObject = fileArray.getJSONObject(0);
126+
127+
return fileObject.get("id").toString();
128+
}
129+
} catch (IOException e) {
130+
throw new RuntimeException(e);
131+
}
132+
return "";
133+
}
134+
}

0 commit comments

Comments
 (0)