Пример #1
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);
            }
        }
Пример #2
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.");
            }
        }