-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (79 loc) · 4.23 KB
/
Copy pathProgram.cs
File metadata and controls
91 lines (79 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Net.Http;
using System;
using System.IO;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
namespace PDFtoPDFARestAPI
{
class Program
{
static string processManagerPublicUrl = "https://api.accusoft.com";
static string workerFileUrl = "https://api.accusoft.com";
static string processAPI = "/imageGear/api/v1/pdfToPdfaConverters";
static string workerFileAPI = "/PCCIS/V1/WorkFile";
// Create an HTTP Client
// Note: http clients require different base Urls for local testing. Final implementation must use a single client.
private static HttpClient workFileClient = new HttpClient { BaseAddress = new Uri(workerFileUrl) };
private static HttpClient conversionClient = new HttpClient() { BaseAddress = new Uri(processManagerPublicUrl) };
static async Task Main(string[] args)
{
// Set ACCUSOFT_CLOUD_KEY as environment variable or replace in string below.
var apiKey = System.Environment.GetEnvironmentVariable("ACCUSOFT_CLOUD_KEY") ?? "YourAPIKeyHere...";
workFileClient.DefaultRequestHeaders.Add("acs-api-key", apiKey);
conversionClient.DefaultRequestHeaders.Add("acs-api-key", apiKey);
// Upload pdf image
var content = new StreamContent(System.IO.File.OpenRead(@"../../../../../../../Sample Input/1-page-image-only.pdf"));
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("document/pdf");
var workFileResponse = await workFileClient.PostAsync(workerFileAPI + "?FileExtension=pdf", content);
// Extract fileId from the upload response.
string responseBody = await workFileResponse.Content.ReadAsStringAsync();
var responseData = JsonNode.Parse(responseBody)!;
string fileId = responseData["fileId"]!.ToString();
// Exrtact Accusoft Affinity Token from response if it was included.
// See https://help.accusoft.com/PrizmDoc/latest/HTML/affinity-tokens-and-cluster-mode.html?highlight=affinity%2C
// for more information on Affinity Tokens.
JsonNode? affinityToken = responseData["affinityToken"];
if (affinityToken != null)
{
workFileClient.DefaultRequestHeaders.Add("Accusoft-Affinity-Token", affinityToken.ToString());
}
// Build JSON for our process request
var requestJson = new
{
input = new
{
source = new
{
fileId
}
}
};
// Create conversion process
var processRequestContent = new StringContent(JsonSerializer.Serialize(requestJson), Encoding.UTF8, "application/json");
var processRequest = await conversionClient.PostAsync(processAPI, processRequestContent);
string processRequestResponse = await processRequest.Content.ReadAsStringAsync();
string processId = JsonNode.Parse(processRequestResponse)!["processId"]!.ToString();
// Convert PDF to PDF/A
string convertAPI = processAPI + "/" + processId;
string processStatusResponse = await conversionClient.GetStringAsync(convertAPI);
// Wait for the process to complete
while (JsonNode.Parse(processStatusResponse)!["state"]!.ToString() == "processing")
{
Thread.Sleep(1000);
processStatusResponse = await conversionClient.GetStringAsync(convertAPI);
}
// Get converted file ID and download it from the server
var output = JsonNode.Parse(processStatusResponse)!["output"]!;
var outputWorkFileId = output["fileId"]!.ToString();
HttpResponseMessage downloadRequest = await workFileClient.GetAsync(workerFileAPI + "/" + outputWorkFileId);
using (var fileStream = new FileStream(@"../../../../../../../Sample Output/PDFtoPDFARestAPI.pdf", FileMode.Create))
{
await downloadRequest.Content.CopyToAsync(fileStream);
}
}
}
}