public virtual void FormFieldTaggingTest04() { String outFileName = destinationFolder + "taggedPdfWithForms04.pdf"; String cmpFileName = sourceFolder + "cmp_taggedPdfWithForms04.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "cmp_taggedPdfWithForms01.pdf"), new PdfWriter (outFileName)); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDoc, false); acroForm.RemoveField("TestCheck"); acroForm.RemoveField("push"); pdfDoc.Close(); CompareOutput(outFileName, cmpFileName); }
public virtual void FormFieldTaggingTest06() { String outFileName = destinationFolder + "taggedPdfWithForms06.pdf"; String cmpFileName = sourceFolder + "cmp_taggedPdfWithForms06.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName)); pdfDoc.SetTagged(); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); AddFormFieldsToDocument(pdfDoc, form); form.RemoveField("TestCheck"); form.RemoveField("push"); pdfDoc.Close(); CompareOutput(outFileName, cmpFileName); }
private static byte[] fillFormSF3101(SF3101 rec, string templatePdfFile) { using (var memoryStream = new MemoryStream()) { PdfReader reader = new PdfReader(templatePdfFile); //Iput PdfWriter writer = new PdfWriter(memoryStream); //output PdfDocument pdfDoc = new PdfDocument(reader, writer); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); var fields = form.GetFormFields(); form.RemoveField("SAVE"); //remove save button form.RemoveField("PRINT"); //remove print button form.RemoveField("RESET"); //remove reset button //form.GetField("Reason").SetVisibility(3); //Since we control the template, we can directly set all the fields fields["SSN"].SetValue(rec.SSN); fields["Name"].SetValue(rec.Name); fields["DOB"].SetValue(rec.DOB); fields["Agency"].SetValue(rec.Agency); fields["Payroll Office No"].SetValue(rec.PayrollOfficeNo); if (!String.IsNullOrEmpty(rec.Location)) { fields["Location"].SetValue(rec.Location); } if (!String.IsNullOrEmpty(rec.Reason)) { fields["Reason"].SetValue(rec.Reason); } fields["Data on SF 3100"].SetValue(rec.DataOnSF3100); fields["Total culmulative"].SetValue(rec.TotalCulmulative); fields["Corrected Data"].SetValue(rec.CorrectedData); fields["Overstatement"].SetValue(rec.Overstatement); fields["Total culmulative deductions"].SetValue(rec.TotalCulmulativeDeductions); fields["SF 3100 Data 2"].SetValue(rec.SF3100Data2); fields["SF 3100 Corrected Data 2"].SetValue(rec.SF3100CorrectedData2); fields["SF 3100 Data"].SetValue(rec.SF3100Data); fields["SF 3100 Corrected Data"].SetValue(rec.SF3100CorrectedData); fields["SF 3100 Corrected Data 3"].SetValue(rec.SF3100CorrectedData3); fields["SF 2812 Number"].SetValue(rec.SF2812Number); fields["Register"].SetValue(rec.Register); fields["SF2812 - dated"].SetValue(rec.SF2812Dated); fields["SF 3100 Data 3"].SetValue(rec.SF3100Data3); fields["Title"].SetValue(rec.Title); fields["Telephone Number"].SetValue(rec.TelephoneNumber); fields["Register - dated"].SetValue(rec.RegisterDated); fields["Date signed"].SetValue(rec.DateSigned); form.FlattenFields(); pdfDoc.Close(); return(memoryStream.ToArray()); } }
private static void AddSignature(PdfAcroForm form, PdfAssociation itempdf) { PdfArray position = form.GetField(itempdf.PDF_FIELD).GetWidgets()[0].GetRectangle(); rectangle = position.ToRectangle(); numPage = itempdf.NUM_PAGE; if (!string.IsNullOrEmpty(itempdf.BIOMETRIC)) { form.RemoveField(itempdf.PDF_FIELD); } }
public void FillImageInAcroForm <T>(T detail) { IDictionary <String, PdfFormField> fields = _form.GetFormFields(); Type type = detail.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { PdfFormField toSet; PdfActionAttribute required = property.GetCustomAttribute <PdfActionAttribute>(); if (required != null && (required.Action.Equals(ActionFlag.Ignore) || required.Action.Equals(ActionFlag.MultiLines))) { continue; } if (fields.TryGetValue(property.Name, out toSet)) { var value = property.GetValue(detail, null); PdfFormField field = fields[property.Name]; if (!string.IsNullOrWhiteSpace(value?.ToString())) { var widgets = field.GetWidgets(); if (widgets == null || widgets.Count == 0) { throw new ArgumentNullException($"no widgets to the field"); } PdfArray position = widgets.First().GetRectangle(); PdfPage page = field.GetWidgets().First().GetPage(); if (page == null) { throw new ArgumentNullException( $"field widget annotation is not associated with any page"); } int pageNum = _pdfDoc.GetPageNumber(page); float width = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue()); float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue()); Image image = new Image(ImageDataFactory.Create(File.ReadAllBytes(value.ToString()))); image.ScaleToFit(width, height); float startX = (float)position.GetAsNumber(0).GetValue(); float startY = (float)position.GetAsNumber(1).GetValue(); PdfFontAttribute pdfFont = property.GetCustomAttribute <PdfFontAttribute>(); if (pdfFont != null) { ImagePositionAdjustification(pdfFont.Justification, width, height, image.GetImageScaledWidth(), image.GetImageScaledHeight(), (float)position.GetAsNumber(0).GetValue(), (float)position.GetAsNumber(1).GetValue(), out startX, out startY); } image.SetFixedPosition(pageNum, startX, startY); _form.RemoveField(field.GetFieldName().ToString()); _doc.Add(image); } else { if (required != null) { if (required.Action.Equals(ActionFlag.Required)) { throw new ArgumentNullException($"No {property.Name} image found"); } if (required.Action.Equals(ActionFlag.Optional)) { _form.RemoveField(field.GetFieldName().ToString()); continue; } if (required.Action.Equals(ActionFlag.NotAvailable)) { toSet.SetValue("N/A").SetFontSizeAutoScale().SetReadOnly(true); } } } } } }