예제 #1
0
        //HTML Conversion
        private void convertBtn_Click(object sender, EventArgs e)
        {
            //Initialize HTML converter
            HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

            //WebKit converter settings
            WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

            string urlToConvert = string.Empty;

            if (rdo_urlBtn.Checked)
            {
                if (this.openFileDialog1.FileName == string.Empty)
                {
                    urlToConvert = url_TxtBox.Text;
                }
                else
                {
                    urlToConvert = openFileDialog1.FileName;
                }
            }
            if (!IsUrlValid(urlToConvert) && urlToConvert != string.Empty)
            {
                if (MessageBox.Show("Please enter a valid URL", "Alert!!!",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information)
                    == DialogResult.OK)
                {
                    url_TxtBox.Text = string.Empty;
                }
            }
            else
            {
                int delay = 0;
                if (int.TryParse(this.delay_txtBox.Text, out delay))
                {
                    //Set additional delay
                    webKitSettings.AdditionalDelay = delay;

                    //Assign the WebKit binaries path
                    //webKitSettings.WebKitPath = @"../../QtBinaries";

                    //Enable javascript
                    webKitSettings.EnableJavaScript = this.JavaScript_CheckBox.Checked;

                    //HTML to PDF conversion
                    if (this.rdo_PDFBtn.Checked)
                    {
                        //Enable hyperlink
                        webKitSettings.EnableHyperLink = this.hyperlink_checkBox.Checked;

                        //Enable bookmarks
                        webKitSettings.EnableBookmarks = this.bookmark_checkBox.Checked;

                        //Enble form
                        webKitSettings.EnableForm = this.form_checkBox.Checked;

                        //Enable toc
                        webKitSettings.EnableToc = this.toc_checkBox.Checked;

                        //Page rotation angle
                        webKitSettings.PageRotateAngle = (PdfPageRotateAngle)Enum.Parse(typeof(PdfPageRotateAngle), this.rotation_comboBox.SelectedItem.ToString());

                        int margin = 0;
                        if (int.TryParse(this.margin_comboBox.Text, out margin))
                        {
                            //Set page margins
                            webKitSettings.Margin.All = margin;

                            //Set page orientation.
                            if (this.rdo_portraitBtn.Checked)
                            {
                                webKitSettings.Orientation = PdfPageOrientation.Portrait;
                            }
                            else
                            {
                                webKitSettings.Orientation = PdfPageOrientation.Landscape;
                            }

                            //Adding Header
                            if (this.header_checkBox.Checked)
                            {
                                webKitSettings.PdfHeader = this.AddHeader(webKitSettings.PdfPageSize.Width, "Syncfusion Essential PDF", " ");
                            }

                            //Adding Footer
                            if (this.footer_checkBox.Checked)
                            {
                                webKitSettings.PdfFooter = this.AddFooter(webKitSettings.PdfPageSize.Width, "@Copyright 2015");
                            }

                            htmlConverter.ConverterSettings = webKitSettings;

                            PdfDocument document = null;
                            try
                            {
                                if (rdo_urlBtn.Checked)
                                {
                                    //Convert url to PDF document.
                                    document = htmlConverter.Convert(urlToConvert);
                                }
                                else
                                {
                                    //Convert HTML string to PDF document
                                    document = htmlConverter.Convert(this.htmlString_txtBox.Text, this.baseURL_txtBox.Text);
                                }

                                // Save and close the document.
                                document.Save("Sample.pdf");
                                document.Close(true);

                                //Message box confirmation to view the created PDF document.
                                if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created",
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                                    == DialogResult.Yes)
                                {
                                    //Launching the PDF file using the default Application.[Acrobat Reader]
                                    System.Diagnostics.Process.Start("Sample.pdf");
                                    this.Close();
                                }
                                else
                                {
                                    // Exit
                                    this.Close();
                                }
                            }
                            catch (PdfException ex)
                            {
                                if (MessageBox.Show("WebKit assemblies are missing", "Alert!!!", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                                {
                                }
                            }
                        }
                        else
                        {
                            if (MessageBox.Show("Please enter a valid margin", "Alert!!!",
                                                MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                            {
                                margin_comboBox.Text = "20";
                            }
                        }
                    }
                    //HTML to SVG Conversion
                    else if (this.rdo_SvgBtn.Checked)
                    {
                        //Assign the WebKit settings
                        htmlConverter.ConverterSettings = webKitSettings;
                        try
                        {
                            //Convert url to svg
                            htmlConverter.ConvertToSvg(urlToConvert, "Sample.svg");

                            //Message box confirmation to view the created SVG document.
                            if (MessageBox.Show("Do you want to view the SVG file?", "SVG File Created",
                                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                                == DialogResult.Yes)
                            {
                                //Launching the SVG file using the default Application.[Default Browser]
                                System.Diagnostics.Process.Start("Sample.svg");
                                this.Close();
                            }
                            else
                            {
                                // Exit
                                this.Close();
                            }
                        }
                        catch (PdfException ex)
                        {
                            if (MessageBox.Show("WebKit assemblies are missing", "Alert!!!", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                            {
                            }
                        }
                    }
                    // HTML to Image Conversion
                    else if (this.rdo_ImageBtn.Checked)
                    {
                        //Assign the WebKit settings
                        htmlConverter.ConverterSettings = webKitSettings;
                        try
                        {
                            Image[] images;
                            if (this.rdo_urlBtn.Checked)
                            {
                                //Convert HTML to image
                                images = htmlConverter.ConvertToImage(urlToConvert);
                            }
                            else
                            {
                                //Convert HTML string to image
                                images = htmlConverter.ConvertToImage(this.htmlString_txtBox.Text, this.baseURL_txtBox.Text);
                            }

                            //Save the image
                            images[0].Save("Sample.png", System.Drawing.Imaging.ImageFormat.Png);
                            images[0].Dispose();

                            //Message box confirmation to view the created Image document.
                            if (MessageBox.Show("Do you want to view the image file?", "Image File Created",
                                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                                == DialogResult.Yes)
                            {
                                //Launching the Image file using the default Application.[Image viewer]
                                System.Diagnostics.Process.Start("Sample.png");
                                this.Close();
                            }
                            else
                            {
                                // Exit
                                this.Close();
                            }
                        }
                        catch (PdfException ex)
                        {
                            if (MessageBox.Show("WebKit assemblies are missing", "Alert!!!", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                            {
                            }
                        }
                    }
                    //HTML to MHTML conversion
                    else
                    {
                        //Assign the WebKit settings
                        htmlConverter.ConverterSettings = webKitSettings;
                        try
                        {
                            //Convert url to mhtml
                            htmlConverter.ConvertToMhtml(urlToConvert, "Sample.mhtml");

                            //Message box confirmation to view the created MHTML document.
                            if (MessageBox.Show("Do you want to view the MHTML file?", "MHTML File Created",
                                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                                == DialogResult.Yes)
                            {
                                //Launching the MHTML file using the default Application.[Default browser]
                                System.Diagnostics.Process.Start("Sample.mhtml");
                                this.Close();
                            }
                            else
                            {
                                // Exit
                                this.Close();
                            }
                        }
                        catch (PdfException ex)
                        {
                            if (MessageBox.Show("WebKit assemblies are missing", "Alert!!!", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                            {
                            }
                        }
                    }
                }
                else
                {
                    if (MessageBox.Show("Please enter a valid delay", "Alert!!!", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        == DialogResult.OK)
                    {
                        delay_txtBox.Text = "0";
                    }
                }
            }
        }