public IActionResult CreatePdfa([FromForm] PdfFormData formData) { if (formData.File.Length > _options.MaxFileSize) { return(new FileSizeExceededError(_options.MaxFileSize)); } using (var stream = formData.File.OpenReadStream()) { var output = new MemoryStream(); var intent = new PdfaColorProfile().ToPdfOutputIntent(_env.ContentRootPath); var conformance = formData.ToPdfAConformanceLevel(); var reader = new PdfReader(stream); var writer = new PdfWriter(output); using (var pdf = new PdfDocument(reader)) using (var pdfa = new PdfADocument(writer, conformance, intent)) { var pages = new PdfaPages(pdfa); var metadata = new PdfaMetadata(pdfa); pages.Copy(pdf); metadata.CopyCustomProperties(pdf); metadata.AddOrReplaceCustomProperties(formData.CustomPropertiesAsDictionary()); metadata.AddOrReplaceTitle(formData.Title); metadata.AddOrReplaceAuthor(formData.Author); metadata.AddOrReplaceCreationDate(formData.CreationDate); new Document(pdfa).Close(); return(File(output.ToArray(), MediaTypeNames.Application.Pdf, $"{formData.File.Name}.pdf")); } } }
public static PdfFormData AddCustomProperty(this PdfFormData model, string name, string value) { model.CustomProperties = model.CustomProperties ?? new List <CustomPropertyModel>(); model.CustomProperties.Add(new CustomPropertyModel { Name = name, Value = value }); return(model); }
public async Task ShouldRespond400ForRequiredFields() { var path = $"/pdfa"; var formData = new PdfFormData().Upload(new byte[0]); var response = await _client.PostAsync(path, formData); var responseContent = await response.Content.ReadAsStringAsync(); var responseJson = JsonConvert.DeserializeObject <BadRequestError>(responseContent); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Contains(responseJson.Errors, error => error == "The ConformanceLevel field is required."); }
public async Task ShouldRespond400ForSizeExceeded() { var path = $"/pdfa"; var size10MB = 10485760; var pdfFile = new byte[size10MB]; var formData = new PdfFormData().WithConformanceLevel("PDF_A_2B").Upload(pdfFile); var response = await _client.PostAsync(path, formData); var responseContent = await response.Content.ReadAsStringAsync(); var responseJson = JsonConvert.DeserializeObject <BadRequestError>(responseContent); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Contains(responseJson.Errors, error => error == "File must be up to 5242880 bytes."); }
public static MultipartFormDataContent Upload(this PdfFormData model, byte[] bytes) { var file = new ByteArrayContent(bytes); file.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Pdf); var formData = new MultipartFormDataContent(); formData.Add(file, nameof(PdfFormData.File), "dummy.pdf"); if (model.ConformanceLevel != null) { formData.Add(new StringContent(model.ConformanceLevel), nameof(PdfFormData.ConformanceLevel)); } if (model.Title != null) { formData.Add(new StringContent(model.Title), nameof(PdfFormData.Title)); } if (model.Author != null) { formData.Add(new StringContent(model.Author), nameof(PdfFormData.Author)); } if (model.CreationDate != null) { var dateTimeIso8601 = model.CreationDate.Value.ToString("o"); formData.Add(new StringContent(dateTimeIso8601), nameof(PdfFormData.CreationDate)); } if (model.CustomProperties != null) { for (var i = 0; i < model.CustomProperties.Count; i++) { var customProperty = model.CustomProperties.ElementAt(i); var customProperyName = new StringContent(customProperty.Name); var customProperyValue = new StringContent(customProperty.Value); formData.Add(customProperyName, $"{nameof(PdfFormData.CustomProperties)}[{i}][Name]"); formData.Add(customProperyValue, $"{nameof(PdfFormData.CustomProperties)}[{i}][Value]"); } } return(formData); }
private void btnFillFormData_Click(object sender, EventArgs e) { #region #FillFields // Load a document with an interactive form. using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { documentProcessor.LoadDocument(filePath + fileName + ".pdf"); // Obtain interactive form data from a document. PdfFormData formData = documentProcessor.GetFormData(); // Specify the value for FirstName and LastName text boxes. formData["FirstName"].Value = "Janet"; formData["LastName"].Value = "Leverling"; // Specify the value for the Gender radio group. formData["Gender"].Value = "Female"; // Specify the check box checked appearance name. formData["Check"].Value = "Yes"; // Specify values for the Category list box. formData["Category"].Value = new string[] { "Entertainment", "Meals", "Morale" }; // Obtain data from the Address form field and specify values for Address child form fields. PdfFormData address = formData["Address"]; // Specify the value for the Country combo box. address["Country"].Value = "United States"; // Specify the value for City and Address text boxes. address["City"].Value = "California"; address["Address"].Value = "20 Maple Avenue"; // Apply data to the interactive form. documentProcessor.ApplyFormData(formData); // Save the modified document. documentProcessor.SaveDocument(filePath + fileName + "_new.pdf"); btnFillFormData.Enabled = false; btnLoadFilledPDF.Enabled = true; } #endregion #FillFields }
private void btnGetFieldNames_Click(object sender, EventArgs e) { #region #GetFields // Load a document with an interactive form. using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { documentProcessor.LoadDocument(filePath + fileName + ".pdf"); // Get names of interactive form fields. PdfFormData formData = documentProcessor.GetFormData(); IList <string> names = formData.GetFieldNames(); // Show the field names in the rich text box. string[] strings = new string[names.Count]; names.CopyTo(strings, 0); richTextBox1.Lines = strings; } #endregion #GetFields }
public async Task ShouldConvertToPdfA2B(string level, string part, string conformance) { var path = $"/pdfa"; var fixture = $"{Program.TestProjectPath}/Fixtures/dummy.pdf"; var pdfFile = await File.ReadAllBytesAsync(fixture); var formData = new PdfFormData().WithConformanceLevel(level).Upload(pdfFile); var response = await _client.PostAsync(path, formData); var stream = await response.Content.ReadAsStreamAsync(); var pdfa = new PdfADocument(new PdfReader(stream), new PdfWriter(new MemoryStream())); var xmpMetadataBytes = pdfa.GetXmpMetadata(); var xmpMetadata = XMPMetaFactory.ParseFromBuffer(xmpMetadataBytes); var conformanceLevel = PdfAConformanceLevel.GetConformanceLevel(xmpMetadata); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(part, conformanceLevel.GetPart()); Assert.Equal(conformance, conformanceLevel.GetConformance()); }
public async Task ShouldReplaceAuthor() { var path = $"/pdfa"; var author = Guid.NewGuid().ToString("N"); var fixture = $"{Program.TestProjectPath}/Fixtures/dummy.pdf"; var pdfFile = await File.ReadAllBytesAsync(fixture); var formData = new PdfFormData() .WithConformanceLevel("PDF_A_2B") .WithAuthor(author) .Upload(pdfFile); var response = await _client.PostAsync(path, formData); var stream = await response.Content.ReadAsStreamAsync(); var pdfa = new PdfADocument(new PdfReader(stream), new PdfWriter(new MemoryStream())); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(author, pdfa.GetDocumentInfo().GetAuthor()); }
static void Main(string[] args) { // Load a document with an interactive form. PdfDocumentProcessor processor = new PdfDocumentProcessor(); processor.LoadDocument(@"DocumentToFill.pdf"); // Obtain interactive form data from a document. PdfFormData formData = processor.GetFormData(); // Obtain the radio group checked appearance names. List <string> radioGroupCheckedValues = GetRadioGroupCheckedAppearanceNames(processor, "Gender"); // Specify a checked appearance name for the Female radio button. formData["Gender"].Value = radioGroupCheckedValues[0]; // Apply data to the interactive form. processor.ApplyFormData(formData); // Save the modified document. processor.SaveDocument("..\\..\\Result.pdf"); }
public async Task ShouldReplaceCreationDate() { var path = $"/pdfa"; var creationDate = new DateTime(1980, 10, 10); var fixture = $"{Program.TestProjectPath}/Fixtures/dummy.pdf"; var pdfFile = await File.ReadAllBytesAsync(fixture); var formData = new PdfFormData() .WithConformanceLevel("PDF_A_2B") .WithCreationDate(creationDate) .Upload(pdfFile); var response = await _client.PostAsync(path, formData); var stream = await response.Content.ReadAsStreamAsync(); var pdfa = new PdfADocument(new PdfReader(stream), new PdfWriter(new MemoryStream())); var creationDateProperty = PdfName.CreationDate.GetValue(); var pdfaCreationDate = pdfa.GetDocumentInfo().GetMoreInfo(creationDateProperty); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(creationDate, Convert.ToDateTime(pdfaCreationDate)); }
public async Task ShouldAddCustomProperties() { var path = $"/pdfa"; var prop1 = Guid.NewGuid().ToString("N"); var prop2 = Guid.NewGuid().ToString("N"); var fixture = $"{Program.TestProjectPath}/Fixtures/dummy.pdf"; var pdfFile = await File.ReadAllBytesAsync(fixture); var formData = new PdfFormData().WithConformanceLevel("PDF_A_2B") .AddCustomProperty(nameof(prop1), prop1) .AddCustomProperty(nameof(prop2), prop2) .Upload(pdfFile); var response = await _client.PostAsync(path, formData); var stream = await response.Content.ReadAsStreamAsync(); var pdfa = new PdfADocument(new PdfReader(stream), new PdfWriter(new MemoryStream())); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(prop1, pdfa.GetDocumentInfo().GetMoreInfo(nameof(prop1))); Assert.Equal(prop2, pdfa.GetDocumentInfo().GetMoreInfo(nameof(prop2))); }
public static PdfFormData WithCreationDate(this PdfFormData model, DateTime creationDate) { model.CreationDate = creationDate; return(model); }
public static PdfFormData WithAuthor(this PdfFormData model, string author) { model.Author = author; return(model); }
public static PdfFormData WithTitle(this PdfFormData model, string title) { model.Title = title; return(model); }
public static PdfFormData WithConformanceLevel(this PdfFormData model, string conformanceLevel) { model.ConformanceLevel = conformanceLevel; return(model); }