protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA); Rectangle rect = new Rectangle(40, 788, 20, 18); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); // Radio buttons will be added to this radio group PdfButtonFormField radioGroup = PdfFormField.CreateRadioGroup(pdfDoc, "Language", ""); for (int page = 1; page <= LANGUAGES.Length; page++) { pdfDoc.AddNewPage(); // Create a radio button that is added to a radio group. PdfFormField field = PdfFormField.CreateRadioButton(pdfDoc, rect, radioGroup, LANGUAGES[page - 1]); field.SetBorderWidth(1); field.SetBorderColor(ColorConstants.BLACK); // Method specifies on which page the form field's widget must be shown. field.SetPage(page); doc.ShowTextAligned(new Paragraph(LANGUAGES[page - 1]).SetFont(font).SetFontSize(18), 70, 786, page, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0); } form.AddField(radioGroup); doc.Close(); }
public virtual void PdfA1aRadioFieldOffAppearanceTest() { String name = "pdfA1a_radioFieldOffAppearance"; 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); PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1" , @is); PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent); doc.SetTagged(); doc.GetCatalog().SetLang(new PdfString("en-US")); doc.AddNewPage(); PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true); PdfButtonFormField group = PdfFormField.CreateRadioGroup(doc, "group", "1", PdfAConformanceLevel.PDF_A_1B); group.SetReadOnly(true); Rectangle rect1 = new Rectangle(36, 700, 20, 20); Rectangle rect2 = new Rectangle(36, 680, 20, 20); PdfFormField.CreateRadioButton(doc, rect1, group, "1", PdfAConformanceLevel.PDF_A_1B).SetBorderWidth(2).SetBorderColor (ColorConstants.RED).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetVisibility(PdfFormField.VISIBLE); PdfFormField.CreateRadioButton(doc, rect2, group, "2", PdfAConformanceLevel.PDF_A_1B).SetBorderWidth(2).SetBorderColor (ColorConstants.RED).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetVisibility(PdfFormField.VISIBLE); form.AddField(group); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPath, cmpPath, destinationFolder, diff )); }
public override void Draw(DrawContext context) { int pageNumber = GetOccupiedArea().GetPageNumber(); Rectangle bbox = GetInnerAreaBBox(); PdfDocument pdf = context.GetDocument(); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true); PdfFormField chk = PdfFormField.CreateRadioButton(pdf, bbox, _group, _value, PdfAConformanceLevel.PDF_A_1B ); chk.SetPage(pageNumber); chk.SetValue("Off"); chk.RegenerateField(); chk.SetVisibility(PdfFormField.VISIBLE); chk.SetBorderColor(ColorConstants.BLACK); chk.SetBackgroundColor(ColorConstants.WHITE); chk.SetReadOnly(true); PdfFormXObject appearance = new PdfFormXObject(bbox); PdfCanvas canvas = new PdfCanvas(appearance, pdf); canvas.SaveState().MoveTo(bbox.GetLeft(), bbox.GetBottom()).LineTo(bbox.GetRight(), bbox.GetBottom()).LineTo (bbox.GetRight(), bbox.GetTop()).LineTo(bbox.GetLeft(), bbox.GetTop()).LineTo(bbox.GetLeft(), bbox.GetBottom ()).SetLineWidth(1f).Stroke().RestoreState(); form.AddFieldAppearanceToPage(chk, pdf.GetPage(pageNumber)); chk.SetAppearance(PdfName.N, "v1".Equals(_value) ? _value : "Off", appearance.GetPdfObject()); }
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); } }
public virtual void EncryptAes256Pdf2PermissionsTest01() { String filename = "encryptAes256Pdf2PermissionsTest01.pdf"; int permissions = EncryptionConstants.ALLOW_FILL_IN | EncryptionConstants.ALLOW_SCREENREADERS | EncryptionConstants .ALLOW_DEGRADED_PRINTING; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename, new WriterProperties().SetPdfVersion (PdfVersion.PDF_2_0).SetStandardEncryption(USER, OWNER, permissions, EncryptionConstants.ENCRYPTION_AES_256 ))); pdfDoc.GetDocumentInfo().SetMoreInfo(customInfoEntryKey, customInfoEntryValue); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); PdfTextFormField textField1 = PdfFormField.CreateText(pdfDoc, new Rectangle(100, 600, 200, 30), "Name", "Enter your name" ); form.AddField(textField1); PdfTextFormField textField2 = PdfFormField.CreateText(pdfDoc, new Rectangle(100, 550, 200, 30), "Surname", "Enter your surname"); form.AddField(textField2); PdfButtonFormField group = PdfFormField.CreateRadioGroup(pdfDoc, "Sex", "Male"); PdfFormField.CreateRadioButton(pdfDoc, new Rectangle(100, 530, 10, 10), group, "Male"); PdfFormField.CreateRadioButton(pdfDoc, new Rectangle(120, 530, 10, 10), group, "Female"); form.AddField(group); pdfDoc.Close(); CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename , destinationFolder, "diff_", USER, USER); if (errorMessage != null) { NUnit.Framework.Assert.Fail(errorMessage); } }
/* (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) { PdfDocument doc = drawContext.GetDocument(); PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true); Rectangle area = flatRenderer.GetOccupiedArea().GetBBox().Clone(); PdfPage page = doc.GetPage(occupiedArea.GetPageNumber()); String groupName = this.GetProperty <String>(Html2PdfProperty.FORM_FIELD_VALUE); PdfButtonFormField radioGroup = (PdfButtonFormField)form.GetField(groupName); bool addNew = false; if (null == radioGroup) { radioGroup = PdfFormField.CreateRadioGroup(doc, groupName, "on"); addNew = true; } if (IsBoxChecked()) { radioGroup.SetValue(GetModelId()); } PdfFormField radio = PdfFormField.CreateRadioButton(doc, area, radioGroup, GetModelId()); radio.SetCheckType(PdfFormField.TYPE_CIRCLE); if (addNew) { form.AddField(radioGroup, page); } else { form.ReplaceField(GetModelId(), radioGroup); } WriteAcroFormFieldLangAttribute(doc); }
public float AddRadioGroup(ItemRef item, Rectangle rectStart, List <String> labels, int distance) { int fieldFlags = 0; float curY = rectStart.Top; drawingFuncs.Add(() => { PdfFormField group = PdfFormField.CreateRadioButton(writer, true); group.FieldName = item.UniqueId; for (int i = 0; i < labels.Count; i++) { Rectangle rect = new Rectangle(rectStart.Left + i * distance, rectStart.Top, rectStart.Right + i * distance, rectStart.Bottom); RadioCheckField tf = new RadioCheckField(Writer, rect, item.UniqueId + "_" + i.ToString(), i.ToString()) { BackgroundColor = new GrayColor(0.8f), BorderColor = GrayColor.BLACK, CheckType = RadioCheckField.TYPE_CIRCLE, BorderStyle = PdfBorderDictionary.STYLE_SOLID, FontSize = 12 }; group.AddKid(tf.RadioField); } if (item.IsMandatory) { fieldFlags = fieldFlags | PdfFormField.FF_REQUIRED; } group.SetFieldFlags(fieldFlags); stamper.AddAnnotation(group, 1); }); return(curY); }
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); }
public override void Draw(DrawContext drawContext) { PdfDocument document = drawContext.GetDocument(); PdfAcroForm form = PdfAcroForm.GetAcroForm(document, true); // Create a radio button that is added to a radio group. PdfFormField field = PdfFormField.CreateRadioButton(document, GetOccupiedAreaBBox(), radioGroup, value); // This method merges field with its annotation and place it on the given page. // This method won't work if the field has no or more than one widget annotations. form.AddFieldAppearanceToPage(field, document.GetPage(GetOccupiedArea().GetPageNumber())); }
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 void Verify_RadioButtons_CanBeCreated() { var pdfFilePath = TestUtils.GetOutputFileName(); var stream = new FileStream(pdfFilePath, FileMode.Create); // step 1 var document = new Document(); // step 2 var writer = PdfWriter.GetInstance(document, stream); // step 3 document.AddAuthor(TestUtils.Author); document.Open(); // step 4 var cb = writer.DirectContent; var bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); var radiogroup = PdfFormField.CreateRadioButton(writer, true); radiogroup.FieldName = "language"; var rect = new Rectangle(40, 806, 60, 788); for (var page = 0; page < _languages.Length;) { var radio = new RadioCheckField(writer, rect, null, _languages[page]) { BackgroundColor = new GrayColor(0.8f) }; var radiofield = radio.RadioField; radiofield.PlaceInPage = ++page; radiogroup.AddKid(radiofield); } writer.AddAnnotation(radiogroup); foreach (string lang in _languages) { cb.BeginText(); cb.SetFontAndSize(bf, 18); cb.ShowTextAligned(Element.ALIGN_LEFT, lang, 70, 790, 0); cb.EndText(); document.NewPage(); } document.Close(); stream.Dispose(); TestUtils.VerifyPdfFileIsReadable(pdfFilePath); }
public virtual void DefaultRadiobuttonFieldTest() { String file = "defaultRadiobuttonFieldTest.pdf"; String filename = destinationFolder + file; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); Rectangle rect1 = new Rectangle(36, 700, 20, 20); Rectangle rect2 = new Rectangle(36, 680, 20, 20); PdfButtonFormField group = PdfFormField.CreateRadioGroup(pdfDoc, "TestGroup", "1"); PdfFormField.CreateRadioButton(pdfDoc, rect1, group, "1"); PdfFormField.CreateRadioButton(pdfDoc, rect2, group, "2"); form.AddField(group); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, sourceFolder + "cmp_" + file, destinationFolder , "diff_")); }
public virtual void CustomizedRadiobuttonFieldTest() { String file = "customizedRadiobuttonFieldTest.pdf"; String filename = destinationFolder + file; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename)); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); Rectangle rect1 = new Rectangle(36, 700, 20, 20); Rectangle rect2 = new Rectangle(36, 680, 20, 20); PdfButtonFormField group2 = PdfFormField.CreateRadioGroup(pdfDoc, "TestGroup2", "1"); PdfFormField.CreateRadioButton(pdfDoc, rect1, group2, "1").SetBorderWidth(2).SetBorderColor(ColorConstants .RED).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetVisibility(PdfFormField.VISIBLE); PdfFormField.CreateRadioButton(pdfDoc, rect2, group2, "2").SetBorderWidth(2).SetBorderColor(ColorConstants .RED).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetVisibility(PdfFormField.VISIBLE); form.AddField(group2); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, sourceFolder + "cmp_" + file, destinationFolder , "diff_")); }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); BaseFont bf = BaseFont.CreateFont(); PdfContentByte directcontent = writer.DirectContent; directcontent.BeginText(); directcontent.SetFontAndSize(bf, 12); directcontent.ShowTextAligned(Element.ALIGN_LEFT, "Married?", 36, 770, 0); directcontent.ShowTextAligned(Element.ALIGN_LEFT, "YES", 58, 750, 0); directcontent.ShowTextAligned(Element.ALIGN_LEFT, "NO", 102, 750, 0); directcontent.ShowTextAligned( Element.ALIGN_LEFT, "Name partner?", 36, 730, 0 ); directcontent.EndText(); PdfFormField married = PdfFormField.CreateRadioButton(writer, true); married.FieldName = "married"; married.ValueAsName = "yes"; Rectangle rectYes = new Rectangle(40, 766, 56, 744); RadioCheckField yes = new RadioCheckField(writer, rectYes, null, "yes"); yes.Checked = true; married.AddKid(yes.RadioField); Rectangle rectNo = new Rectangle(84, 766, 100, 744); RadioCheckField no = new RadioCheckField(writer, rectNo, null, "no"); no.Checked = false; married.AddKid(no.RadioField); writer.AddAnnotation(married); Rectangle rect = new Rectangle(40, 710, 200, 726); TextField partner = new TextField(writer, rect, "partner"); partner.BorderColor = GrayColor.GRAYBLACK; partner.BorderWidth = 0.5f; writer.AddAnnotation(partner.GetTextField()); } return(ms.ToArray()); } }
public void AddFields() { PdfContentByte cb = writer.DirectContent; Font _bf = new Font(Font.FontFamily.HELVETICA, 9); PdfFormField _radioGroup = PdfFormField.CreateRadioButton(writer, true); _radioGroup.FieldName = "language_gc"; _radioGroup.PlaceInPage = 0; Rectangle _rect; RadioCheckField _radioG; PdfFormField _radioField1; for (int i = 0; i < LANGUAGES_gc.Length; i++) { _rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40); _radioG = new RadioCheckField(writer, _rect, null, LANGUAGES_gc[i]) { BackgroundColor = new GrayColor(0.8f), BorderColor = GrayColor.BLACK, CheckType = RadioCheckField.TYPE_CIRCLE }; _radioField1 = _radioG.RadioField; _radioGroup.AddKid(_radioField1); ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 70, 790 - i * 40, 0); } writer.AddAnnotation(_radioGroup); cb = writer.DirectContent; TextField _text = new TextField(writer, new Rectangle(ConvertX(103), ConvertY(190), ConvertX(182), ConvertY(199)), "g1"); _text.Alignment = Element.ALIGN_CENTER; _text.Options = TextField.MULTILINE; _text.Text = "abc"; PdfFormField textbox = _text.GetTextField(); textbox.PlaceInPage = 1; writer.AddAnnotation(textbox); cb = writer.DirectContentUnder; table.WriteSelectedRows(0, -1, ConvertX(25), ConvertY(133), cb); idTable.WriteSelectedRows(0, -1, ConvertX(25), ConvertY(205), cb); }
public virtual void PdfA1DocWithPdfA1RadioButtonFieldTest() { String name = "pdfA1DocWithPdfA1RadioButtonField"; String fileName = destinationFolder + name + ".pdf"; String cmp = sourceFolder + "cmp/PdfAFormFieldTest/cmp_pdfA1DocWithPdfA1RadioButtonField.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); PdfButtonFormField radioGroup = PdfFormField.CreateRadioGroup(pdfDoc, "radio group", "", conformanceLevel); PdfFormField.CreateRadioButton(pdfDoc, new Rectangle(36, 496, 20, 20), radioGroup, "1", conformanceLevel). SetBorderWidth(2).SetBorderColor(ColorConstants.ORANGE); PdfFormField.CreateRadioButton(pdfDoc, new Rectangle(66, 496, 20, 20), radioGroup, "2", conformanceLevel). SetBorderWidth(2).SetBorderColor(ColorConstants.ORANGE); form.AddField(radioGroup); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(fileName, cmp, destinationFolder)); NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(fileName)); }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfContentByte cb = writer.DirectContent; BaseFont bf = BaseFont.CreateFont( BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED ); PdfFormField radiogroup = PdfFormField.CreateRadioButton( writer, true ); radiogroup.FieldName = "language"; Rectangle rect = new Rectangle(40, 806, 60, 788); RadioCheckField radio; PdfFormField radiofield; for (int page = 0; page < LANGUAGES.Length;) { radio = new RadioCheckField(writer, rect, null, LANGUAGES[page]); radio.BackgroundColor = new GrayColor(0.8f); radiofield = radio.RadioField; radiofield.PlaceInPage = ++page; radiogroup.AddKid(radiofield); } writer.AddAnnotation(radiogroup); for (int i = 0; i < LANGUAGES.Length; i++) { cb.BeginText(); cb.SetFontAndSize(bf, 18); cb.ShowTextAligned(Element.ALIGN_LEFT, LANGUAGES[i], 70, 790, 0); cb.EndText(); document.NewPage(); } } }
public override void Draw(DrawContext drawContext) { // Create a radio button that is added to a radio group. PdfFormField.CreateRadioButton(drawContext.GetDocument(), GetOccupiedAreaBBox(), radioGroup, value); }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); jsString = File.ReadAllText( Path.Combine(Utility.ResourceJavaScript, RESOURCE) ); writer.AddJavaScript(jsString); PdfContentByte canvas = writer.DirectContent; Font font = new Font(Font.FontFamily.HELVETICA, 18); Rectangle rect; PdfFormField field; PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true); radiogroup.FieldName = "language"; RadioCheckField radio; for (int i = 0; i < LANGUAGES.Length; i++) { rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40); radio = new RadioCheckField(writer, rect, null, LANGUAGES[i]); radio.BorderColor = GrayColor.GRAYBLACK; radio.BackgroundColor = GrayColor.GRAYWHITE; radio.CheckType = RadioCheckField.TYPE_CIRCLE; field = radio.RadioField; radiogroup.AddKid(field); ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase(LANGUAGES[i], font), 70, 790 - i * 40, 0 ); } writer.AddAnnotation(radiogroup); PdfAppearance[] onOff = new PdfAppearance[2]; onOff[0] = canvas.CreateAppearance(20, 20); onOff[0].Rectangle(1, 1, 18, 18); onOff[0].Stroke(); onOff[1] = canvas.CreateAppearance(20, 20); onOff[1].SetRGBColorFill(255, 128, 128); onOff[1].Rectangle(1, 1, 18, 18); onOff[1].FillStroke(); onOff[1].MoveTo(1, 1); onOff[1].LineTo(19, 19); onOff[1].MoveTo(1, 19); onOff[1].LineTo(19, 1); onOff[1].Stroke(); RadioCheckField checkbox; for (int i = 0; i < LANGUAGES.Length; i++) { rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40); checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "on"); // checkbox.CheckType = RadioCheckField.TYPE_CHECK; field = checkbox.CheckField; field.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0] ); field.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1] ); writer.AddAnnotation(field); ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0 ); } rect = new Rectangle(300, 806, 370, 788); PushbuttonField button = new PushbuttonField(writer, rect, "Buttons"); button.BackgroundColor = new GrayColor(0.75f); button.BorderColor = GrayColor.GRAYBLACK; button.BorderWidth = 1; button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED; button.TextColor = GrayColor.GRAYBLACK; button.FontSize = 12; button.Text = "Push me"; button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT; button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS; button.ProportionalIcon = true; button.IconHorizontalAdjustment = 0; button.Image = Image.GetInstance( Path.Combine(Utility.ResourceImage, IMAGE) ); field = button.Field; field.Action = PdfAction.JavaScript("this.showButtonState()", writer); writer.AddAnnotation(field); } return(ms.ToArray()); } }
/// <summary> /// /// </summary> /// <param name="metaString"></param> /// <param name="_stream"></param> /// <param name="htmls"></param> /// <param name="_includeSolutions"></param> /// <param name="studentNameAddedToPrint"></param> public static void PDFToStream(string metaString, Stream _stream, IEnumerable <HTML> htmls, bool _includeSolutions, string studentNameAddedToPrint) { int errorCount = 0; errorCount = htmls.Count(h => h.hardErrors.Count > 0); if ((errorCount > 0) && MessageBox.Show("some rows had errors in them and will not show up in the PDF - do you wish to continue?", "", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } using (Document document = new Document()) { using (PdfWriter _writer = PdfWriter.GetInstance(document, _stream)) { List <Action <PdfWriter, Rectangle> > pageActions = new List <Action <PdfWriter, Rectangle> >(); _writer.PageEvent = new AllFormsPageEvent(pageActions); Dictionary <string, PdfFormField> radioGroups = new Dictionary <string, PdfFormField>(); document.Open(); document.AddAuthor(metaString); //MUST HAPPEN AFTER DOC OPEN //optional studentNameAddedToPrint if (!string.IsNullOrWhiteSpace(studentNameAddedToPrint)) { TitleNode title = new TitleNode(studentNameAddedToPrint); document.Add(title.element); } PdfPTable questionsTable = new PdfPTable(2); questionsTable.SplitLate = true; //splits row that is too large questionsTable.SplitRows = false; //allows rows to split string previousTitle = string.Empty; foreach (HTML html in htmls.Where(h => h.hardErrors.Count == 0)) { if (html.title != previousTitle) { PdfPCell titleCell = html.titleCellNode().cell(); titleCell.Border = PdfPCell.NO_BORDER; titleCell.Padding = 10f; questionsTable.AddCell(titleCell); previousTitle = html.title; } foreach (CellNode cell in html.cellNodes) { //add actions for input fields foreach (FieldNode node in cell.descendants.OfType <FieldNode>()) { node.actionId = pageActions.Count.ToString(); if (node is RadioNode) { PdfFormField radioGroup; if (!radioGroups.TryGetValue(node.fieldName, out radioGroup)) { radioGroup = PdfFormField.CreateRadioButton(_writer, false); radioGroup.FieldName = node.fieldName; radioGroups.Add(node.fieldName, radioGroup); } pageActions.Add((node as RadioNode).radioAction(radioGroup)); } else { pageActions.Add(node.action); } } //add cell to document PdfPCell pcell = cell.cell(); //pcell.Border = PdfPCell.NO_BORDER; pcell.Padding = 10f; questionsTable.AddCell(pcell); } } questionsTable.CompleteRow(); document.Add(questionsTable); if (_includeSolutions) { document.Add(Chunk.NEXTPAGE); Paragraph solutionsPara = new Paragraph("Solutions \n"); solutionsPara.Add(string.Join("\n", htmls.OfType <QuestionAssignmentHTML>().Select(h => h.solutionPlainText("\n")))); document.Add(solutionsPara); } document.Close(); } //end of using writer } //end of using document }
public TestCreatePDF() { filenameBlank = filename.Replace(".", "_Blank."); FileStream fs = new System.IO.FileStream(filenameBlank, System.IO.FileMode.Create); writer = PdfWriter.GetInstance(doc, fs); doc.Open(); cb = writer.DirectContent; Rectangle rect = new Rectangle(50, 700, 300, 600) { BorderWidth = .25f, Border = 255 }; cb.Rectangle(rect); cb.Stroke(); //Add text Font font3 = new Font(FontFactory.GetFont(FontFactory.HELVETICA, 1000, Font.NORMAL, BaseColor.BLACK)); Chunk chunk = new Chunk("Roopesh", new Font(FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL, BaseColor.BLUE))); Phrase phrase = new Phrase(chunk); ColumnText ctext = new ColumnText(cb); ctext.SetSimpleColumn(rect.Left + 5, rect.Top, rect.Right, rect.Bottom); ctext.SetText(phrase); ctext.Go(); this.doc.Close(); GetReader(); //Add TextBox rect = new Rectangle(rect.Left, rect.Top - 100, rect.Right, rect.Bottom - 100); TextField tf = new TextField(writer, rect, "text1") { Alignment = Element.ALIGN_LEFT | Element.ALIGN_TOP, BorderColor = BaseColor.BLACK, BorderStyle = PdfBorderDictionary.STYLE_SOLID, Text = "TextField" }; stamper.AddAnnotation(tf.GetTextField(), 1); //Add Radio rect = new Rectangle(rect.Left, rect.Top - 100, rect.Right, rect.Bottom - 25); PdfFormField group = PdfFormField.CreateRadioButton(writer, true); group.FieldName = "grp1"; for (int i = 0; i < 5; i++) { Rectangle radioRect = new Rectangle(rect.Left + i * 25, rect.Top, rect.Left + (i + 1) * 25, rect.Bottom); RadioCheckField radioField = new RadioCheckField(writer, radioRect, "chk" + i.ToString(), i.ToString()) { BorderColor = GrayColor.BLACK, CheckType = RadioCheckField.TYPE_CIRCLE, BorderStyle = PdfBorderDictionary.STYLE_SOLID }; group.AddKid(radioField.RadioField); } //group.SetAdditionalActions(PdfName.E, PdfAction.JavaScript("app.alert(validate);",writer)); stamper.AddAnnotation(group, 1); //Add submit button rect = new Rectangle(rect.Left, rect.Top - 100, rect.Right, rect.Bottom - 25); PushbuttonField button = new PushbuttonField(writer, rect, "postSubmit") { FontSize = 8, BackgroundColor = BaseColor.LIGHT_GRAY, BorderColor = GrayColor.BLACK, BorderWidth = 1f, BorderStyle = PdfBorderDictionary.STYLE_BEVELED, TextColor = GrayColor.GREEN, Text = "Submit", Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT }; PdfFormField field = button.Field; String javascript = "validate();"; field.Action = PdfAction.JavaScript(javascript, writer); //field.Action = PdfAction.CreateSubmitForm( @"*****@*****.**", null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_INCLUDE_NO_VALUE_FIELDS); stamper.AddAnnotation(field, 1); PdfAcroForm f = new PdfAcroForm(writer); //Add common Javascript code writer.AddJavaScript("var requiredFields = ['text1', 'grp1'];"); string validateFunction = "function validate () { " + " for (i=0; i<requiredFields.length; i++) {" + " var grp = this.getField(requiredFields[i]); if (!grp || grp.value === null || grp.value == ''|| grp.value=='Off') { " + " app.alert('please select this '+ requiredFields[i]); return false; }}" + "return true}"; writer.AddJavaScript(validateFunction); //Close all the streams stamper.Close(); reader.Close(); doc.Close(); }
public ValueCreationBlock() { filenameBlank = filename.Replace(".", "_Blank."); FileStream fs = new System.IO.FileStream(filenameBlank, System.IO.FileMode.Create); writer = PdfWriter.GetInstance(doc, fs); doc.Open(); cb = writer.DirectContent; BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false); Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, BaseColor.BLACK); PdfPTable table = new PdfPTable(2); table.TotalWidth = 570f; table.LockedWidth = true; //relative col widths in proportions - 1/3 and 2/3 float[] widths = new float[] { 2f, 3f }; table.SetWidths(widths); table.HorizontalAlignment = Element.ALIGN_CENTER; PdfPCell cell1 = new PdfPCell(new Phrase("Value Creation and Impact", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.WHITE))); cell1.PaddingLeft = 10; cell1.BackgroundColor = PDFColor.BCGGreen; cell1.Colspan = 3; cell1.Border = PdfPCell.NO_BORDER; cell1.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right table.AddCell(cell1); PdfPTable nested = new PdfPTable(1); //nested.TotalWidth = 270f; //nested.LockedWidth = true; //float[] widthforcol = new float[] { 100f, 170f}; //nested.SetWidths(widthforcol); PdfPCell cell2 = new PdfPCell(new Phrase("Develops clear recommendations with an action bias", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.BLACK))); cell2.PaddingLeft = 10; //cell2.Colspan = 2; cell2.BackgroundColor = BaseColor.LIGHT_GRAY; cell2.Border = PdfPCell.NO_BORDER; cell2.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right nested.AddCell(cell2); PdfPCell cell3 = new PdfPCell(new Phrase("Networks within client organization to understand agenda", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.BLACK))); cell3.PaddingLeft = 10; //cell3.Colspan = 2; cell3.BackgroundColor = BaseColor.LIGHT_GRAY; cell3.Border = PdfPCell.NO_BORDER; cell3.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right nested.AddCell(cell3); PdfPCell cell4 = new PdfPCell(new Phrase("Is able to assess implementation challenges", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.BLACK))); cell4.PaddingLeft = 10; //cell4.Colspan = 2; cell4.BackgroundColor = BaseColor.LIGHT_GRAY; cell4.Border = PdfPCell.NO_BORDER; cell4.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right nested.AddCell(cell4); PdfPCell cell5 = new PdfPCell(new Phrase("Applies expertise to generate superior and sustainable results for client; is committed to making change happen", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.BLACK))); cell5.PaddingLeft = 10; //cell5.Colspan = 2; cell5.BackgroundColor = BaseColor.LIGHT_GRAY; cell5.Border = PdfPCell.NO_BORDER; cell5.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right nested.AddCell(cell5); PdfPCell cell6 = new PdfPCell(new Phrase("Effectively transfers capabilities to client teams", new Font(Font.NORMAL, 10f, Font.NORMAL, BaseColor.BLACK))); cell6.PaddingLeft = 10; //cell6.Colspan = 2; cell6.BackgroundColor = BaseColor.LIGHT_GRAY; cell6.Border = PdfPCell.NO_BORDER; cell6.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right nested.AddCell(cell6); //Rectangle rect = new iTextSharp.text.Rectangle(10, 20, 30, 40); //var rf1 = new RadioCheckField(writer, rect, "cellRadioBox", "on"); //rf1.CheckType = RadioCheckField.TYPE_CHECK; //PdfFormField field = rf1.CheckField; string[] labels = { "NA", "1", "2", "3", "4", "5" }; BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); // create a radio field spanning different pages PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true); radiogroup.FieldName = "language"; //Rectangle rect = new Rectangle(40, 606, 60, 588); RadioCheckField radio; PdfFormField radiofield; Rectangle rect; for (int i = 0; i < labels.Length; ++i) { rect = new Rectangle(50 + i * 30, 705, 50 + (i + 1) * 30, 695);//PDFDocPageSize.RIGHT //rect = new Rectangle(40, 606, 60, 588); radio = new RadioCheckField(writer, rect, null, labels[i]); radio.BackgroundColor = new GrayColor(0.8f); radiofield = radio.RadioField; radiofield.PlaceInPage = 1; //radiofield.PlaceInPage= ++page; radiogroup.AddKid(radiofield); } var radioEvents = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, radiogroup); PdfPCell radioCell = new PdfPCell(); radioCell.CellEvent = radioEvents; nested.AddCell(radioCell); PdfPCell nesthousing = new PdfPCell(nested); nesthousing.Padding = 0f; table.AddCell(nesthousing); TextField textfield = new TextField(writer, new iTextSharp.text.Rectangle(10, 20, 30, 40), "cellTextBox"); PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12)); iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, textfield.GetTextField()); tbCell.CellEvent = events; table.AddCell(tbCell); doc.Add(table); this.doc.Close(); GetReader(); //Add common Javascript code writer.AddJavaScript("var requiredFields = ['text1', 'grp1'];"); string validateFunction = "function validate () { " + " for (i=0; i<requiredFields.length; i++) {" + " var grp = this.getField(requiredFields[i]); if (!grp || grp.value === null || grp.value == ''|| grp.value=='Off') { " + " app.alert('please select this '+ requiredFields[i]); return false; }}" + "return true}"; writer.AddJavaScript(validateFunction); //Close all the streams stamper.Close(); reader.Close(); doc.Close(); }