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; //Draw a text into page string text1 = "Enter a number, such as 12345: "; //Draw a text into page page.Canvas.DrawString(text1, font, brush, x, y); //Add a textBox field tempX = font.MeasureString(text1).Width + x + 15; PdfTextBoxField textbox = new PdfTextBoxField(page, "Number-TextBox"); textbox.Bounds = new RectangleF(tempX, y, 100, 15); textbox.BorderWidth = 0.75f; textbox.BorderStyle = PdfBorderStyle.Solid; //Add a JavaScript action to be performed when uses type a keystroke into a text field string js = PdfJavaScript.GetNumberKeystrokeString(2, 0, 0, 0, "$", true); PdfJavaScriptAction jsAction = new PdfJavaScriptAction(js); textbox.Actions.KeyPressed = jsAction; //Add a JavaScript action to format the value of text field js = PdfJavaScript.GetNumberFormatString(2, 0, 0, 0, "$", true); jsAction = new PdfJavaScriptAction(js); textbox.Actions.Format = jsAction; pdf.Form.Fields.Add(textbox); //Save and launch the result file string output = "AddJavaScriptAction_out.pdf"; //Save to file pdf.SaveToFile(output); //Launch the Pdf file PDFDocumentViewer(output); }
private void button1_Click(object sender, EventArgs e) { //Pdf file path string input = @"..\..\..\..\..\..\Data\AddTooltipForFormField.pdf"; //Open pdf document PdfDocument doc = new PdfDocument(); doc.LoadFromFile(input); //Get the first page PdfPageBase page = doc.Pages[0]; //As for existing pdf, the property needs to be set as true doc.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 = 590; float tempX = 0; string text = "E-mail: "; //Draw a text into page page.Canvas.DrawString(text, font, brush, x, y); tempX = font.MeasureString(text).Width + x + 15; //Create a pdf textbox field PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox"); //Set the bounds of textbox field textbox.Bounds = new RectangleF(tempX, y, 100, 15); //Set the border width of textbox field textbox.BorderWidth = 0.75f; //Set the border style of textbox field textbox.BorderStyle = PdfBorderStyle.Solid; //Add the textbox field into pdf document doc.Form.Fields.Add(textbox); //Add a tooltip for the textbox field doc.Form.Fields["TextBox"].ToolTip = "Please insert a valid email address"; string output = "AddTooltipForFormField.pdf"; //Save pdf document doc.SaveToFile(output); //Launch the Pdf file PDFDocumentViewer(output); }
public void createTextBox(string fieldName, int koordinateXChange, int koordianteYChange, int textboxWidth) { PdfTextBoxField textField = new PdfTextBoxField(page, fieldName); textField.Bounds = new Rectangle(koordinateXChange, koordianteYChange, textboxWidth, textboxHeight); textField.BorderWidth = 0f; //textField.BorderStyle = PdfBorderStyle.; pdf.Form.Fields.Add(textField); }
void table_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) { if (args.CellIndex == 2 && args.RowIndex > -1) { CreateTextBox(interactivePage, "price" + args.RowIndex.ToString(), "Price", args.Bounds, true, args.Value); args.Skip = true; } else if (args.CellIndex == 3 && args.RowIndex == -1) { PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(new RectangleF(args.Bounds.Right - 18, args.Bounds.Top + 2, 1, 1), "Please enter a validate interger between 1 to 50"); popupAnnotation.Border.Width = 4; popupAnnotation.Open = false; popupAnnotation.Border.HorizontalRadius = 10; popupAnnotation.Border.VerticalRadius = 10; popupAnnotation.Icon = PdfPopupIcon.Comment; interactivePage.Annotations.Add(popupAnnotation); } else if (args.CellIndex == 3 && args.RowIndex > -1) { PdfTextBoxField textBoxField = new PdfTextBoxField(interactivePage, "quantity" + args.RowIndex.ToString()); //Set properties to the textbox. textBoxField.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);; textBoxField.BorderColor = new PdfColor(white); textBoxField.BackColor = Syncfusion.Drawing.Color.FromArgb(255, 238, 238, 248); textBoxField.Bounds = args.Bounds; textBoxField.Text = "0"; PdfJavaScriptAction action = new PdfJavaScriptAction(@"event.rc = event.value > -1 && event.value < 51; var f = this.getField('price" + args.RowIndex.ToString() + @"') var f1 = this.getField('quantity" + args.RowIndex.ToString() + @"') var f2 = this.getField('TotalPrice" + args.RowIndex.ToString() + @"') var f3 = this.getField('GrandTotal'); if(!event.rc) { f1.fillColor=color.red; app.beep(); } else { f1.fillColor = color.transparent; f2.value = f1.value * f.value; f3.value = this.getField('TotalPrice0').value + this.getField('TotalPrice1').value + this.getField('TotalPrice2').value + this.getField('TotalPrice3').value + this.getField('TotalPrice4').value +this.getField('TotalPrice5').value; }"); textBoxField.Actions.LostFocus = action; document.Form.Fields.Add(textBoxField); } else if (args.CellIndex == 4 && args.RowIndex > -1) { CreateTextBox(interactivePage, "TotalPrice" + args.RowIndex.ToString(), "Total Price", args.Bounds, true, "0"); } }
private void button1_Click(object sender, EventArgs e) { //create a pdf document PdfDocument doc = new PdfDocument(); //add a new page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(0)); //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 = 50; float tempX = 0; string text = "E-mail: "; //draw a text into page page.Canvas.DrawString(text, font, brush, x, y); tempX = font.MeasureString(text).Width + x + 15; //create a pdf textbox field PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox"); //set the bounds of textbox field textbox.Bounds = new RectangleF(tempX, y, 100, 15); //set the border width of textbox field textbox.BorderWidth = 0.75f; //set the border style of textbox field textbox.BorderStyle = PdfBorderStyle.Solid; //add the textbox field into pdf document doc.Form.Fields.Add(textbox); //add a tooltip for the textbox field doc.Form.Fields["TextBox"].ToolTip = "Please insert a valid email address"; string output = "AddTooltipForFormField.pdf"; //save pdf document doc.SaveToFile(output); //Launching the Pdf file PDFDocumentViewer(output); }
/// <summary> /// Creates textbox and adds it in the form. /// </summary> /// <param name="page"></param> /// <param name="text"></param> /// <param name="tooltip"></param> /// <param name="f"></param> /// <param name="bounds"></param> private void CreateTextBox(PdfPage page, string text, string tooltip, PdfFont f, RectangleF bounds) { // Create a Text box field. PdfTextBoxField textBoxField = new PdfTextBoxField(page, text); //Set properties to the textbox. textBoxField.Font = f; textBoxField.BorderColor = new PdfColor(System.Drawing.Color.FromArgb(255, 128, 128, 128)); textBoxField.BorderStyle = PdfBorderStyle.Beveled; textBoxField.Bounds = bounds; textBoxField.ToolTip = tooltip; document.Form.Fields.Add(textBoxField); }
private void CreateInputField(PdfPage questionlistPage, PdfLayoutResult afterResult, PdfDocument document) { PdfTextBoxField field = new PdfTextBoxField(questionlistPage, "input") { Text = "Schrijf hier uw advies.", Location = new PointF(6, afterResult.Bounds.Bottom + 40), Size = new SizeF(500, 16), Font = timesRoman, ForeColor = new PdfColor(System.Drawing.Color.White), BackColor = new PdfColor(126, 155, 203) }; document.Form.Fields.Add(field); }
/// <summary> /// Creates textbox and adds it in the form. /// </summary> /// <param name="page"></param> /// <param name="text"></param> /// <param name="tooltip"></param> /// <param name="f"></param> /// <param name="bounds"></param> private void CreateTextBox(PdfPage page, string text, string tooltip, RectangleF bounds, bool readOnly, string value) { // Create a Text box field. PdfTextBoxField textBoxField = new PdfTextBoxField(page, text); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); //Set properties to the textbox. textBoxField.Font = font; textBoxField.BackColor = Syncfusion.Drawing.Color.FromArgb(255, 238, 238, 248); textBoxField.BorderColor = new PdfColor(255,255,255); textBoxField.Bounds = bounds; textBoxField.ToolTip = tooltip; textBoxField.ReadOnly = readOnly; textBoxField.Text = value; document.Form.Fields.Add(textBoxField); }
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 = "TexBox: "; //Draw a text into page page.Canvas.DrawString(text, font, brush, x, y); //Add a textBox field tempX = font.MeasureString(text).Width + x + 15; PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox"); textbox.Bounds = new RectangleF(tempX, y, 100, 15); textbox.BorderWidth = 0.75f; textbox.BorderStyle = PdfBorderStyle.Solid; pdf.Form.Fields.Add(textbox); String result = "AddTextBoxField_out.pdf"; //Save the document pdf.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
public ActionResult InteractiveFeatures(string InsideBrowser) { #region Field Definitions document = new PdfDocument(); document.PageSettings.Margins.All = 0; document.PageSettings.Size = new SizeF(PdfPageSize.A4.Width, 600); interactivePage = document.Pages.Add(); PdfGraphics g = interactivePage.Graphics; RectangleF rect = new RectangleF(0, 0, interactivePage.Graphics.ClientSize.Width, 100); PdfBrush whiteBrush = new PdfSolidBrush(white); PdfPen whitePen = new PdfPen(white, 5); PdfBrush purpleBrush = new PdfSolidBrush(new PdfColor(255, 158, 0, 160)); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 25); Syncfusion.Drawing.Color maroonColor = Color.FromArgb(255, 188, 32, 60); Syncfusion.Drawing.Color orangeColor = Color.FromArgb(255, 255, 167, 73); #endregion #region Header g.DrawRectangle(purpleBrush, rect); g.DrawPie(whitePen, whiteBrush, new RectangleF(-20, 35, 700, 200), 20, -180); g.DrawRectangle(whiteBrush, new RectangleF(0, 99.5f, 700, 200)); g.DrawString("Invoice", new PdfStandardFont(PdfFontFamily.TimesRoman, 24), PdfBrushes.White, new PointF(500, 10)); string basePath = _hostingEnvironment.WebRootPath; string dataPath = string.Empty; dataPath = basePath + @"/PDF/"; //Read the file FileStream file = new FileStream(dataPath + "AdventureCycle.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); g.DrawImage(PdfImage.FromStream(file), new RectangleF(100, 70, 390, 130)); #endregion #region Body //Invoice Number Random invoiceNumber = new Random(); g.DrawString("Invoice No: " + invoiceNumber.Next().ToString(), new PdfStandardFont(PdfFontFamily.Helvetica, 14), new PdfSolidBrush(maroonColor), new PointF(50, 210)); g.DrawString("Date: ", new PdfStandardFont(PdfFontFamily.Helvetica, 14), new PdfSolidBrush(maroonColor), new PointF(350, 210)); //Current Date PdfTextBoxField textBoxField = new PdfTextBoxField(interactivePage, "date"); textBoxField.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); textBoxField.Bounds = new RectangleF(384, 204, 150, 30); textBoxField.ForeColor = new PdfColor(maroonColor); textBoxField.ReadOnly = true; document.Actions.AfterOpen = new PdfJavaScriptAction(@"var newdate = new Date(); var thisfieldis = this.getField('date'); var theday = util.printd('dddd',newdate); var thedate = util.printd('d',newdate); var themonth = util.printd('mmmm',newdate); var theyear = util.printd('yyyy',newdate); thisfieldis.strokeColor=color.transparent; thisfieldis.value = theday + ' ' + thedate + ', ' + themonth + ' ' + theyear ;"); document.Form.Fields.Add(textBoxField); //invoice table PdfLightTable table = new PdfLightTable(); table.Style.ShowHeader = true; g.DrawRectangle(new PdfSolidBrush(Syncfusion.Drawing.Color.FromArgb(238, 238, 238, 248)), new RectangleF(50, 240, 500, 140)); //Header Style PdfCellStyle headerStyle = new PdfCellStyle(); headerStyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold); headerStyle.TextBrush = whiteBrush; headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); headerStyle.BackgroundBrush = new PdfSolidBrush(orangeColor); headerStyle.BorderPen = new PdfPen(whiteBrush, 0); table.Style.HeaderStyle = headerStyle; //Cell Style PdfCellStyle bodyStyle = new PdfCellStyle(); bodyStyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); bodyStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left); bodyStyle.BorderPen = new PdfPen(whiteBrush, 0); table.Style.DefaultStyle = bodyStyle; table.DataSource = GetProductReport(_hostingEnvironment.WebRootPath); table.Columns[0].Width = 90; table.Columns[1].Width = 160; table.Columns[3].Width = 100; table.Columns[4].Width = 65; table.Style.CellPadding = 3; table.BeginCellLayout += table_BeginCellLayout; PdfLightTableLayoutResult result = table.Draw(interactivePage, new RectangleF(50, 240, 500, 140)); g.DrawString("Grand Total:", new PdfStandardFont(PdfFontFamily.Helvetica, 12), new PdfSolidBrush(Syncfusion.Drawing.Color.FromArgb(255, 255, 167, 73)), new PointF(result.Bounds.Right - 150, result.Bounds.Bottom)); CreateTextBox(interactivePage, "GrandTotal", "Grand Total", new RectangleF(result.Bounds.Width - 15, result.Bounds.Bottom - 2, 66, 18), true, ""); //Send to Server PdfButtonField sendButton = new PdfButtonField(interactivePage, "OrderOnline"); sendButton.Bounds = new RectangleF(200, result.Bounds.Bottom + 70, 80, 25); sendButton.BorderColor = white; sendButton.BackColor = maroonColor; sendButton.ForeColor = white; sendButton.Text = "Order Online"; PdfSubmitAction submitAction = new PdfSubmitAction("http://stevex.net/dump.php"); submitAction.DataFormat = SubmitDataFormat.Html; sendButton.Actions.MouseUp = submitAction; document.Form.Fields.Add(sendButton); //Order by Mail PdfButtonField sendMail = new PdfButtonField(interactivePage, "sendMail"); sendMail.Bounds = new RectangleF(300, result.Bounds.Bottom + 70, 80, 25); sendMail.Text = "Order By Mail"; sendMail.BorderColor = white; sendMail.BackColor = maroonColor; sendMail.ForeColor = white; // Create a javascript action. PdfJavaScriptAction javaAction = new PdfJavaScriptAction("address = app.response(\"Enter an e-mail address.\",\"SEND E-MAIL\",\"\");" + "var aSubmitFields = [];" + "for( var i = 0 ; i < this.numFields; i++){" + "aSubmitFields[i] = this.getNthFieldName(i);" + "}" + "if (address){ cmdLine = \"mailto:\" + address;this.submitForm(cmdLine,true,false,aSubmitFields);}"); sendMail.Actions.MouseUp = javaAction; document.Form.Fields.Add(sendMail); //Print PdfButtonField printButton = new PdfButtonField(interactivePage, "print"); printButton.Bounds = new RectangleF(400, result.Bounds.Bottom + 70, 80, 25); printButton.BorderColor = white; printButton.BackColor = maroonColor; printButton.ForeColor = white; printButton.Text = "Print"; printButton.Actions.MouseUp = new PdfJavaScriptAction("this.print (true); "); document.Form.Fields.Add(printButton); file = new FileStream(dataPath + "Product Catalog.pdf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); PdfAttachment attachment = new PdfAttachment("Product Catalog.pdf", file); attachment.ModificationDate = DateTime.Now; attachment.Description = "Specification"; document.Attachments.Add(attachment); //Open Specification PdfButtonField openSpecificationButton = new PdfButtonField(interactivePage, "openSpecification"); openSpecificationButton.Bounds = new RectangleF(50, result.Bounds.Bottom + 20, 87, 15); openSpecificationButton.TextAlignment = PdfTextAlignment.Left; openSpecificationButton.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); openSpecificationButton.BorderStyle = PdfBorderStyle.Underline; openSpecificationButton.BorderColor = orangeColor; openSpecificationButton.BackColor = new PdfColor(255, 255, 255); openSpecificationButton.ForeColor = orangeColor; openSpecificationButton.Text = "Open Specification"; openSpecificationButton.Actions.MouseUp = new PdfJavaScriptAction("this.exportDataObject({ cName: 'Product Catalog.pdf', nLaunch: 2 });"); document.Form.Fields.Add(openSpecificationButton); RectangleF uriAnnotationRectangle = new RectangleF(interactivePage.Graphics.ClientSize.Width - 160, interactivePage.Graphics.ClientSize.Height - 30, 80, 20); PdfTextWebLink linkAnnot = new PdfTextWebLink(); linkAnnot.Url = "http://www.adventure-works.com"; linkAnnot.Text = "http://www.adventure-works.com"; linkAnnot.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 8); linkAnnot.Brush = PdfBrushes.White; linkAnnot.DrawTextWebLink(interactivePage, uriAnnotationRectangle.Location); #endregion #region Footer g.DrawRectangle(purpleBrush, new RectangleF(0, interactivePage.Graphics.ClientSize.Height - 100, interactivePage.Graphics.ClientSize.Width, 100)); g.DrawPie(whitePen, whiteBrush, new RectangleF(-20, interactivePage.Graphics.ClientSize.Height - 250, 700, 200), 0, 180); #endregion //Save the PDF to the MemoryStream MemoryStream ms = new MemoryStream(); document.Save(ms); //If the position is not set to '0' then the PDF will be empty. ms.Position = 0; //Close the PDF document. document.Close(true); //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "Interactive features.pdf"; return(fileStreamResult); }
/// <summary> /// Create a simple PDF document /// </summary> /// <returns>Return the created PDF document as stream</returns> public MemoryStream JobApplicationPDF() { //Create a new PDF document PdfDocument pdfDoc = new PdfDocument(); pdfDoc.ViewerPreferences.HideMenubar = true; pdfDoc.ViewerPreferences.HideWindowUI = true; pdfDoc.ViewerPreferences.HideToolbar = true; pdfDoc.ViewerPreferences.FitWindow = true; pdfDoc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage; pdfDoc.PageSettings.Orientation = PdfPageOrientation.Portrait; pdfDoc.PageSettings.Margins.All = 0; //To set coordinates to draw form fields RectangleF bounds = new RectangleF(180, 65, 156, 15); PdfUnitConverter con = new PdfUnitConverter(); //Read the file FileStream file = new FileStream(ResolveApplicationImagePath("careers.png"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); PdfImage img = new PdfBitmap(file); //Set the page size SizeF pageSize = new SizeF(500, 310); pdfDoc.PageSettings.Height = pageSize.Height; pdfDoc.PageSettings.Width = pageSize.Width; PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Bold); #region First Page pdfDoc.Pages.Add(); PdfPage firstPage = pdfDoc.Pages[0]; pdfDoc.Pages[0].Graphics.DrawImage(img, 0, 0, pageSize.Width, pageSize.Height); pdfDoc.Pages[0].Graphics.DrawString("General Information", pdfFont, new PdfSolidBrush(new PdfColor(213, 123, 19)), 25, 40); pdfDoc.Pages[0].Graphics.DrawString("Education Grade", pdfFont, new PdfSolidBrush(new PdfColor(213, 123, 19)), 25, 190); pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10); //Create fields in first page. pdfDoc.Pages[0].Graphics.DrawString("First Name:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 65); //Create text box for firstname. PdfTextBoxField textBoxField1 = new PdfTextBoxField(pdfDoc.Pages[0], "FirstName"); textBoxField1.ToolTip = "First Name"; PdfStandardFont font1 = new PdfStandardFont(PdfFontFamily.Helvetica, 10); textBoxField1.Font = font1; textBoxField1.BorderColor = new PdfColor(Color.Gray); textBoxField1.BorderStyle = PdfBorderStyle.Beveled; textBoxField1.Bounds = bounds; pdfDoc.Form.Fields.Add(textBoxField1); pdfDoc.Pages[0].Graphics.DrawString("Last Name:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 83); //Set position to draw form fields bounds.Y = bounds.Y + 18; //Create text box for lastname. PdfTextBoxField textBoxField2 = new PdfTextBoxField(pdfDoc.Pages[0], "LastName"); textBoxField2.ToolTip = "Last Name"; textBoxField2.Font = font1; textBoxField2.BorderColor = new PdfColor(Color.Gray); textBoxField2.BorderStyle = PdfBorderStyle.Beveled; textBoxField2.Bounds = bounds; pdfDoc.Form.Fields.Add(textBoxField2); pdfDoc.Pages[0].Graphics.DrawString("Email:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 103); //Set position to draw form fields bounds.Y = bounds.Y + 18; //Create text box for Email. PdfTextBoxField textBoxField3 = new PdfTextBoxField(pdfDoc.Pages[0], "Email"); textBoxField3.ToolTip = "Email id"; textBoxField3.Font = font1; textBoxField3.BorderColor = new PdfColor(Color.Gray); textBoxField3.BorderStyle = PdfBorderStyle.Beveled; textBoxField3.Bounds = bounds; pdfDoc.Form.Fields.Add(textBoxField3); pdfDoc.Pages[0].Graphics.DrawString("Business Phone:", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 123); //Set position to draw form fields bounds.Y = bounds.Y + 18; //Create text box for Business phone. PdfTextBoxField textBoxField4 = new PdfTextBoxField(pdfDoc.Pages[0], "Business"); textBoxField4.ToolTip = "Business phone"; textBoxField4.Font = font1; textBoxField4.BorderColor = new PdfColor(Color.Gray); textBoxField4.BorderStyle = PdfBorderStyle.Beveled; textBoxField4.Bounds = bounds; pdfDoc.Form.Fields.Add(textBoxField4); pdfDoc.Pages[0].Graphics.DrawString("Which position are\nyou applying for?", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 143); //Create combo box for Position. #region Create ComboBox //Set position to draw Combo Box bounds.Y = bounds.Y + 24; PdfComboBoxField comboBox = new PdfComboBoxField(pdfDoc.Pages[0], "JobTitle"); comboBox.Bounds = bounds; comboBox.BorderWidth = 1; comboBox.BorderColor = new PdfColor(Color.Gray); comboBox.Font = pdfFont; comboBox.ToolTip = "Job Title"; comboBox.Items.Add(new PdfListFieldItem("Development", "accounts")); comboBox.Items.Add(new PdfListFieldItem("Support", "advertise")); comboBox.Items.Add(new PdfListFieldItem("Documentation", "agri")); pdfDoc.Form.Fields.Add(comboBox); #endregion pdfDoc.Pages[0].Graphics.DrawString("Highest qualification", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), 25, 217); //Create Checkbox box. #region Create CheckBox pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 8); //Set position to draw Checkbox bounds.Y = 239; bounds.X = 25; bounds.Width = 10; bounds.Height = 10; // Create a Check Box PdfCheckBoxField chb = new PdfCheckBoxField(pdfDoc.Pages[0], "Adegree"); chb.Font = pdfFont; chb.ToolTip = "degree"; chb.Bounds = bounds; chb.BorderColor = new PdfColor(Color.Gray); bounds.X += chb.Bounds.Height + 10; pdfDoc.Pages[0].Graphics.DrawString("Associate degree", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y); bounds.X += 90; pdfDoc.Form.Fields.Add(chb); //Create a Checkbox chb = new PdfCheckBoxField(pdfDoc.Pages[0], "Bdegree"); chb.Font = pdfFont; chb.Bounds = bounds; chb.BorderColor = new PdfColor(Color.Gray); bounds.X += chb.Bounds.Height + 10; pdfDoc.Pages[0].Graphics.DrawString("Bachelor degree", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y); bounds.X += 90; pdfDoc.Form.Fields.Add(chb); //Create a Checkbox chb = new PdfCheckBoxField(pdfDoc.Pages[0], "college"); chb.Font = pdfFont; chb.ToolTip = "college"; chb.Bounds = bounds; chb.BorderColor = new PdfColor(Color.Gray); bounds.X += chb.Bounds.Height + 10; pdfDoc.Pages[0].Graphics.DrawString("College", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y); bounds.Y += 20; bounds.X = 25; pdfDoc.Form.Fields.Add(chb); //Create a Checkbox chb = new PdfCheckBoxField(pdfDoc.Pages[0], "pg"); chb.Font = pdfFont; chb.Bounds = bounds; chb.BorderColor = new PdfColor(Color.Gray); bounds.X += chb.Bounds.Height + 10; pdfDoc.Pages[0].Graphics.DrawString("Post Graduate", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y); bounds.X += 90; pdfDoc.Form.Fields.Add(chb); //Create a Checkbox chb = new PdfCheckBoxField(pdfDoc.Pages[0], "mba"); chb.Font = pdfFont; chb.Bounds = bounds; chb.BorderColor = new PdfColor(Color.Gray); bounds.X += chb.Bounds.Height + 10; pdfDoc.Pages[0].Graphics.DrawString("MBA", pdfFont, new PdfSolidBrush(new PdfColor(124, 143, 166)), bounds.X, bounds.Y); pdfDoc.Form.Fields.Add(chb); #endregion # region Create Button
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); }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PdfFixedDocument document = new PdfFixedDocument(); PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 12); PdfBrush brush = new PdfBrush(); PdfPage page = document.Pages.Add(); // First name page.Graphics.DrawString("First name:", helvetica, brush, 50, 50); PdfTextBoxField firstNameTextBox = new PdfTextBoxField("firstname"); page.Fields.Add(firstNameTextBox); firstNameTextBox.Widgets[0].Font = helvetica; firstNameTextBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 45, 200, 20); firstNameTextBox.Widgets[0].BorderColor = PdfRgbColor.Black; firstNameTextBox.Widgets[0].BorderWidth = 1; // Last name page.Graphics.DrawString("Last name:", helvetica, brush, 50, 80); PdfTextBoxField lastNameTextBox = new PdfTextBoxField("lastname"); page.Fields.Add(lastNameTextBox); lastNameTextBox.Widgets[0].Font = helvetica; lastNameTextBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 75, 200, 20); lastNameTextBox.Widgets[0].BorderColor = PdfRgbColor.Black; lastNameTextBox.Widgets[0].BorderWidth = 1; // Sex page.Graphics.DrawString("Sex:", helvetica, brush, 50, 110); PdfRadioButtonField sexRadioButton = new PdfRadioButtonField("sex"); PdfRadioButtonWidget maleRadioItem = new PdfRadioButtonWidget(); sexRadioButton.Widgets.Add(maleRadioItem); PdfRadioButtonWidget femaleRadioItem = new PdfRadioButtonWidget(); sexRadioButton.Widgets.Add(femaleRadioItem); page.Fields.Add(sexRadioButton); page.Graphics.DrawString("Male", helvetica, brush, 180, 110); maleRadioItem.ExportValue = "M"; maleRadioItem.CheckStyle = PdfCheckStyle.Circle; maleRadioItem.VisualRectangle = new PdfVisualRectangle(150, 105, 20, 20); maleRadioItem.BorderColor = PdfRgbColor.Black; maleRadioItem.BorderWidth = 1; page.Graphics.DrawString("Female", helvetica, brush, 280, 110); femaleRadioItem.ExportValue = "F"; femaleRadioItem.CheckStyle = PdfCheckStyle.Circle; femaleRadioItem.VisualRectangle = new PdfVisualRectangle(250, 105, 20, 20); femaleRadioItem.BorderColor = PdfRgbColor.Black; femaleRadioItem.BorderWidth = 1; // First car page.Graphics.DrawString("First car:", helvetica, brush, 50, 140); PdfComboBoxField firstCarList = new PdfComboBoxField("firstcar"); firstCarList.Items.Add(new PdfListItem("Mercedes", "Mercedes")); firstCarList.Items.Add(new PdfListItem("BMW", "BMW")); firstCarList.Items.Add(new PdfListItem("Audi", "Audi")); firstCarList.Items.Add(new PdfListItem("Volkswagen", "Volkswagen")); firstCarList.Items.Add(new PdfListItem("Porsche", "Porsche")); firstCarList.Items.Add(new PdfListItem("Honda", "Honda")); firstCarList.Items.Add(new PdfListItem("Toyota", "Toyota")); firstCarList.Items.Add(new PdfListItem("Lexus", "Lexus")); firstCarList.Items.Add(new PdfListItem("Infiniti", "Infiniti")); firstCarList.Items.Add(new PdfListItem("Acura", "Acura")); page.Fields.Add(firstCarList); firstCarList.Widgets[0].Font = helvetica; firstCarList.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 135, 200, 20); firstCarList.Widgets[0].BorderColor = PdfRgbColor.Black; firstCarList.Widgets[0].BorderWidth = 1; // Second car page.Graphics.DrawString("Second car:", helvetica, brush, 50, 170); PdfListBoxField secondCarList = new PdfListBoxField("secondcar"); secondCarList.Items.Add(new PdfListItem("Mercedes", "Mercedes")); secondCarList.Items.Add(new PdfListItem("BMW", "BMW")); secondCarList.Items.Add(new PdfListItem("Audi", "Audi")); secondCarList.Items.Add(new PdfListItem("Volkswagen", "Volkswagen")); secondCarList.Items.Add(new PdfListItem("Porsche", "Porsche")); secondCarList.Items.Add(new PdfListItem("Honda", "Honda")); secondCarList.Items.Add(new PdfListItem("Toyota", "Toyota")); secondCarList.Items.Add(new PdfListItem("Lexus", "Lexus")); secondCarList.Items.Add(new PdfListItem("Infiniti", "Infiniti")); secondCarList.Items.Add(new PdfListItem("Acura", "Acura")); page.Fields.Add(secondCarList); secondCarList.Widgets[0].Font = helvetica; secondCarList.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 165, 200, 60); secondCarList.Widgets[0].BorderColor = PdfRgbColor.Black; secondCarList.Widgets[0].BorderWidth = 1; // I agree page.Graphics.DrawString("I agree:", helvetica, brush, 50, 240); PdfCheckBoxField agreeCheckBox = new PdfCheckBoxField("agree"); page.Fields.Add(agreeCheckBox); agreeCheckBox.Widgets[0].Font = helvetica; (agreeCheckBox.Widgets[0] as PdfCheckWidget).ExportValue = "YES"; (agreeCheckBox.Widgets[0] as PdfCheckWidget).CheckStyle = PdfCheckStyle.Check; agreeCheckBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 235, 20, 20); agreeCheckBox.Widgets[0].BorderColor = PdfRgbColor.Black; agreeCheckBox.Widgets[0].BorderWidth = 1; // Sign here page.Graphics.DrawString("Sign here:", helvetica, brush, 50, 270); PdfSignatureField signHereField = new PdfSignatureField("signhere"); page.Fields.Add(signHereField); signHereField.Widgets[0].Font = helvetica; signHereField.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 265, 200, 60); // Submit form PdfPushButtonField submitBtn = new PdfPushButtonField("submit"); page.Fields.Add(submitBtn); submitBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(450, 45, 150, 30); (submitBtn.Widgets[0] as PdfPushButtonWidget).Caption = "Submit form"; submitBtn.Widgets[0].BackgroundColor = PdfRgbColor.LightGray; PdfSubmitFormAction submitFormAction = new PdfSubmitFormAction(); submitFormAction.DataFormat = PdfSubmitDataFormat.FDF; submitFormAction.Fields.Add("firstname"); submitFormAction.Fields.Add("lastname"); submitFormAction.Fields.Add("sex"); submitFormAction.Fields.Add("firstcar"); submitFormAction.Fields.Add("secondcar"); submitFormAction.Fields.Add("agree"); submitFormAction.Fields.Add("signhere"); submitFormAction.SubmitFields = true; submitFormAction.Url = "http://www.xfiniumsoft.com/"; submitBtn.Widgets[0].MouseUp = submitFormAction; // Reset form PdfPushButtonField resetBtn = new PdfPushButtonField("reset"); page.Fields.Add(resetBtn); resetBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(450, 85, 150, 30); (resetBtn.Widgets[0] as PdfPushButtonWidget).Caption = "Reset form"; resetBtn.Widgets[0].BackgroundColor = PdfRgbColor.LightGray; PdfResetFormAction resetFormAction = new PdfResetFormAction(); resetBtn.Widgets[0].MouseUp = resetFormAction; // Print form PdfPushButtonField printBtn = new PdfPushButtonField("print"); page.Fields.Add(printBtn); printBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(450, 125, 150, 30); (printBtn.Widgets[0] as PdfPushButtonWidget).Caption = "Print form"; printBtn.Widgets[0].BackgroundColor = PdfRgbColor.LightGray; PdfJavaScriptAction printAction = new PdfJavaScriptAction(); printAction.Script = "this.print(true);\n"; printBtn.Widgets[0].MouseUp = printAction; SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.formgenerator.pdf") }; return output; }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PdfFixedDocument document = new PdfFixedDocument(); PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 12); PdfBrush brush = new PdfBrush(); PdfPage page = document.Pages.Add(); // First name page.Graphics.DrawString("First name:", helvetica, brush, 50, 50); PdfTextBoxField firstNameTextBox = new PdfTextBoxField("firstname"); page.Fields.Add(firstNameTextBox); firstNameTextBox.Widgets[0].Font = helvetica; firstNameTextBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 45, 200, 20); firstNameTextBox.Widgets[0].BorderColor = PdfRgbColor.Black; firstNameTextBox.Widgets[0].BorderWidth = 1; // Last name page.Graphics.DrawString("Last name:", helvetica, brush, 50, 80); PdfTextBoxField lastNameTextBox = new PdfTextBoxField("lastname"); page.Fields.Add(lastNameTextBox); lastNameTextBox.Widgets[0].Font = helvetica; lastNameTextBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 75, 200, 20); lastNameTextBox.Widgets[0].BorderColor = PdfRgbColor.Black; lastNameTextBox.Widgets[0].BorderWidth = 1; // Sex page.Graphics.DrawString("Sex:", helvetica, brush, 50, 110); PdfRadioButtonField sexRadioButton = new PdfRadioButtonField("sex"); PdfRadioButtonWidget maleRadioItem = new PdfRadioButtonWidget(); sexRadioButton.Widgets.Add(maleRadioItem); PdfRadioButtonWidget femaleRadioItem = new PdfRadioButtonWidget(); sexRadioButton.Widgets.Add(femaleRadioItem); page.Fields.Add(sexRadioButton); page.Graphics.DrawString("Male", helvetica, brush, 180, 110); maleRadioItem.ExportValue = "M"; maleRadioItem.CheckStyle = PdfCheckStyle.Circle; maleRadioItem.VisualRectangle = new PdfVisualRectangle(150, 105, 20, 20); maleRadioItem.BorderColor = PdfRgbColor.Black; maleRadioItem.BorderWidth = 1; page.Graphics.DrawString("Female", helvetica, brush, 280, 110); femaleRadioItem.ExportValue = "F"; femaleRadioItem.CheckStyle = PdfCheckStyle.Circle; femaleRadioItem.VisualRectangle = new PdfVisualRectangle(250, 105, 20, 20); femaleRadioItem.BorderColor = PdfRgbColor.Black; femaleRadioItem.BorderWidth = 1; // First car page.Graphics.DrawString("First car:", helvetica, brush, 50, 140); PdfComboBoxField firstCarList = new PdfComboBoxField("firstcar"); firstCarList.Items.Add(new PdfListItem("Mercedes", "Mercedes")); firstCarList.Items.Add(new PdfListItem("BMW", "BMW")); firstCarList.Items.Add(new PdfListItem("Audi", "Audi")); firstCarList.Items.Add(new PdfListItem("Volkswagen", "Volkswagen")); firstCarList.Items.Add(new PdfListItem("Porsche", "Porsche")); firstCarList.Items.Add(new PdfListItem("Honda", "Honda")); firstCarList.Items.Add(new PdfListItem("Toyota", "Toyota")); firstCarList.Items.Add(new PdfListItem("Lexus", "Lexus")); firstCarList.Items.Add(new PdfListItem("Infiniti", "Infiniti")); firstCarList.Items.Add(new PdfListItem("Acura", "Acura")); page.Fields.Add(firstCarList); firstCarList.Widgets[0].Font = helvetica; firstCarList.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 135, 200, 20); firstCarList.Widgets[0].BorderColor = PdfRgbColor.Black; firstCarList.Widgets[0].BorderWidth = 1; // Second car page.Graphics.DrawString("Second car:", helvetica, brush, 50, 170); PdfListBoxField secondCarList = new PdfListBoxField("secondcar"); secondCarList.Items.Add(new PdfListItem("Mercedes", "Mercedes")); secondCarList.Items.Add(new PdfListItem("BMW", "BMW")); secondCarList.Items.Add(new PdfListItem("Audi", "Audi")); secondCarList.Items.Add(new PdfListItem("Volkswagen", "Volkswagen")); secondCarList.Items.Add(new PdfListItem("Porsche", "Porsche")); secondCarList.Items.Add(new PdfListItem("Honda", "Honda")); secondCarList.Items.Add(new PdfListItem("Toyota", "Toyota")); secondCarList.Items.Add(new PdfListItem("Lexus", "Lexus")); secondCarList.Items.Add(new PdfListItem("Infiniti", "Infiniti")); secondCarList.Items.Add(new PdfListItem("Acura", "Acura")); page.Fields.Add(secondCarList); secondCarList.Widgets[0].Font = helvetica; secondCarList.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 165, 200, 60); secondCarList.Widgets[0].BorderColor = PdfRgbColor.Black; secondCarList.Widgets[0].BorderWidth = 1; // I agree page.Graphics.DrawString("I agree:", helvetica, brush, 50, 240); PdfCheckBoxField agreeCheckBox = new PdfCheckBoxField("agree"); page.Fields.Add(agreeCheckBox); agreeCheckBox.Widgets[0].Font = helvetica; (agreeCheckBox.Widgets[0] as PdfCheckWidget).ExportValue = "YES"; (agreeCheckBox.Widgets[0] as PdfCheckWidget).CheckStyle = PdfCheckStyle.Check; agreeCheckBox.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 235, 20, 20); agreeCheckBox.Widgets[0].BorderColor = PdfRgbColor.Black; agreeCheckBox.Widgets[0].BorderWidth = 1; // Sign here page.Graphics.DrawString("Sign here:", helvetica, brush, 50, 270); PdfSignatureField signHereField = new PdfSignatureField("signhere"); page.Fields.Add(signHereField); signHereField.Widgets[0].Font = helvetica; signHereField.Widgets[0].VisualRectangle = new PdfVisualRectangle(150, 265, 200, 60); // Submit form PdfPushButtonField submitBtn = new PdfPushButtonField("submit"); page.Fields.Add(submitBtn); submitBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(450, 45, 150, 30); (submitBtn.Widgets[0] as PdfPushButtonWidget).Caption = "Submit form"; submitBtn.Widgets[0].BackgroundColor = PdfRgbColor.LightGray; PdfSubmitFormAction submitFormAction = new PdfSubmitFormAction(); submitFormAction.DataFormat = PdfSubmitDataFormat.FDF; submitFormAction.Fields.Add("firstname"); submitFormAction.Fields.Add("lastname"); submitFormAction.Fields.Add("sex"); submitFormAction.Fields.Add("firstcar"); submitFormAction.Fields.Add("secondcar"); submitFormAction.Fields.Add("agree"); submitFormAction.Fields.Add("signhere"); submitFormAction.SubmitFields = true; submitFormAction.Url = "http://www.xfiniumsoft.com/"; submitBtn.Widgets[0].MouseUp = submitFormAction; // Reset form PdfPushButtonField resetBtn = new PdfPushButtonField("reset"); page.Fields.Add(resetBtn); resetBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(450, 85, 150, 30); (resetBtn.Widgets[0] as PdfPushButtonWidget).Caption = "Reset form"; resetBtn.Widgets[0].BackgroundColor = PdfRgbColor.LightGray; PdfResetFormAction resetFormAction = new PdfResetFormAction(); resetBtn.Widgets[0].MouseUp = resetFormAction; // Print form PdfPushButtonField printBtn = new PdfPushButtonField("print"); page.Fields.Add(printBtn); printBtn.Widgets[0].VisualRectangle = new PdfVisualRectangle(450, 125, 150, 30); (printBtn.Widgets[0] as PdfPushButtonWidget).Caption = "Print form"; printBtn.Widgets[0].BackgroundColor = PdfRgbColor.LightGray; PdfJavaScriptAction printAction = new PdfJavaScriptAction(); printAction.Script = "this.print(true);\n"; printBtn.Widgets[0].MouseUp = printAction; SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.formgenerator.pdf") }; return(output); }
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; }
public static bool AddFieldsToPDF(string fileName, string targetFileName, WordFieldsInfo wfi, ref bool keepGoing) { try { using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) using (PdfLoadedDocument pdfDoc = new PdfLoadedDocument(stream)) { if (pdfDoc.Form == null) { pdfDoc.CreateForm(); } else { // Clear the existing Form while (pdfDoc.Form.Fields.Count > 0) { pdfDoc.Form.Fields.RemoveAt(0); } } foreach (WordFieldInfo fi in wfi.Fields) { if (!keepGoing) { return(false); } fi.Status = "Adding to PDF"; if (fi.Page <= pdfDoc.Pages.Count) { PdfPageBase page = pdfDoc.Pages[fi.Page - 1]; switch (fi.FieldType) { case FieldType.DropDownList: PdfComboBoxField cboField = new PdfComboBoxField(page, GetFieldName(fi)); cboField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y, (float)fi.Width, (float)fi.Height); foreach (KeyValuePair <string, string> kvp in fi.Options) { cboField.Items.Add(new PdfListFieldItem(kvp.Key, kvp.Value)); } cboField.SelectedIndex = 0; pdfDoc.Form.Fields.Add(cboField); break; case FieldType.SingleLine: case FieldType.MultiLine: PdfTextBoxField txtField = new PdfTextBoxField(page, GetFieldName(fi)); txtField.BackColor = new Syncfusion.Pdf.Graphics.PdfColor((float)0xdd); txtField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y, (float)fi.Width, (float)fi.Height); txtField.Multiline = true; txtField.Scrollable = true; txtField.SpellCheck = true; pdfDoc.Form.Fields.Add(txtField); break; case FieldType.Checkbox: PdfCheckBoxField chkField = new PdfCheckBoxField(page, GetFieldName(fi)); chkField.Bounds = new System.Drawing.RectangleF((float)fi.X, (float)fi.Y, (float)fi.Width, (float)fi.Height); pdfDoc.Form.Fields.Add(chkField); break; default: System.Diagnostics.Trace.WriteLine($"{fi.FieldType}"); break; } fi.Status = "Added to PDF"; } else { throw new IndexOutOfRangeException($"Field {fi.FieldType} at {fi.Page} ({fi.X}, {fi.Y}) is not on a valid page in the PDF"); } } using (Stream outStream = File.Open(targetFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { pdfDoc.Save(outStream); } pdfDoc.Close(true); } return(true); } catch (Exception ex) { throw; } }