public virtual void ButtonFieldTest01() { String filename = destinationFolder + "buttonFieldTest01.pdf"; PdfWriter writer = new PdfWriter(new FileStream(filename, FileMode.Create)); PdfDocument pdfDoc = new PdfDocument(writer); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); Rectangle rect = new Rectangle(36, 700, 20, 20); Rectangle rect1 = new Rectangle(36, 680, 20, 20); PdfButtonFormField group = PdfFormField.CreateRadioGroup(pdfDoc, "TestGroup", "1"); PdfFormField.CreateRadioButton(pdfDoc, rect, group, "1"); PdfFormField.CreateRadioButton(pdfDoc, rect1, group, "2"); form.AddField(group); PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDoc, new Rectangle(36, 650, 40, 20), "push" , "Capcha"); PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(36, 560, 20, 20), "TestCheck" , "1"); form.AddField(pushButton); form.AddField(checkBox); pdfDoc.Close(); CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(filename, sourceFolder + "cmp_buttonFieldTest01.pdf", destinationFolder , "diff_"); if (errorMessage != null) { NUnit.Framework.Assert.Fail(errorMessage); } }
private void AddCheckBox(PdfDocument pdfDoc, float fontSize, float yPos, float checkBoxW, float checkBoxH) { Rectangle rect = new Rectangle(50, yPos, checkBoxW, checkBoxH); AddCheckBox(pdfDoc, fontSize, yPos, checkBoxW, PdfFormField.CreateCheckBox(pdfDoc, rect, MessageFormatUtil .Format("cb_fs_{0}_{1}_{2}", fontSize, checkBoxW, checkBoxH), "YES", PdfFormField.TYPE_CHECK)); }
public virtual void ManipulatePdf(String src, String dest) { //Initialize PDF document PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); //Add text annotation PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(400, 795, 0, 0)) .SetOpen(true) .SetTitle(new PdfString("iText")) .SetContents("Please, fill out the form."); pdfDoc.GetFirstPage().AddAnnotation(ann); PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage()); canvas.BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12).MoveText(265, 597 ).ShowText("I agree to the terms and conditions.").EndText(); //Add form field PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); PdfButtonFormField checkField = PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(245, 594, 15, 15), "agreement" , "Off", PdfFormField.TYPE_CHECK); checkField.SetRequired(true); form.AddField(checkField); //Update reset button form.GetField("reset").SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1" , "experience2", "experience3", "shift", "info", "agreement" }, 0)); pdfDoc.Close(); }
public virtual void PdfA1aCheckFieldOffAppearanceTest() { String name = "pdfA1a_checkFieldOffAppearance"; String outPath = destinationFolder + name + ".pdf"; String cmpPath = cmpFolder + "cmp_" + name + ".pdf"; String diff = "diff_" + name + "_"; PdfWriter writer = new PdfWriter(outPath); Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read); PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom", "" , "http://www.color.org", "sRGB IEC61966-2.1", @is)); doc.SetTagged(); doc.GetCatalog().SetLang(new PdfString("en-US")); doc.AddNewPage(); PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true); PdfFormField chk = PdfFormField.CreateCheckBox(doc, new Rectangle(100, 500, 50, 50), "name", "Off", PdfFormField .TYPE_CHECK, PdfAConformanceLevel.PDF_A_1A); chk.SetBorderColor(ColorConstants.BLACK); chk.SetBorderWidth(1); form.AddField(chk); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff )); }
public virtual void CheckBoxFontSizeTest04() { String outPdf = destinationFolder + "checkBoxFontSizeTest04.pdf"; String cmpPdf = sourceFolder + "cmp_checkBoxFontSizeTest04.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outPdf)); pdfDoc.AddNewPage(); AddCheckBox(pdfDoc, 0, 730, 10, PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(50, 730, 10, 10), "cb_1" , "YES", PdfFormField.TYPE_CIRCLE)); AddCheckBox(pdfDoc, 0, 700, 10, PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(50, 700, 10, 10), "cb_2" , "YES", PdfFormField.TYPE_CROSS)); AddCheckBox(pdfDoc, 0, 670, 10, PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(50, 670, 10, 10), "cb_3" , "YES", PdfFormField.TYPE_DIAMOND)); AddCheckBox(pdfDoc, 0, 640, 10, PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(50, 640, 10, 10), "cb_4" , "YES", PdfFormField.TYPE_SQUARE)); AddCheckBox(pdfDoc, 0, 610, 10, PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(50, 610, 10, 10), "cb_5" , "YES", PdfFormField.TYPE_STAR)); pdfDoc.Close(); CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(outPdf, cmpPdf, destinationFolder, "diff_"); if (errorMessage != null) { NUnit.Framework.Assert.Fail(errorMessage); } }
public static PdfAcroForm AddAcroForm(Document doc) { Paragraph title = new Paragraph("Application for employment").SetTextAlignment(TextAlignment.CENTER).SetFontSize (16); doc.Add(title); doc.Add(new Paragraph("Full name:").SetFontSize(12)); doc.Add(new Paragraph("Native language: English French German Russian Spanish" ).SetFontSize(12)); doc.Add(new Paragraph("Experience in: cooking driving software development").SetFontSize (12)); doc.Add(new Paragraph("Preferred working shift:").SetFontSize(12)); doc.Add(new Paragraph("Additional information:").SetFontSize(12)); //Add acroform PdfAcroForm form = PdfAcroForm.GetAcroForm(doc.GetPdfDocument(), true); //Create text field PdfTextFormField nameField = PdfTextFormField.CreateText(doc.GetPdfDocument(), new Rectangle(99, 753, 425, 15), "name", ""); form.AddField(nameField); //Create radio buttons PdfButtonFormField group = PdfFormField.CreateRadioGroup(doc.GetPdfDocument(), "language", ""); PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(130, 728, 15, 15), group, "English"); PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(200, 728, 15, 15), group, "French"); PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(260, 728, 15, 15), group, "German"); PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(330, 728, 15, 15), group, "Russian"); PdfFormField.CreateRadioButton(doc.GetPdfDocument(), new Rectangle(400, 728, 15, 15), group, "Spanish"); form.AddField(group); //Create checkboxes for (int i = 0; i < 3; i++) { PdfButtonFormField checkField = PdfFormField.CreateCheckBox(doc.GetPdfDocument(), new Rectangle(119 + i * 69, 701, 15, 15), String.Concat("experience", (i + 1).ToString()), "Off", PdfFormField.TYPE_CHECK); form.AddField(checkField); } //Create combobox String[] options = new String[] { "Any", "6.30 am - 2.30 pm", "1.30 pm - 9.30 pm" }; PdfChoiceFormField choiceField = PdfFormField.CreateComboBox(doc.GetPdfDocument(), new Rectangle(163, 676, 115, 15), "shift", "Any", options); form.AddField(choiceField); //Create multiline text field PdfTextFormField infoField = PdfTextFormField.CreateMultilineText(doc.GetPdfDocument(), new Rectangle(158, 625, 366, 40), "info", ""); form.AddField(infoField); //Create push button field PdfButtonFormField button = PdfFormField.CreatePushButton(doc.GetPdfDocument(), new Rectangle(479, 594, 45 , 15), "reset", "RESET"); button.SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1", "experience2" , "experience3", "shift", "info" }, 0)); form.AddField(button); return(form); }
/* (non-Javadoc) * @see com.itextpdf.html2pdf.attach.impl.layout.form.renderer.AbstractFormFieldRenderer#applyAcroField(com.itextpdf.layout.renderer.DrawContext) */ protected internal override void ApplyAcroField(DrawContext drawContext) { String name = GetModelId(); PdfDocument doc = drawContext.GetDocument(); Rectangle area = flatRenderer.GetOccupiedArea().GetBBox().Clone(); PdfPage page = doc.GetPage(occupiedArea.GetPageNumber()); PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(doc, area, name, IsBoxChecked() ? "Yes" : "Off"); PdfAcroForm.GetAcroForm(doc, true).AddField(checkBox, page); }
public virtual void AddFieldTest() { String outputFile = "addFieldTest.pdf"; PdfDocument outputDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(destinationFolder + outputFile ), new StampingProperties().UseAppendMode()); PdfFormField field = PdfFormField.CreateCheckBox(outputDoc, new Rectangle(10, 10, 24, 24), "checkboxname", "On", PdfFormField.TYPE_CHECK); PdfAcroForm.GetAcroForm(outputDoc, true).AddField(field); outputDoc.Close(); CompareWithCmp(outputFile); }
public virtual void AddFieldTest() { String outputFile = "addFieldTest.pdf"; PdfDocument outputDoc = new PdfDocument(new PdfReader(INPUT_FILE_WITH_TWO_FORM_FIELDS), new PdfWriter(DESTINATION_DIR + outputFile), new StampingProperties().UseAppendMode()); PdfFormField field = PdfFormField.CreateCheckBox(outputDoc, new Rectangle(10, 10, 24, 24), "checkboxname", "On", PdfFormField.TYPE_CHECK); PdfAcroForm.GetAcroForm(outputDoc, true).AddField(field); outputDoc.Close(); CompareWithCmp(outputFile); }
public virtual void FormFieldTaggingTest02() { String outFileName = destinationFolder + "taggedPdfWithForms02.pdf"; String cmpFileName = sourceFolder + "cmp_taggedPdfWithForms02.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName)); pdfDoc.SetTagged(); pdfDoc.InitializeOutlines(); PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDoc, true); acroForm.AddField(PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(36, 560, 20, 20), "TestCheck", "1")); PdfDocument docToCopyFrom = new PdfDocument(new PdfReader(sourceFolder + "cmp_taggedPdfWithForms07.pdf")); docToCopyFrom.CopyPagesTo(1, docToCopyFrom.GetNumberOfPages(), pdfDoc, new PdfPageFormCopier()); pdfDoc.Close(); CompareOutput(outFileName, cmpFileName); }
public virtual void AddFieldToIndirectFieldsArrayTest() { String outputFile = "addFieldToIndirectFieldsArrayTest.pdf"; PdfDocument document = new PdfDocument(new PdfReader(inputFileWithIndirectFieldsArray), new PdfWriter(destinationFolder + outputFile), new StampingProperties().UseAppendMode()); PdfFormField field = PdfFormField.CreateCheckBox(document, new Rectangle(10, 10, 24, 24), "checkboxname", "On", PdfFormField.TYPE_CHECK); // Get an existing acroform and add new field to it PdfAcroForm.GetAcroForm(document, false).AddField(field); document.Close(); CompareWithCmp(outputFile); }
private void AddFormFieldsToDocument(PdfDocument pdfDoc, PdfAcroForm acroForm) { Rectangle rect = new Rectangle(36, 700, 20, 20); Rectangle rect1 = new Rectangle(36, 680, 20, 20); PdfButtonFormField group = PdfFormField.CreateRadioGroup(pdfDoc, "TestGroup", "1"); PdfFormField.CreateRadioButton(pdfDoc, rect, group, "1"); PdfFormField.CreateRadioButton(pdfDoc, rect1, group, "2"); acroForm.AddField(group); PdfButtonFormField pushButton = PdfFormField.CreatePushButton(pdfDoc, new Rectangle(36, 650, 40, 20), "push" , "Capcha"); PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(36, 560, 20, 20), "TestCheck" , "1"); acroForm.AddField(pushButton); acroForm.AddField(checkBox); }
public override void Draw(DrawContext drawContext) { PdfAcroForm form = PdfAcroForm.GetAcroForm(drawContext.GetDocument(), true); // Define the coordinates of the middle float x = (GetOccupiedAreaBBox().GetLeft() + GetOccupiedAreaBBox().GetRight()) / 2; float y = (GetOccupiedAreaBBox().GetTop() + GetOccupiedAreaBBox().GetBottom()) / 2; // Define the position of a check box that measures 20 by 20 Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20); // The 4th parameter is the initial value of checkbox: 'Yes' - checked, 'Off' - unchecked // By default, checkbox value type is cross. PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(drawContext.GetDocument(), rect, name, "Yes"); form.AddField(checkBox); }
public virtual void PdfA1DocWithPdfA1CheckBoxFieldTest() { String name = "pdfA1DocWithPdfA1CheckBoxField"; String fileName = destinationFolder + name + ".pdf"; String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1CheckBoxField.pdf"; Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read); PdfAConformanceLevel conformanceLevel = PdfAConformanceLevel.PDF_A_1B; PdfADocument pdfDoc = new PdfADocument(new PdfWriter(fileName), conformanceLevel, new PdfOutputIntent("Custom" , "", "http://www.color.org", "sRGB IEC61966-2.1", @is)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); form.AddField(PdfFormField.CreateCheckBox(pdfDoc, new Rectangle(36, 726, 20, 20), "checkBox", "1", PdfFormField .TYPE_STAR, conformanceLevel)); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(fileName, cmp, destinationFolder)); NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(fileName)); }
public static void GenerateForm(string filename, IEnumerable <FormField> formFields) { var pdfDocument = new PdfDocument(new PdfWriter(filename)); var document = new Document(pdfDocument); var form = PdfAcroForm.GetAcroForm(pdfDocument, true); var xPosition = 20; var yPosition = 800; var fieldSpacingHeight = 20; foreach (var formField in formFields) { if (formField.FieldType == FormFieldType.TextBox) { var width = 100; var height = 20; var rectangle = new Rectangle(xPosition, yPosition, width, height); var field = PdfFormField.CreateText(document.GetPdfDocument(), rectangle, formField.FieldName, ""); form.AddField(field); yPosition -= height; } if (formField.FieldType == FormFieldType.CheckBox) { var width = 20; var height = 20; var rectangle = new Rectangle(xPosition, yPosition, width, height); var field = PdfFormField.CreateCheckBox(document.GetPdfDocument(), rectangle, formField.FieldName, ""); form.AddField(field); yPosition -= height; } yPosition -= fieldSpacingHeight; } document.Close(); }
public override void Draw(DrawContext drawContext) { Rectangle position = GetOccupiedAreaBBox(); PdfAcroForm form = PdfAcroForm.GetAcroForm(drawContext.GetDocument(), true); // Define the coordinates of the middle float x = (position.GetLeft() + position.GetRight()) / 2; float y = (position.GetTop() + position.GetBottom()) / 2; // Define the position of a check box that measures 20 by 20 Rectangle rect = new Rectangle(x - 10, y - 10, 20, 20); // The 4th parameter is the initial value of checkbox: 'Yes' - checked, 'Off' - unchecked // By default, checkbox value type is cross. PdfButtonFormField checkBox = PdfFormField.CreateCheckBox(drawContext.GetDocument(), rect, this.name, "Yes"); switch (checkboxTypeIndex) { case 0: { checkBox.SetCheckType(PdfFormField.TYPE_CHECK); // Use this method if you changed any field parameters and didn't use setValue checkBox.RegenerateField(); break; } case 1: { checkBox.SetCheckType(PdfFormField.TYPE_CIRCLE); checkBox.RegenerateField(); break; } case 2: { checkBox.SetCheckType(PdfFormField.TYPE_CROSS); checkBox.RegenerateField(); break; } case 3: { checkBox.SetCheckType(PdfFormField.TYPE_DIAMOND); checkBox.RegenerateField(); break; } case 4: { checkBox.SetCheckType(PdfFormField.TYPE_SQUARE); checkBox.RegenerateField(); break; } case 5: { checkBox.SetCheckType(PdfFormField.TYPE_STAR); checkBox.RegenerateField(); break; } } form.AddField(checkBox); }