diff --git a/samples/SignParsingForms.cs b/samples/SignParsingForms.cs new file mode 100644 index 0000000..be03d5e --- /dev/null +++ b/samples/SignParsingForms.cs @@ -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(); + + //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(); + + 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> widgetsList = JsonConvert.DeserializeObject>>(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", "signer@email.com"); + + // 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); + } + } +} + diff --git a/src/ILovePDF/Core/UploadTaskResponse.cs b/src/ILovePDF/Core/UploadTaskResponse.cs index 5b7fa52..4f5e8e9 100644 --- a/src/ILovePDF/Core/UploadTaskResponse.cs +++ b/src/ILovePDF/Core/UploadTaskResponse.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Globalization; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace iLovePdf.Core { @@ -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 GetPdfPageInfo(int pageNumber) + public Dictionary GetPdfPageInfo(int pageNumber) { var pdfPages = GetSanitizedPdfPages(); if (pdfPages == null) @@ -64,9 +69,9 @@ public Dictionary GetPdfPageInfo(int pageNumber) return pdfPages[pageNumber - 1]; } - public List> GetSanitizedPdfPages() + public List> GetSanitizedPdfPages() { - var result = new List>(); + var result = new List>(); if (PdfPages == null) { @@ -77,10 +82,10 @@ public List> 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 + result.Add(new Dictionary { { "width", width }, { "height", height } diff --git a/src/ILovePDF/Model/TaskParams/Sign/Elements/InputElement.cs b/src/ILovePDF/Model/TaskParams/Sign/Elements/InputElement.cs index ff936fe..7ef43ca 100644 --- a/src/ILovePDF/Model/TaskParams/Sign/Elements/InputElement.cs +++ b/src/ILovePDF/Model/TaskParams/Sign/Elements/InputElement.cs @@ -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) - { - } + { + } /// /// It must be a JSON string where you can define the attributes of that element, @@ -40,5 +40,10 @@ public string Info /// [JsonIgnore] public string Description { set; get; } + + public void SetLabel (string label) + { + this.Label = label; + } } } diff --git a/src/ILovePDF/Model/TaskParams/Sign/Elements/TextElement.cs b/src/ILovePDF/Model/TaskParams/Sign/Elements/TextElement.cs index ac2177e..a43cee8 100644 --- a/src/ILovePDF/Model/TaskParams/Sign/Elements/TextElement.cs +++ b/src/ILovePDF/Model/TaskParams/Sign/Elements/TextElement.cs @@ -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; } ///