Skip to content

Commit

Permalink
Merge pull request #61 from datalogics-tsmith/complex-solution-flows
Browse files Browse the repository at this point in the history
Add complex solution flows for all languages
  • Loading branch information
datalogics-cgreen authored Dec 14, 2023
2 parents 47597be + e37fa2c commit 5eaa0c5
Show file tree
Hide file tree
Showing 30 changed files with 2,202 additions and 1 deletion.
99 changes: 99 additions & 0 deletions DotNET/Complex Flow Examples/decrypt-add-reencrypt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Newtonsoft.Json.Linq;
using System.Text;

/* In this sample, we will show how to take an encrypted file and decrypt, modify
* and re-encrypt it to create an encryption-at-rest solution as discussed in
* https://pdfrest.com/solutions/create-secure-document-workflows-with-pdf-password-protection/
* We will be running the document through /decrypted-pdf to open the document
* to modification, running the decrypted result through /pdf-with-added-image,
* and then sending the output with the new image through /encrypted-pdf to
* lock it up again.
*/

var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
// Begin decryption
using var decryptRequest = new HttpRequestMessage(HttpMethod.Post, "decrypted-pdf");

decryptRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
decryptRequest.Headers.Accept.Add(new("application/json"));
var decryptMultipartContent = new MultipartFormDataContent();

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

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password"));
decryptMultipartContent.Add(byteArrayOption, "current_open_password");


decryptRequest.Content = decryptMultipartContent;
var decryptResponse = await httpClient.SendAsync(decryptRequest);

var decryptResult = await decryptResponse.Content.ReadAsStringAsync();

Console.WriteLine("Decrypt response received.");
Console.WriteLine(decryptResult);

dynamic decryptJson = JObject.Parse(decryptResult);
string decryptID = decryptJson.outputId;

// Begin add image
using var addImageRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-added-image");

addImageRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
addImageRequest.Headers.Accept.Add(new("application/json"));
var addImageMultipartContent = new MultipartFormDataContent();

var addImageId = new ByteArrayContent(Encoding.UTF8.GetBytes(decryptID));
addImageMultipartContent.Add(addImageId, "id");

var addImageImage = File.ReadAllBytes("/path/to/file.png");
var imageContent = new ByteArrayContent(addImageImage);
addImageMultipartContent.Add(imageContent, "image_file", "file_name.png");
imageContent.Headers.TryAddWithoutValidation("Content-Type", "image/png");

var addImagePage = new ByteArrayContent(Encoding.UTF8.GetBytes("1"));
addImageMultipartContent.Add(addImagePage, "page");

var addImageX = new ByteArrayContent(Encoding.UTF8.GetBytes("0"));
addImageMultipartContent.Add(addImageX, "x");
var addImageY = new ByteArrayContent(Encoding.UTF8.GetBytes("0"));
addImageMultipartContent.Add(addImageY, "y");

addImageRequest.Content = addImageMultipartContent;
var addImageResponse = await httpClient.SendAsync(addImageRequest);

using var addImageResult = await addImageResponse.Content.ReadAsStringAsync();

Console.WriteLine("Add image response received.");
Console.WriteLine(addImageResult);

dynamic addImageJson = JObject.Parse(addImageResult);
string addImageID = addImageJson.outputId;

// Begin re-encryption
var encryptRequest = new HttpRequestMessage(HttpMethod.Post, "encrypted-pdf");

encryptRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
encryptRequest.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();

var encryptID = new ByteArrayContent(Encoding.UTF8.GetBytes(addImageID));
multipartContent.Add(encryptID, "id");

var encryptPassword = new ByteArrayContent(Encoding.UTF8.GetBytes("password"));
multipartContent.Add(encryptPassword, "new_open_password");


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

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

Console.WriteLine("Encrypt response received.");
Console.WriteLine(apiResult);
}
96 changes: 96 additions & 0 deletions DotNET/Complex Flow Examples/merge-different-file-types.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Newtonsoft.Json.Linq;
using System.Text;

/* In this sample, we will show how to merge different file types together as
* discussed in https://pdfrest.com/solutions/merge-multiple-types-of-files-together/.
First, we will upload an image file to the /pdf route and capture the output ID.
* Next, we will upload a PowerPoint file to the /pdf route and capture its output
* ID. Finally, we will pass both IDs to the /merged-pdf route to combine both inputs
* into a single PDF.
*
* Note that there is nothing special about an image and a PowerPoint file, and
* this sample could be easily used to convert and combine any two file types
* that the /pdf route takes as inputs.
*/

var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
// Begin first PDF conversion
using var imageRequest = new HttpRequestMessage(HttpMethod.Post, "pdf");

imageRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
imageRequest.Headers.Accept.Add(new("application/json"));
var imageMultipartContent = new MultipartFormDataContent();

var imageByteArray = File.ReadAllBytes("/path/to/file.png");
var imageByteAryContent = new ByteArrayContent(imageByteArray);
imageMultipartContent.Add(imageByteAryContent, "file", "file.png");
imageByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "image/x-png");

imageRequest.Content = imageMultipartContent;
var imageResponse = await httpClient.SendAsync(imageRequest);

var imageResult = await imageResponse.Content.ReadAsStringAsync();
Console.WriteLine("Image to PDF response received.");
Console.WriteLine(imageResult);

dynamic imageResponseData = JObject.Parse(imageResult);
string imageID = imageResponseData.outputId;

// Begin second PDF conversion
using var powerpointRequest = new HttpRequestMessage(HttpMethod.Post, "pdf");

powerpointRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
powerpointRequest.Headers.Accept.Add(new("application/json"));
var powerpointMultipartContent = new MultipartFormDataContent();

var powerpointByteArray = File.ReadAllBytes("/path/to/file.ppt");
var powerpointByteAryContent = new ByteArrayContent(powerpointByteArray);
powerpointMultipartContent.Add(powerpointByteAryContent, "file", "file.ppt");
powerpointByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.ms-powerpoint");

powerpointRequest.Content = powerpointMultipartContent;
var powerpointResponse = await httpClient.SendAsync(powerpointRequest);

var powerpointResult = await powerpointResponse.Content.ReadAsStringAsync();
Console.WriteLine("powerpoint to PDF response received.");
Console.WriteLine(powerpointResult);

dynamic powerpointResponseData = JObject.Parse(powerpointResult);
string powerpointID = powerpointResponseData.outputId;

// Begin file merge
using var request = new HttpRequestMessage(HttpMethod.Post, "merged-pdf");

request.Headers.TryAddWithoutValidation("Api-Key", apiKey);
request.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();


var imageByteArrayID = new ByteArrayContent(Encoding.UTF8.GetBytes(imageID));
multipartContent.Add(imageByteArrayID, "id[]");

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("id"));
multipartContent.Add(byteArrayOption, "type[]");
var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("all"));
multipartContent.Add(byteArrayOption2, "pages[]");


var powerpointByteArrayID = new ByteArrayContent(Encoding.UTF8.GetBytes(powerpointID));
multipartContent.Add(powerpointByteArrayID, "id[]");

var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("id"));
multipartContent.Add(byteArrayOption3, "type[]");
var byteArrayOption4 = new ByteArrayContent(Encoding.UTF8.GetBytes("all"));
multipartContent.Add(byteArrayOption4, "pages[]");

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

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

Console.WriteLine("Merge response received.");
Console.WriteLine(apiResult);
}
67 changes: 67 additions & 0 deletions DotNET/Complex Flow Examples/pdfa-3b-with-attachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Newtonsoft.Json.Linq;
using System.Text;

/* In this sample, we will show how to attach an xml document to a PDF file and then
* convert the file with the attachment to conform to the PDF/A standard, which
* can be useful for invoicing and standards compliance. We will be running the
* input document through /pdf-with-added-attachment to add the attachment and
* then /pdfa to do the PDF/A conversion.
* Note that there is nothing special about attaching an xml file, and any appropriate
* file may be attached and wrapped into the PDF/A conversion.
*/

var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
// Begin file attachment
using var attachRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-added-attachment");

attachRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
attachRequest.Headers.Accept.Add(new("application/json"));
var attachMultipartContent = new MultipartFormDataContent();

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

var byteArray2 = File.ReadAllBytes("/path/to/file.xml");
var byteAryContent2 = new ByteArrayContent(byteArray2);
attachMultipartContent.Add(byteAryContent2, "file_to_attach", "file_name.xml");
byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/xml");

attachRequest.Content = attachMultipartContent;
var attachResponse = await httpClient.SendAsync(attachRequest);

var attachResult = await attachResponse.Content.ReadAsStringAsync();
Console.WriteLine("Attachement response received.");
Console.WriteLine(attachResult);

dynamic responseData = JObject.Parse(attachResult);
string attachementID = responseData.outputId;

// Begin PDF/A conversion
using var pdfaRequest = new HttpRequestMessage(HttpMethod.Post, "pdfa");

pdfaRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
pdfaRequest.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();


var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(attachementID));
multipartContent.Add(byteArrayOption, "id");

var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("PDF/A-3b"));
multipartContent.Add(byteArrayOption2, "output_type");


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

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

Console.WriteLine("PDF/A response received.");
Console.WriteLine(apiResult);
}
62 changes: 62 additions & 0 deletions DotNET/Complex Flow Examples/preserve-word-document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Newtonsoft.Json.Linq;
using System.Text;

/* In this sample, we will show how to optimize a Word file for long-term preservation
* as discussed in https://pdfrest.com/solutions/optimize-word-excel-and-powerpoint-files-for-long-term-preservation/
* We will take our Word (or Excel or PowerPoint) document and first convert it to
* a PDF with a call to the /pdf route. Then, we will take that converted PDF
* and convert it to the PDF/A format for long-term storage.
*/

var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{

// Begin PDF conversion
using var pdfRequest = new HttpRequestMessage(HttpMethod.Post, "pdf");

pdfRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
pdfRequest.Headers.Accept.Add(new("application/json"));
var pdfMultipartContent = new MultipartFormDataContent();

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

pdfRequest.Content = pdfMultipartContent;
var pdfResponse = await httpClient.SendAsync(pdfRequest);

var pdfResult = await pdfResponse.Content.ReadAsStringAsync();
Console.WriteLine("PDF response received.");
Console.WriteLine(pdfResult);

dynamic responseData = JObject.Parse(pdfResult);
string pdfID = responseData.outputId;

// Begin PDF/A conversion
using var pdfaRequest = new HttpRequestMessage(HttpMethod.Post, "pdfa");

pdfaRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
pdfaRequest.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();


var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(pdfID));
multipartContent.Add(byteArrayOption, "id");

var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("PDF/A-3b"));
multipartContent.Add(byteArrayOption2, "output_type");


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

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

Console.WriteLine("PDF/A response received.");
Console.WriteLine(apiResult);


}
69 changes: 69 additions & 0 deletions DotNET/Complex Flow Examples/protected-watermark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Text;

/* In this sample, we will show how to watermark a PDF document and then restrict
* editing on the document so that the watermark cannot be removed, as discussed in
* https://pdfrest.com/solutions/add-pdf-watermarks-that-cannot-be-removed/.
* We will be running the input file through /watermarked-pdf to apply the watermark
* and then /restricted-pdf to lock the watermark in.
*/

var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
// Begin watermarking
using var watermarkRequest = new HttpRequestMessage(HttpMethod.Post, "watermarked-pdf");

watermarkRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
watermarkRequest.Headers.Accept.Add(new("application/json"));
var watermarkMultipartContent = new MultipartFormDataContent();

var watermarkByteArray = File.ReadAllBytes("/path/to/file.pdf");
var watermarkByteAryContent = new ByteArrayContent(watermarkByteArray);
watermarkMultipartContent.Add(watermarkByteAryContent, "file", "file.pdf");
watermarkByteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

var watermarkByteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("Watermarked"));
watermarkMultipartContent.Add(watermarkByteArrayOption, "watermark_text");

watermarkRequest.Content = watermarkMultipartContent;
var watermarkResponse = await httpClient.SendAsync(watermarkRequest);

var watermarkResult = await watermarkResponse.Content.ReadAsStringAsync();
Console.WriteLine("Watermark to PDF response received.");
Console.WriteLine(watermarkResult);

dynamic watermarkResponseData = JObject.Parse(watermarkResult);
string watermarkID = watermarkResponseData.outputId;

// Begin restricting
using var restrictRequest = new HttpRequestMessage(HttpMethod.Post, "restricted-pdf");

restrictRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
restrictRequest.Headers.Accept.Add(new("application/json"));
var restrictMultipartContent = new MultipartFormDataContent();

var watermarkIDByteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes(watermarkID));
restrictMultipartContent.Add(watermarkIDByteArrayOption, "id");

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password"));
restrictMultipartContent.Add(byteArrayOption, "new_permissions_password");

var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("copy_content"));
restrictMultipartContent.Add(byteArrayOption2, "restrictions[]");
var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("edit_content"));
restrictMultipartContent.Add(byteArrayOption3, "restrictions[]");
var byteArrayOption4 = new ByteArrayContent(Encoding.UTF8.GetBytes("edit_annotations"));
restrictMultipartContent.Add(byteArrayOption3, "restrictions[]");


restrictRequest.Content = restrictMultipartContent;
var restrictResponse = await httpClient.SendAsync(restrictRequest);

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

Console.WriteLine("Restrict response received.");
Console.WriteLine(apiResult);
}
Loading

0 comments on commit 5eaa0c5

Please sign in to comment.