Example #1
0
        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;
            }
        }