private void button1_Click(object sender, EventArgs e) { string input = @"..\..\..\..\..\..\Data\FormFieldTemplate.pdf"; //Open pdf document PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(input); //Get the first page PdfPageBase page = pdf.Pages[0]; //As for existing pdf, the property needs to be set as true pdf.AllowCreateForm = true; //Create a new pdf font PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); //Create a pdf brush PdfBrush brush = PdfBrushes.Black; float x = 150; float y = 550; float temX = 0; //Create a pdf radio button list PdfRadioButtonListField radioButton = new PdfRadioButtonListField(page, "RadioButton"); radioButton.Required = true; //Add items into radio button list. for (int i = 0; i < 3; i++) { PdfRadioButtonListItem item = new PdfRadioButtonListItem(string.Format("item{0}", i)); item.BorderWidth = 0.75f; item.Bounds = new RectangleF(x, y, 15, 15); item.BorderColor = Color.Red; item.ForeColor = Color.Red; radioButton.Items.Add(item); temX = x + 20; page.Canvas.DrawString(string.Format("Item{0}", i), font, brush, temX, y); x = temX + 100; } //Add the radio button list field into pdf document pdf.Form.Fields.Add(radioButton); String result = "AddRadioButtonFieldWithOptions_out.pdf"; //Save the document pdf.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
private void button1_Click(object sender, EventArgs e) { string input = @"..\..\..\..\..\..\Data\FormFieldTemplate.pdf"; //Open pdf document PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(input); //Get the first page PdfPageBase page = pdf.Pages[0]; //As for existing pdf, the property needs to be set as true pdf.AllowCreateForm = true; //Create a new pdf font PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); //Create a pdf brush PdfBrush brush = PdfBrushes.Black; float x = 50; float y = 550; float tempX = 0; string text = "RadioButton: "; //Draw a text into page page.Canvas.DrawString(text, font, brush, x, y); tempX = font.MeasureString(text).Width + x + 15; //Create a pdf radio button field PdfRadioButtonListField radioButton = new PdfRadioButtonListField(page, "RadioButton"); radioButton.Required = true; PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(); fieldItem.BorderWidth = 0.75f; fieldItem.Bounds = new RectangleF(tempX, y, 15, 15); radioButton.Items.Add(fieldItem); //Add the radio button field into pdf document pdf.Form.Fields.Add(radioButton); String result = "AddRadioButtonField_out.pdf"; //Save the document pdf.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
private float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex) { float width = page.Canvas.ClientSize.Width; float padding = 2; //measure field label String label = fieldNode.GetAttribute("label", ""); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); float labelMaxWidth = width * 0.4f - 2 * padding; SizeF labelSize = font1.MeasureString(label, labelMaxWidth, format); //measure field height float fieldHeight = MeasureFieldHeight(fieldNode); float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight; height = height + 2; //draw background PdfBrush brush = PdfBrushes.SteelBlue; if (fieldIndex % 2 == 1) { brush = PdfBrushes.LightGreen; } page.Canvas.DrawRectangle(brush, 0, y, width, height); //draw field label PdfBrush brush1 = PdfBrushes.LightYellow; RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height); page.Canvas.DrawString(label, font1, brush1, labelBounds, format); //daw field float fieldMaxWidth = width * 0.57f - 2 * padding; float fieldX = labelBounds.Right + 2 * padding; float fieldY = y + (height - fieldHeight) / 2; String fieldType = fieldNode.GetAttribute("type", ""); String fieldId = fieldNode.GetAttribute("id", ""); bool required = "true" == fieldNode.GetAttribute("required", ""); switch (fieldType) { case "text": case "password": PdfTextBoxField textField = new PdfTextBoxField(page, fieldId); textField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); textField.BorderWidth = 0.75f; textField.BorderStyle = PdfBorderStyle.Solid; textField.Required = required; if ("password" == fieldType) { textField.Password = true; } if ("true" == fieldNode.GetAttribute("multiple", "")) { textField.Multiline = true; textField.Scrollable = true; } form.Fields.Add(textField); break; case "checkbox": PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, fieldId); float checkboxWidth = fieldHeight - 2 * padding; float checkboxHeight = checkboxWidth; checkboxField.Bounds = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight); checkboxField.BorderWidth = 0.75f; checkboxField.Style = PdfCheckBoxStyle.Cross; checkboxField.Required = required; form.Fields.Add(checkboxField); break; case "list": XPathNodeIterator itemNodes = fieldNode.Select("item"); if ("true" == fieldNode.GetAttribute("multiple", "")) { PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId); listBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); listBoxField.BorderWidth = 0.75f; listBoxField.MultiSelect = true; listBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); listBoxField.Required = required; //add items into list box. foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; listBoxField.Items.Add(new PdfListFieldItem(text, text)); } listBoxField.SelectedIndex = 0; form.Fields.Add(listBoxField); break; } if (itemNodes != null && itemNodes.Count <= 3) { PdfRadioButtonListField radioButtonListFile = new PdfRadioButtonListField(page, fieldId); radioButtonListFile.Required = required; //add items into radio button list. float fieldItemHeight = fieldHeight / itemNodes.Count; float radioButtonWidth = fieldItemHeight - 2 * padding; float radioButtonHeight = radioButtonWidth; foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text); fieldItem.BorderWidth = 0.75f; fieldItem.Bounds = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight); radioButtonListFile.Items.Add(fieldItem); float fieldItemLabelX = fieldX + radioButtonWidth + padding; SizeF fieldItemLabelSize = font1.MeasureString(text); float fieldItemLabelY = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2; page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY); fieldY = fieldY + fieldItemHeight; } form.Fields.Add(radioButtonListFile); break; } //combo box PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId); comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); comboBoxField.BorderWidth = 0.75f; comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); comboBoxField.Required = required; //add items into combo box. foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; comboBoxField.Items.Add(new PdfListFieldItem(text, text)); } form.Fields.Add(comboBoxField); break; } if (required) { //draw * float flagX = width * 0.97f + padding; PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); SizeF size = font3.MeasureString("*"); float flagY = y + (height - size.Height) / 2; page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY); } return(y + height); }
private float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex) { float width = page.Canvas.ClientSize.Width; float padding = 2; //measure field label String label = fieldNode.GetAttribute("label", ""); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); float labelMaxWidth = width * 0.4f - 2 * padding; SizeF labelSize = font1.MeasureString(label, labelMaxWidth, format); //measure field height float fieldHeight = MeasureFieldHeight(fieldNode); float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight; height = height + 2; //draw background PdfBrush brush = PdfBrushes.SteelBlue; if (fieldIndex % 2 == 1) { brush = PdfBrushes.LightGreen; } page.Canvas.DrawRectangle(brush, 0, y, width, height); //draw field label PdfBrush brush1 = PdfBrushes.LightYellow; RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height); page.Canvas.DrawString(label, font1, brush1, labelBounds, format); //daw field float fieldMaxWidth = width * 0.57f - 2 * padding; float fieldX = labelBounds.Right + 2 * padding; float fieldY = y + (height - fieldHeight) / 2; String fieldType = fieldNode.GetAttribute("type", ""); String fieldId = fieldNode.GetAttribute("id", ""); bool required = "true" == fieldNode.GetAttribute("required", ""); switch (fieldType) { case "text": case "password": PdfTextBoxField textField = new PdfTextBoxField(page, fieldId); textField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); textField.BorderWidth = 0.75f; textField.BorderStyle = PdfBorderStyle.Solid; textField.Required = required; if ("password" == fieldType) { textField.Password = true; } if ("true" == fieldNode.GetAttribute("multiple", "")) { textField.Multiline = true; textField.Scrollable = true; } form.Fields.Add(textField); break; case "checkbox": PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, fieldId); float checkboxWidth = fieldHeight - 2 * padding; float checkboxHeight = checkboxWidth; checkboxField.Bounds = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight); checkboxField.BorderWidth = 0.75f; checkboxField.Style = PdfCheckBoxStyle.Cross; checkboxField.Required = required; form.Fields.Add(checkboxField); break; case "list": XPathNodeIterator itemNodes = fieldNode.Select("item"); if ("true" == fieldNode.GetAttribute("multiple", "")) { PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId); listBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); listBoxField.BorderWidth = 0.75f; listBoxField.MultiSelect = true; listBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); listBoxField.Required = required; //add items into list box. foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; listBoxField.Items.Add(new PdfListFieldItem(text, text)); } listBoxField.SelectedIndex = 0; form.Fields.Add(listBoxField); break; } if (itemNodes != null && itemNodes.Count <= 3) { PdfRadioButtonListField radioButtonListFile = new PdfRadioButtonListField(page, fieldId); radioButtonListFile.Required = required; //add items into radio button list. float fieldItemHeight = fieldHeight / itemNodes.Count; float radioButtonWidth = fieldItemHeight - 2 * padding; float radioButtonHeight = radioButtonWidth; foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text); fieldItem.BorderWidth = 0.75f; fieldItem.Bounds = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight); radioButtonListFile.Items.Add(fieldItem); float fieldItemLabelX = fieldX + radioButtonWidth + padding; SizeF fieldItemLabelSize = font1.MeasureString(text); float fieldItemLabelY = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2; page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY); fieldY = fieldY + fieldItemHeight; } form.Fields.Add(radioButtonListFile); break; } //combo box PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId); comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); comboBoxField.BorderWidth = 0.75f; comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); comboBoxField.Required = required; //add items into combo box. foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; comboBoxField.Items.Add(new PdfListFieldItem(text, text)); } form.Fields.Add(comboBoxField); break; } if (required) { //draw * float flagX = width * 0.97f + padding; PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); SizeF size = font3.MeasureString("*"); float flagY = y + (height - size.Height) / 2; page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY); } return y + height; }