示例#1
0
        private void ConvertUrlsListButton_Click(object sender, EventArgs e)
        {
            if (DefaultOutputPathTextBox.Text == "")
            {
                StatusRichTextBox.Text = "Неверно задан каталог для выходного файла!";
                OutputFileTextBox.Focus();
                return;
            }

            StatusRichTextBox.Text = "";

            if (ConvertAllCheckBox.Checked)
            {
                UrlsToConvertDataGridView.SelectAll();
            }

            if (!int.TryParse(ResponseTimeoutTextBox.Text, out var timeout))
            {
                timeout = 15000;
            }

            var count = 0;

            foreach (DataGridViewRow row in UrlsToConvertDataGridView.SelectedRows)
            {
                count++;
                var realDataRow = (UrlToConvert)row.DataBoundItem;

                var r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");
                var m = r.Match(realDataRow.Url);
                if (!m.Success)
                {
                    StatusRichTextBox.Text += "Неверно задан URL html-страницы: " + realDataRow.Url + "\n";
                    HtmlPageTextBox.Focus();
                    continue;
                }

                var url = FixUrlsList(realDataRow.Url);

                var fileName = realDataRow.Name;
                if (GetTitleFromUrlСheckBox.Checked)
                {
                    StatusRichTextBox.Text += "Идет получение Title по Url...\n";
                    var title = ResponseTagHelper.GetWebPageTitle(url, timeout);
                    if (title != "")
                    {
                        fileName = title;
                    }
                }

                fileName = fileName.Replace('/', '_');
                fileName = fileName.Replace('\\', '_');
                fileName = fileName.Replace(':', '_');
                fileName = fileName.Replace('*', '_');
                fileName = fileName.Replace('?', '_');
                fileName = fileName.Replace('«', '_');
                fileName = fileName.Replace('<', '_');
                fileName = fileName.Replace('>', '_');
                fileName = fileName.Replace('|', '_');

                if (!string.Equals(fileName, realDataRow.Name))
                {
                    StatusRichTextBox.Text += "Выходное имя файла \"" + realDataRow.Name + "\" изменено на \"" + fileName + "\"\n";
                }

                if (NumerateOutputPdfFileNamesCheckBox.Checked)
                {
                    fileName = count + " - " + fileName;
                }

                Convert(url, Path.Combine(DefaultOutputPathTextBox.Text, fileName + ".pdf"),
                        true, realDataRow.Open);
            }
        }
示例#2
0
        private void Convert(string url, string outputFileFullPath, bool immediately, bool open)
        {
            try
            {
                PdfConvert.Environment.Debug = false;
                if (ExeFileTextBox.Text != "")
                {
                    PdfConvert.Environment.WkHtmlToPdfPath = ExeFileTextBox.Text;
                }

                var pdfDocument = new PdfDocument();
                if (!immediately)
                {
                    if (!HtmlFromFileCheckBox.Checked)
                    {
                        var r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");
                        var m = r.Match(HtmlPageTextBox.Text);
                        if (!m.Success)
                        {
                            StatusRichTextBox.Text = "Неверно задан URL html-страницы!";
                            HtmlPageTextBox.Focus();
                            return;
                        }

                        pdfDocument.Url = HtmlPageTextBox.Text;
                    }
                    else
                    {
                        pdfDocument.Url  = "-";
                        pdfDocument.Html = StatusRichTextBox.Text;
                    }
                }
                else
                {
                    pdfDocument.Url = url;
                    if (AddUrlToFooterCheckBox.Checked)
                    {
                        pdfDocument.FooterCenter = url;
                    }
                    if (AddTitleToHeaderCheckBox.Checked)
                    {
                        pdfDocument.HeaderLeft = "[title]";
                    }
                }

                PdfConvert.ConvertHtmlToPdf(pdfDocument, new PdfOutput
                {
                    OutputFilePath = outputFileFullPath
                }, HideWkhtmltopdfWindowCheckBox.Checked);

                StatusRichTextBox.Text += "\"" + outputFileFullPath + "\" создан.\n";

                if (!open)
                {
                    return;
                }

                if (!UseOtherProgramToOpenPdfCheckBox.Checked)
                {
                    Process.Start(outputFileFullPath);
                }
                else
                {
                    Process.Start(OtherProgramToOpenPdfPathTextBox.Text, outputFileFullPath);
                }
            }
            catch (Exception ex)
            {
                StatusRichTextBox.Text += "Произошла ошибка: " + ex.Message + "\n";
            }
        }