public IActionResult Post()
        {
            var file = Path.GetFullPath("pdf/ESDC-EMP5624-new.pdf");
            //Load the PDF document
            FileStream        docStream      = new FileStream(file, FileMode.Open, FileAccess.Read);
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
            //Loads the form
            PdfLoadedForm form = loadedDocument.Form;
            //load the check box from field collection
            PdfLoadedCheckBoxField loadedCheckBoxField = form.Fields[0] as PdfLoadedCheckBoxField;
            // Syncfusion.Pdf.Parsing.
            // Console.WriteLine(form.Fields["EMP5624_E[0].Page1[0].txtF_phone_number[0]"]);
            PdfLoadedTextBoxField phoneNumberField = form.Fields["EMP5624_E[0].Page1[0].txtF_phone_number[0]"] as PdfLoadedTextBoxField;

            phoneNumberField.Text = "23481378653993";

            // random data
            var textEntries = new List <string>();

            //Fill the XFA form.
            form.EnableXfaFormFill = true;
            var data = new Dictionary <string, dynamic>();

            foreach (PdfLoadedField field in form.Fields)
            {
                if (field is PdfLoadedTextBoxField)
                {
                    data.Add(((PdfLoadedTextBoxField)field).Name, "Hello");
                }
                if (field is PdfLoadedCheckBoxField)
                {
                    data.Add(((PdfLoadedTextBoxField)field).Name, true);
                }
                if (field is PdfLoadedChoiceField)
                {
                    // ((PdfLoadedChoiceField)field).SelectedIndex = new int[] { 0 };
                    data.Add(((PdfLoadedTextBoxField)field).Name, 1);
                }
                if (field is PdfLoadedRadioButtonListField)
                {
                    // ((PdfLoadedRadioButtonListField)field).SelectedValue = "";
                    data.Add(((PdfLoadedTextBoxField)field).Name, true);
                }
            }

            // Flatten the whole form (prevent editing)
            // form.Flatten = true;

            //Save the PDF document to stream
            MemoryStream stream = new MemoryStream();

            loadedDocument.Save(stream);
            //If the position is not set to '0' then the PDF will be empty.
            stream.Position = 0;
            //Close the document.
            loadedDocument.Close(true);
            //Defining the ContentType for pdf file.
            string contentType = "application/pdf";
            //Define the file name.
            string fileName = "output.pdf";

            //Creates a FileContentResult object by using the file contents, content type, and file name.
            return(File(stream, contentType, fileName));
            // return Ok();
        }
Пример #2
0
        public void OnButtonClicked(object sender, EventArgs e)
        {
            Stream docStream = typeof(MailAttachment).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.PDF.Samples.Assets.FormFillingDocument.pdf");

            MemoryStream stream = new MemoryStream();

            //Load the existing PDF document
            using (PdfLoadedDocument ldoc = new PdfLoadedDocument(docStream))
            {
                //Get the PDF form
                PdfLoadedForm lForm = ldoc.Form;

                if (name.Text != null)
                {
                    //Load the textbox field
                    PdfLoadedTextBoxField nameText = lForm.Fields["name"] as PdfLoadedTextBoxField;

                    //Fill the text box
                    nameText.Text = this.name.Text;
                }

                //Get the Radio button field
                PdfLoadedRadioButtonListField genderRadio = lForm.Fields["gender"] as PdfLoadedRadioButtonListField;

                switch (gender.Items[gender.SelectedIndex])
                {
                case "Male":
                    genderRadio.SelectedIndex = 0;
                    break;

                case "Female":
                    genderRadio.SelectedIndex = 2;
                    break;

                case "Unspecified":
                    genderRadio.SelectedIndex = 1;
                    break;
                }

                //Load the textbox field
                PdfLoadedTextBoxField dobText = lForm.Fields["dob"] as PdfLoadedTextBoxField;

                //Fill the text box
                dobText.Text = this.dob.Date.ToString("dd MMMM yyyy");

                if (this.emailID.Text != null)
                {
                    //Load the textbox field
                    PdfLoadedTextBoxField emailText = lForm.Fields["email"] as PdfLoadedTextBoxField;

                    //Fill the text box
                    emailText.Text = this.emailID.Text;
                }

                //Load the combobox field
                PdfLoadedComboBoxField countryCombo = lForm.Fields["state"] as PdfLoadedComboBoxField;

                //Set the selected value
                countryCombo.SelectedValue = this.country.Items[country.SelectedIndex];

                //Get the Checkbox field
                PdfLoadedCheckBoxField newsCheck = lForm.Fields["newsletter"] as PdfLoadedCheckBoxField;

                newsCheck.Checked = this.newsLetter.IsToggled;

                //Flatten the form fields
                ldoc.Form.Flatten = true;

                //Save the PDF document
                ldoc.Save(stream);
            }

            stream.Position = 0;

            //Open in default system viewer.
            if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
            {
                Xamarin.Forms.DependencyService.Get <IMailService>().ComposeMail("MailAttachment.pdf", null, "Workshop Registration", "Syncfusion", stream);
            }
            else
            {
                Xamarin.Forms.DependencyService.Get <IMailService>().ComposeMail("MailAttachment.pdf", null, "Workshop Registration", "Syncfusion", stream);
            }
        }
Пример #3
0
        public ICustomActivityResult Execute()

        {
            try
            {
                StringWriter sw = new StringWriter();
                DataTable    dt = new DataTable("resultSet");
                dt.Columns.Add("Result", typeof(string));

                if (string.IsNullOrEmpty(Path))
                {
                    throw new Exception("File not found");
                }
                if (File.Exists(Path) == false)
                {
                    throw new Exception("File not found");
                }

                PdfLoadedDocument ldoc = null;
                if (string.IsNullOrEmpty(FilePassword))
                {
                    ldoc = new PdfLoadedDocument(Path);
                }
                else
                {
                    ldoc = new PdfLoadedDocument(Path, FilePassword);
                }

                // Loading Page collections
                PdfLoadedPageCollection loadedPages = ldoc.Pages;
                // Extract text from PDF document pages
                StringBuilder sb = new StringBuilder();
                foreach (PdfLoadedPage lpage in loadedPages)
                {
                    sb.Append(lpage.ExtractText());
                }


                /* Start Form */

                PdfLoadedForm pdfForm = ldoc.Form;
                bool          found   = false;
                if (pdfForm != null)
                {
                    if (pdfForm.Fields.Count > 0)
                    {
                        sb.AppendLine("");
                        sb.AppendLine("-------- Form Controls Values --------");
                    }
                    foreach (PdfLoadedField field in pdfForm.Fields)
                    {
                        string fname  = field.Name;
                        string fValue = string.Empty;

                        try
                        {
                            if (field is PdfLoadedTextBoxField)
                            {
                                PdfLoadedTextBoxField textField = (field as PdfLoadedTextBoxField);
                                fValue = textField.Text;
                                found  = true;
                            }
                            if (field is PdfLoadedCheckBoxField)
                            {
                                PdfLoadedCheckBoxField chbField = (field as PdfLoadedCheckBoxField);
                                fValue = chbField.Checked ? "checked" : string.Empty;
                                found  = true;
                            }
                            if (field is PdfLoadedComboBoxField)
                            {
                                PdfLoadedComboBoxField listField = (field as PdfLoadedComboBoxField);
                                fValue = listField.SelectedValue;
                                found  = true;
                            }
                            if (field is PdfLoadedRadioButtonListField)
                            {
                                PdfLoadedRadioButtonListField listField = (field as PdfLoadedRadioButtonListField);
                                fValue = listField.SelectedItem.Value;
                                found  = true;
                            }
                            if (field is PdfLoadedListBoxField)
                            {
                                PdfLoadedListBoxField listField = (field as PdfLoadedListBoxField);
                                string[] arrValues = listField.SelectedValue;
                                if (arrValues != null)
                                {
                                    string allValues = string.Empty;
                                    foreach (string cValue in arrValues)
                                    {
                                        allValues += cValue + ";";
                                    }
                                    int lindex = allValues.LastIndexOf(";");
                                    if (lindex > -1)
                                    {
                                        fValue = allValues.Substring(0, lindex);
                                    }
                                    else
                                    {
                                        fValue = allValues;
                                    }
                                }
                                else
                                {
                                    fValue = string.Empty;
                                }
                                found = true;
                            }

                            if (found)
                            {
                                sb.AppendLine(string.Format("Field Name: {0} Value: {1}", fname, fValue));
                                found = false;
                            }
                            else
                            {
                                sb.AppendLine(string.Format("Field Name: {0} Error: {1}", fname, "The control type was not recognized."));
                            }
                        }
                        catch
                        {
                            sb.AppendLine(string.Format("Field Name: {0} Error: {1}", fname, "Failed to retrieve the value."));
                        }
                    }
                }
                /* End Form */

                dt.Rows.Add(sb.ToString());

                return(this.GenerateActivityResult(dt));
            }
            catch
            {
                throw new Exception("Failed to process the file.");
            }
        }