Exemple #1
0
        private void cmdSaveAsPdf_Click(object sender, EventArgs e)
        {
            // Check to see if we have any "Print to PDF" printers, as they will be a lot more reliable than wkhtmltopdf
            string strPdfPrinter = string.Empty;

            foreach (string strPrinter in PrinterSettings.InstalledPrinters)
            {
                if (strPrinter == "Microsoft Print to PDF" || strPrinter == "Foxit Reader PDF Printer" || strPrinter == "Adobe PDF")
                {
                    strPdfPrinter = strPrinter;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(strPdfPrinter))
            {
                DialogResult ePdfPrinterDialogResult = Program.MainForm.ShowMessageBox(this,
                                                                                       string.Format(GlobalOptions.CultureInfo, LanguageManager.GetString("Message_Viewer_FoundPDFPrinter"), strPdfPrinter),
                                                                                       LanguageManager.GetString("MessageTitle_Viewer_FoundPDFPrinter"),
                                                                                       MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                if (ePdfPrinterDialogResult == DialogResult.Cancel)
                {
                    return;
                }
                if (ePdfPrinterDialogResult == DialogResult.Yes)
                {
                    if (DoPdfPrinterShortcut(strPdfPrinter))
                    {
                        return;
                    }
                    Program.MainForm.ShowMessageBox(this, LanguageManager.GetString("Message_Viewer_PDFPrinterError"));
                }
            }

            // Save the generated output as PDF.
            SaveFileDialog1.Filter = LanguageManager.GetString("DialogFilter_Pdf") + '|' + LanguageManager.GetString("DialogFilter_All");
            SaveFileDialog1.Title  = LanguageManager.GetString("Button_Viewer_SaveAsPdf");
            SaveFileDialog1.ShowDialog();
            string strSaveFile = SaveFileDialog1.FileName;

            if (string.IsNullOrEmpty(strSaveFile))
            {
                return;
            }

            if (!strSaveFile.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
            {
                strSaveFile += ".pdf";
            }

            if (!Directory.Exists(Path.GetDirectoryName(strSaveFile)) || !Utils.CanWriteToPath(strSaveFile))
            {
                Program.MainForm.ShowMessageBox(this, LanguageManager.GetString("Message_File_Cannot_Be_Accessed"));
                return;
            }
            if (File.Exists(strSaveFile))
            {
                try
                {
                    File.Delete(strSaveFile);
                }
                catch (IOException)
                {
                    Program.MainForm.ShowMessageBox(this, LanguageManager.GetString("Message_File_Cannot_Be_Accessed"));
                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    Program.MainForm.ShowMessageBox(this, LanguageManager.GetString("Message_File_Cannot_Be_Accessed"));
                    return;
                }
            }

            // No PDF printer found, let's use wkhtmltopdf

            PdfDocument objPdfDocument = new PdfDocument
            {
                Html = webViewer.DocumentText
            };

            objPdfDocument.ExtraParams.Add("encoding", "UTF-8");
            objPdfDocument.ExtraParams.Add("dpi", "300");
            objPdfDocument.ExtraParams.Add("margin-top", "13");
            objPdfDocument.ExtraParams.Add("margin-bottom", "19");
            objPdfDocument.ExtraParams.Add("margin-left", "13");
            objPdfDocument.ExtraParams.Add("margin-right", "13");
            objPdfDocument.ExtraParams.Add("image-quality", "100");
            objPdfDocument.ExtraParams.Add("print-media-type", string.Empty);

            try
            {
                PdfConvert.ConvertHtmlToPdf(objPdfDocument, new PdfConvertEnvironment
                {
                    WkHtmlToPdfPath = Path.Combine(Utils.GetStartupPath, "wkhtmltopdf.exe"),
                    Timeout         = 60000,
                    TempFolderPath  = Path.GetTempPath()
                }, new PdfOutput
                {
                    OutputFilePath = strSaveFile
                });

                if (!string.IsNullOrWhiteSpace(GlobalOptions.PDFAppPath))
                {
                    Uri    uriPath   = new Uri(strSaveFile);
                    string strParams = GlobalOptions.PDFParameters
                                       .Replace("{page}", "1")
                                       .Replace("{localpath}", uriPath.LocalPath)
                                       .Replace("{absolutepath}", uriPath.AbsolutePath);
                    ProcessStartInfo objPdfProgramProcess = new ProcessStartInfo
                    {
                        FileName    = GlobalOptions.PDFAppPath,
                        Arguments   = strParams,
                        WindowStyle = ProcessWindowStyle.Hidden
                    };
                    Process.Start(objPdfProgramProcess);
                }
            }
            catch (Exception ex)
            {
                Program.MainForm.ShowMessageBox(this, ex.ToString());
            }
        }
        private void cmdSaveAsPdf_Click(object sender, EventArgs e)
        {
            // Save the generated output as PDF.
            SaveFileDialog1.Filter = LanguageManager.GetString("DialogFilter_Pdf") + '|' + LanguageManager.GetString("DialogFilter_All");
            SaveFileDialog1.Title  = LanguageManager.GetString("Button_Viewer_SaveAsPdf");
            SaveFileDialog1.ShowDialog();
            string strSaveFile = SaveFileDialog1.FileName;

            if (string.IsNullOrEmpty(strSaveFile))
            {
                return;
            }

            if (!Directory.Exists(Path.GetDirectoryName(strSaveFile)) || !Utils.CanWriteToPath(strSaveFile))
            {
                Program.MainForm.ShowMessageBox(LanguageManager.GetString("Message_File_Cannot_Be_Accessed"));
                return;
            }
            if (File.Exists(strSaveFile))
            {
                try
                {
                    File.Delete(strSaveFile);
                }
                catch (IOException)
                {
                    Program.MainForm.ShowMessageBox(LanguageManager.GetString("Message_File_Cannot_Be_Accessed"));
                    return;
                }
                catch (UnauthorizedAccessException)
                {
                    Program.MainForm.ShowMessageBox(LanguageManager.GetString("Message_File_Cannot_Be_Accessed"));
                    return;
                }
            }

            PdfDocument objPdfDocument = new PdfDocument
            {
                Html = webBrowser1.DocumentText
            };

            objPdfDocument.ExtraParams.Add("encoding", "UTF-8");
            objPdfDocument.ExtraParams.Add("dpi", "300");
            objPdfDocument.ExtraParams.Add("margin-top", "13");
            objPdfDocument.ExtraParams.Add("margin-bottom", "19");
            objPdfDocument.ExtraParams.Add("margin-left", "13");
            objPdfDocument.ExtraParams.Add("margin-right", "13");
            objPdfDocument.ExtraParams.Add("image-quality", "100");
            objPdfDocument.ExtraParams.Add("print-media-type", "");

            try
            {
                PdfConvert.ConvertHtmlToPdf(objPdfDocument, new PdfConvertEnvironment
                {
                    WkHtmlToPdfPath = Path.Combine(Utils.GetStartupPath, "wkhtmltopdf.exe"),
                    Timeout         = 60000,
                    TempFolderPath  = Path.GetTempPath()
                }, new PdfOutput
                {
                    OutputFilePath = strSaveFile
                });

                if (!string.IsNullOrWhiteSpace(GlobalOptions.PDFAppPath))
                {
                    Uri    uriPath   = new Uri(strSaveFile);
                    string strParams = GlobalOptions.PDFParameters;
                    strParams = strParams
                                .Replace("{page}", "1")
                                .Replace("{localpath}", uriPath.LocalPath)
                                .Replace("{absolutepath}", uriPath.AbsolutePath);
                    ProcessStartInfo objPdfProgramProcess = new ProcessStartInfo
                    {
                        FileName    = GlobalOptions.PDFAppPath,
                        Arguments   = strParams,
                        WindowStyle = ProcessWindowStyle.Hidden
                    };
                    Process.Start(objPdfProgramProcess);
                }
            }
            catch (Exception ex)
            {
                Program.MainForm.ShowMessageBox(ex.ToString());
            }
        }
Exemple #3
0
        private async void cmdSaveAsPdf_Click(object sender, EventArgs e)
        {
            using (new CursorWait(this))
            {
                // Check to see if we have any "Print to PDF" printers, as they will be a lot more reliable than wkhtmltopdf
                string strPdfPrinter = string.Empty;
                foreach (string strPrinter in PrinterSettings.InstalledPrinters)
                {
                    if (strPrinter == "Microsoft Print to PDF" || strPrinter == "Foxit Reader PDF Printer" ||
                        strPrinter == "Adobe PDF")
                    {
                        strPdfPrinter = strPrinter;
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(strPdfPrinter))
                {
                    DialogResult ePdfPrinterDialogResult = Program.MainForm.ShowMessageBox(this,
                                                                                           string.Format(GlobalSettings.CultureInfo,
                                                                                                         await LanguageManager.GetStringAsync("Message_Viewer_FoundPDFPrinter"), strPdfPrinter),
                                                                                           await LanguageManager.GetStringAsync("MessageTitle_Viewer_FoundPDFPrinter"),
                                                                                           MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    switch (ePdfPrinterDialogResult)
                    {
                    case DialogResult.Cancel:
                    case DialogResult.Yes when DoPdfPrinterShortcut(strPdfPrinter):
                        return;

                    case DialogResult.Yes:
                        Program.MainForm.ShowMessageBox(this,
                                                        await LanguageManager.GetStringAsync("Message_Viewer_PDFPrinterError"));
                        break;
                    }
                }

                // Save the generated output as PDF.
                SaveFileDialog1.Filter = await LanguageManager.GetStringAsync("DialogFilter_Pdf") + '|' +
                                         await LanguageManager.GetStringAsync("DialogFilter_All");

                SaveFileDialog1.Title = await LanguageManager.GetStringAsync("Button_Viewer_SaveAsPdf");

                SaveFileDialog1.ShowDialog();
                string strSaveFile = SaveFileDialog1.FileName;

                if (string.IsNullOrEmpty(strSaveFile))
                {
                    return;
                }

                if (!strSaveFile.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
                {
                    strSaveFile += ".pdf";
                }

                if (!Directory.Exists(Path.GetDirectoryName(strSaveFile)) || !Utils.CanWriteToPath(strSaveFile))
                {
                    Program.MainForm.ShowMessageBox(this,
                                                    string.Format(GlobalSettings.CultureInfo,
                                                                  await LanguageManager.GetStringAsync(
                                                                      "Message_File_Cannot_Be_Accessed"), strSaveFile));
                    return;
                }

                if (!await Utils.SafeDeleteFileAsync(strSaveFile, true))
                {
                    Program.MainForm.ShowMessageBox(this,
                                                    string.Format(GlobalSettings.CultureInfo,
                                                                  await LanguageManager.GetStringAsync(
                                                                      "Message_File_Cannot_Be_Accessed"), strSaveFile));
                    return;
                }

                // No PDF printer found, let's use wkhtmltopdf

                try
                {
                    PdfDocument objPdfDocument = new PdfDocument
                    {
                        Html        = webViewer.DocumentText,
                        ExtraParams = new Dictionary <string, string>(8)
                        {
                            { "encoding", "UTF-8" },
                            { "dpi", "300" },
                            { "margin-top", "13" },
                            { "margin-bottom", "19" },
                            { "margin-left", "13" },
                            { "margin-right", "13" },
                            { "image-quality", "100" },
                            { "print-media-type", string.Empty }
                        }
                    };
                    PdfConvertEnvironment objPdfConvertEnvironment = new PdfConvertEnvironment
                    {
                        WkHtmlToPdfPath = Path.Combine(Utils.GetStartupPath, "wkhtmltopdf.exe")
                    };
                    PdfOutput objPdfOutput = new PdfOutput {
                        OutputFilePath = strSaveFile
                    };
                    await PdfConvert.ConvertHtmlToPdfAsync(objPdfDocument, objPdfConvertEnvironment, objPdfOutput);

                    if (!string.IsNullOrWhiteSpace(GlobalSettings.PdfAppPath))
                    {
                        Uri    uriPath   = new Uri(strSaveFile);
                        string strParams = GlobalSettings.PdfParameters
                                           .Replace("{page}", "1")
                                           .Replace("{localpath}", uriPath.LocalPath)
                                           .Replace("{absolutepath}", uriPath.AbsolutePath);
                        ProcessStartInfo objPdfProgramProcess = new ProcessStartInfo
                        {
                            FileName    = GlobalSettings.PdfAppPath,
                            Arguments   = strParams,
                            WindowStyle = ProcessWindowStyle.Hidden
                        };
                        objPdfProgramProcess.Start();
                    }
                }
                catch (Exception ex)
                {
                    Program.MainForm.ShowMessageBox(this, ex.ToString());
                }
            }
        }