Skip to content

Commit

Permalink
Develop (#65)
Browse files Browse the repository at this point in the history
* fix pdforms upload task response

* Update Sign Elements & Update SignParsingForms sample
  • Loading branch information
teamcrombie authored Oct 10, 2024
1 parent 19784ef commit f7e4842
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 14 deletions.
129 changes: 129 additions & 0 deletions samples/SignParsingForms.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using iLovePdf.Core;
using iLovePdf.Model.Task;
using iLovePdf.Model.TaskParams;
using iLovePdf.Model.TaskParams.Sign.Elements;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Samples
{
[SuppressMessage("ReSharper", "UnusedVariable")]
public class SignParsingForms
{
public async Task DoTask()
{
var api = new LovePdfApi("PUBLIC_KEY", "SECRET_KEY");

//create compress task
var task = api.CreateTask<SignTask>();

//set pdfforms and pdfinfo params so response will return this values
var uploadParams = new SignExtraUploadParams().SetPdfForms().SetPdfInfo();

//file variable contains server file name
var file = task.AddFile("path/to/file/document.pdf", uploadParams);

file.GetPdfFormElement();

var elements = new List<BaseSignElement>();

foreach (var formElement in file.PdfForms)
{
var typeOfField = formElement["typeOfField"].ToString();

if (new[] { "textbox", "signature" }.Contains(typeOfField))
{
var fieldId = formElement["fieldId"].ToString();

var widgets = formElement["widgetsInformation"];
string widgetsString = JsonConvert.SerializeObject(widgets);
List<Dictionary<string, object>> widgetsList = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(widgetsString);

var position = widgetsList[0];
var currentPage = position["page"];

var leftPos = position["left"].ToString();
var topPosition = (ConvertToDouble(position["top"]) - ConvertToDouble(formElement["height"])).ToString();
var size = Math.Floor(ConvertToDouble(position["left"]) - ConvertToDouble(position["bottom"]));

if (typeOfField == "textbox")
{
if ((bool)formElement["multilineFlag"] || (bool)formElement["passwordFlag"])
{
return;
}

var textValue = formElement["textValue"].ToString();

if (fieldId.Contains("_input"))
{
var inputElement = new InputElement
(
currentPage.ToString(), //Pages
new Position(leftPos, topPosition), //Position
(int)size, //Size
textValue //Label
);
elements.Add(inputElement);
}
else
{
var textElement = new TextElement
(
textValue, //TextContent
currentPage.ToString(), //Pages
new Position(leftPos, topPosition), //Position
(int)size //Size
);
elements.Add(textElement);
}
}
else if (typeOfField == "signature")
{
var SignatureField = new SignatureElement
(
currentPage.ToString(),
new Position(leftPos, topPosition),
(int)size
);

elements.Add(SignatureField);
}
}
}

// Create task params
var signParams = new SignParams();

// Create a signer
var signer = signParams.AddSigner("Signer", "[email protected]");

// Add file that a receiver of type signer needs to sign.
var signerFile = signer.AddFile(file.ServerFileName);

// Add signers and their elements;
var signatureElement = signerFile.AddSignature();
signatureElement.Position = new Position("left", "bottom");
signatureElement.Pages = "1";
signatureElement.Size = 40;

// Lastly send the signature request
var signature = await task.RequestSignatureAsync(signParams);

}

public double ConvertToDouble(object keyObject)
{
return Convert.ToDouble(keyObject, CultureInfo.InvariantCulture);
}
}
}

21 changes: 13 additions & 8 deletions src/ILovePDF/Core/UploadTaskResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace iLovePdf.Core
{
Expand Down Expand Up @@ -45,15 +47,18 @@ public void GetPdfFormElement()
foreach (var pdfFormElement in PdfForms)
{
pdfFormElement.TryGetValue("page", out x);
var pdfpageinfo = GetPdfPageInfo((int)x);

int number = Convert.ToInt32(x);

var pdfpageinfo = GetPdfPageInfo(number);
foreach (var page in pdfpageinfo)
{
pdfPageInfo.Add(page.Key, page.Value);
pdfFormElement.Add(page.Key, page.Value);
}
}
}

public Dictionary<string, int> GetPdfPageInfo(int pageNumber)
public Dictionary<string, double> GetPdfPageInfo(int pageNumber)
{
var pdfPages = GetSanitizedPdfPages();
if (pdfPages == null)
Expand All @@ -64,9 +69,9 @@ public Dictionary<string, int> GetPdfPageInfo(int pageNumber)
return pdfPages[pageNumber - 1];
}

public List<Dictionary<string, int>> GetSanitizedPdfPages()
public List<Dictionary<string, double>> GetSanitizedPdfPages()
{
var result = new List<Dictionary<string, int>>();
var result = new List<Dictionary<string, double>>();

if (PdfPages == null)
{
Expand All @@ -77,10 +82,10 @@ public List<Dictionary<string, int>> GetSanitizedPdfPages()
foreach(var pdfPage in PdfPages)
{
var dimensions = pdfPage.Split('x');
int width = int.Parse(dimensions[0]);
int height = int.Parse(dimensions[1]);
double width = Convert.ToDouble(dimensions[0], CultureInfo.InvariantCulture);
double height = Convert.ToDouble(dimensions[1], CultureInfo.InvariantCulture);

result.Add(new Dictionary<string, int>
result.Add(new Dictionary<string, double>
{
{ "width", width },
{ "height", height }
Expand Down
11 changes: 8 additions & 3 deletions src/ILovePDF/Model/TaskParams/Sign/Elements/InputElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace iLovePdf.Model.TaskParams.Sign.Elements
{
public class InputElement : BaseSignElement
{
public InputElement(string pages, Position position, int size = 18)
public InputElement(string pages, Position position, int size = 18, string label = null)
: base(SignElementTypes.Input, pages, position, size)
{
}
{
}

/// <summary>
/// It must be a JSON string where you can define the attributes of that element,
Expand Down Expand Up @@ -40,5 +40,10 @@ public string Info
/// </summary>
[JsonIgnore]
public string Description { set; get; }

public void SetLabel (string label)
{
this.Label = label;
}
}
}
7 changes: 4 additions & 3 deletions src/ILovePDF/Model/TaskParams/Sign/Elements/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public TextElement(string textContent, string pages, Position position, int size
{
if (string.IsNullOrEmpty(textContent))
{
throw new ArgumentNullException(nameof(textContent), "Content can't be null or empty");
Text = "";
//throw new ArgumentNullException(nameof(textContent), "Content can't be null or empty");
}
Text = textContent;

Text = textContent;
}

/// <summary>
Expand Down

0 comments on commit f7e4842

Please sign in to comment.