Skip to content

Commit af0705b

Browse files
Merge pull request #98 from datalogics-cgreen/pdfcloud-4857-sign
Add `/signed-pdf` samples
2 parents 3367842 + df6b1aa commit af0705b

24 files changed

Lines changed: 1604 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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/certificate.pem",
12+
"/path/to/private_key.pem"
13+
};
14+
string[] ids = new string[filePaths.Length];
15+
for (int fileIdx = 0; fileIdx < filePaths.Length; fileIdx++)
16+
{
17+
var uploadRequest = new HttpRequestMessage(HttpMethod.Post, "upload");
18+
uploadRequest.Headers.TryAddWithoutValidation("Api-Key", ApiKey);
19+
uploadRequest.Headers.Accept.Add(new("application/json"));
20+
21+
var uploadByteArray = File.ReadAllBytes(filePaths[fileIdx]);
22+
var uploadByteAryContent = new ByteArrayContent(uploadByteArray);
23+
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/octet-stream");
24+
uploadByteAryContent.Headers.TryAddWithoutValidation("Content-Filename", Path.GetFileName(filePaths[fileIdx]));
25+
26+
27+
uploadRequest.Content = uploadByteAryContent;
28+
var uploadResponse = await httpClient.SendAsync(uploadRequest);
29+
30+
var uploadResult = await uploadResponse.Content.ReadAsStringAsync();
31+
32+
Console.WriteLine("Upload response received.");
33+
Console.WriteLine(uploadResult);
34+
35+
JObject uploadResultJson = JObject.Parse(uploadResult);
36+
ids[fileIdx] = uploadResultJson["files"][0]["id"].ToString();
37+
}
38+
39+
40+
using var signedPdfRequest = new HttpRequestMessage(HttpMethod.Post, "signed-pdf");
41+
signedPdfRequest.Headers.TryAddWithoutValidation("Api-Key", ApiKey);
42+
signedPdfRequest.Headers.Accept.Add(new("application/json"));
43+
44+
signedPdfRequest.Headers.TryAddWithoutValidation("Content-Type", "application/json");
45+
46+
var signatureConfiguration = new JObject
47+
{
48+
["type"] = "new",
49+
["name"] = "esignature",
50+
["location"] = new JObject
51+
{
52+
["bottom_left"] = new JObject
53+
{
54+
["x"] = "0",
55+
["y"] = "0"
56+
},
57+
["top_right"] = new JObject
58+
{
59+
["x"] = "216",
60+
["y"] = "72"
61+
},
62+
["page"] = "1"
63+
},
64+
["display"] = new JObject
65+
{
66+
["include_datetime"] = "true"
67+
}
68+
};
69+
70+
JObject parameterJson = new JObject
71+
{
72+
["id"] = ids[0],
73+
["certificate_id"] = ids[1],
74+
["private_key_id"] = ids[2],
75+
["signature_configuration"] = signatureConfiguration.ToString(Formatting.None),
76+
};
77+
signedPdfRequest.Content = new StringContent(parameterJson.ToString(), Encoding.UTF8, "application/json");
78+
var signedPdfResponse = await httpClient.SendAsync(signedPdfRequest);
79+
80+
var signedPdfResult = await signedPdfResponse.Content.ReadAsStringAsync();
81+
82+
Console.WriteLine("Processing response received.");
83+
Console.WriteLine(signedPdfResult);
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 certByteArray = File.ReadAllBytes("/path/to/certificate.pem");
19+
var certByteArrayContent = new ByteArrayContent(certByteArray);
20+
multipartContent.Add(certByteArrayContent, "certificate_file", "certificate.pem");
21+
certByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "application/x-pem-file");
22+
23+
var privateKeyByteArray = File.ReadAllBytes("/path/to/private_key.pem");
24+
var privateKeyByteArrayContent = new ByteArrayContent(privateKeyByteArray);
25+
multipartContent.Add(privateKeyByteArrayContent, "private_key_file", "private_key.pem");
26+
privateKeyByteArrayContent.Headers.TryAddWithoutValidation("Content-Type", "application/x-pem-file");
27+
28+
var signatureConfiguration = new JObject
29+
{
30+
["type"] = "new",
31+
["name"] = "esignature",
32+
["location"] = new JObject
33+
{
34+
["bottom_left"] = new JObject
35+
{
36+
["x"] = "0",
37+
["y"] = "0"
38+
},
39+
["top_right"] = new JObject
40+
{
41+
["x"] = "216",
42+
["y"] = "72"
43+
},
44+
["page"] = "1"
45+
},
46+
["display"] = new JObject
47+
{
48+
["include_datetime"] = "true"
49+
}
50+
};
51+
52+
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(signatureConfiguration.ToString(Formatting.None)));
53+
multipartContent.Add(byteArrayOption, "signature_configuration");
54+
55+
request.Content = multipartContent;
56+
var response = await httpClient.SendAsync(request);
57+
58+
var apiResult = await response.Content.ReadAsStringAsync();
59+
60+
Console.WriteLine("API response received.");
61+
Console.WriteLine(apiResult);
62+
}
63+
}
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+
}

0 commit comments

Comments
 (0)