public void CreateFieldsUsecase_GivenValidFields_ShouldExcutePopulateAndReadOnly() { //Arrange var response = new PdfFieldsOperationsResponse { Output = new byte[512] }; var templateByte = new byte[362]; var inputData = new List <PdfFields> { new PdfFields { FieldName = "Surname", FieldValue = "Tembe" } }; var fieldsSubstitute = Substitute.For <IPdfOperations>(); fieldsSubstitute.PopulateFieldValues(Arg.Any <byte[]>(), Arg.Any <List <PdfFields> >()).Returns(response); fieldsSubstitute.MarkFieldsReadOnly(Arg.Any <byte[]>(), Arg.Any <List <PdfFields> >()).Returns(response); var sut = new PopulatePdfUseCase(fieldsSubstitute); //Act var actual = sut.Execute(templateByte, inputData); //Assert fieldsSubstitute.Received().PopulateFieldValues(Arg.Any <byte[]>(), Arg.Any <List <PdfFields> >()); fieldsSubstitute.Received().MarkFieldsReadOnly(Arg.Any <byte[]>(), Arg.Any <List <PdfFields> >()); }
public PdfFieldsOperationsResponse MarkFieldsReadOnly(byte[] template, List <PdfFields> listOfFields) { var results = new PdfFieldsOperationsResponse(); var documentBytes = new byte[0]; using (var incomingStream = new MemoryStream(template)) { var pdfDocument = new Document(incomingStream); foreach (var field in listOfFields) { var fieldName = field.FieldName; bool fieldNotFound = FieldNotFound(pdfDocument, fieldName); if (fieldNotFound) { return(FieldErrorMessage(documentBytes, results, fieldName)); } pdfDocument.Form[fieldName].ReadOnly = true; } documentBytes = Save(pdfDocument); } results.ErrorMessage = string.Empty; results.Output = documentBytes; return(results); }
private static AttachBarCodeRequest Map(Barcode barcode, PdfFieldsOperationsResponse fieldsOperator, CreateBarCodeResponse barCodeCreator, IConfigurationProvider config) { var mapper = config.CreateMapper(); var attachBarCodeRequest = mapper.Map <Barcode, AttachBarCodeRequest>(barcode); attachBarCodeRequest.FileBytes = fieldsOperator.Output; attachBarCodeRequest.QrCodeBytes = barCodeCreator.BarCode; return(attachBarCodeRequest); }
public PdfFieldsOperationsResponse PopulateFieldValues(byte[] template, List <PdfFields> listOfFields) { var documentBytes = new byte[0]; var results = new PdfFieldsOperationsResponse(); using (var incomingStream = new MemoryStream(template)) { var pdfDocument = new Document(incomingStream); foreach (var field in listOfFields) { var fieldName = field.FieldName; var fieldValue = field.FieldValue; bool fieldNotFound = FieldNotFound(pdfDocument, fieldName); if (fieldNotFound) { return(FieldErrorMessage(documentBytes, results, fieldName)); } var textBoxField = pdfDocument.Form[fieldName] as TextBoxField; if (textBoxField != null) { var maxLength = (pdfDocument.Form[fieldName] as TextBoxField).MaxLen; var exceededLength = fieldValue.Length - maxLength; if (ValueExceededMaxLength(fieldValue, maxLength) && HasMaximumLength(maxLength)) { results.Output = documentBytes; results.ErrorMessage = $"{fieldName} has exceeded its maximum value by {exceededLength}"; return(results); } textBoxField.Value = fieldValue; continue; } var checkboxField = pdfDocument.Form[fieldName] as CheckboxField; checkboxField.Checked = true; } documentBytes = Save(pdfDocument); } results.ErrorMessage = string.Empty; results.Output = documentBytes; return(results); }
public PdfFieldsOperationsResponse Execute(byte[] templateByte, List <PdfFields> fieldsToPopulate) { var populatedFieldBytes = _fieldsOperations.PopulateFieldValues(templateByte, fieldsToPopulate); if (populatedFieldBytes.HadError()) { return(populatedFieldBytes); } var readOnlyBytes = _fieldsOperations.MarkFieldsReadOnly(populatedFieldBytes.Output, fieldsToPopulate); var result = new PdfFieldsOperationsResponse { Output = readOnlyBytes.Output }; return(result); }
private static PdfFieldsOperationsResponse FieldErrorMessage(byte[] documentBytes, PdfFieldsOperationsResponse results, string fieldName) { results.Output = documentBytes; results.ErrorMessage = $"{fieldName} cannot be found"; return(results); }