public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
        {
            if (type != KeyType.KeyUp)
            {
                return(false);
            }

            Keys key = (Keys)windowsKeyCode | modifiers.GetKey();

            switch (key)
            {
            case Keys.F5 | Keys.None:
                browser.Reload();
                return(true);

            case Keys.F9 | Keys.None:
            case Keys.F12 | Keys.None:
                browser.ShowDevTools();
                return(true);
            }

            if (key == (Keys.Control | Keys.P))
            {
                var relativePath = String.Format(@".\output-{0}.pdf", Guid.NewGuid().ToString());
                var fullPath     = Path.GetFullPath(relativePath);
                Debug.WriteLine(browser.GetType());
                PdfPrintSettings pdfPrintSettings = new PdfPrintSettings();
                pdfPrintSettings.Landscape          = true;
                pdfPrintSettings.BackgroundsEnabled = true;
                browser.PrintToPdfAsync(fullPath, pdfPrintSettings).ContinueWith(OnPrintToPdfCompleted, fullPath);
                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        private static void PrintPdf()
        {
            Task.Run(async() =>
            {
                var settings = new PdfPrintSettings
                {
                    BackgroundsEnabled  = options.EnableBackgrounds,
                    Landscape           = orientationInScript ? isLandscape : options.IsLandscape,
                    MarginType          = options.MarginType,
                    HeaderFooterEnabled = options.EnableHeaderFooter,
                    HeaderFooterTitle   = options.Title,
                    HeaderFooterUrl     = options.Footer,
                    MarginBottom        = options.MarginBottom,
                    MarginLeft          = options.MarginLeft,
                    MarginRight         = options.MarginRight,
                    MarginTop           = options.MarginTop,
                    PageHeight          = options.PageHeight,
                    PageWidth           = options.PageWidth
                };

                Thread.Sleep(options.Delay);

                await browser.PrintToPdfAsync(options.Output, settings);

                if (options.Png)
                {
                    // generate png
                    var pngFile = Path.Combine(Path.GetDirectoryName(options.Output), Path.GetFileNameWithoutExtension(options.Output) + ".png");
                    browser.Bitmap.Save(pngFile, System.Drawing.Imaging.ImageFormat.Png);
                }

                tokenSource.Cancel();
            });
        }
Exemplo n.º 3
0
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var settings = new PdfPrintSettings();

            settings.Landscape = true;

            _Browser.PrintToPdfAsync("aaa.pdf", settings);
        }
Exemplo n.º 4
0
        private void _acceptButton_Click(object sender, EventArgs e)
        {
            int   horizontal;
            int   vertical;
            float margin;

            if (!int.TryParse(_horizontal.Text, out horizontal))
            {
                MessageBox.Show(this, "Invalid horizontal");
            }
            else if (!int.TryParse(_vertical.Text, out vertical))
            {
                MessageBox.Show(this, "Invalid vertical");
            }
            else if (!float.TryParse(_margin.Text, out margin))
            {
                MessageBox.Show(this, "Invalid margin");
            }
            else
            {
                var settings = new PdfPrintSettings(
                    _viewer.DefaultPrintMode,
                    1.0,
                    new PdfPrintMultiplePages(
                        horizontal,
                        vertical,
                        _horizontalOrientation.Checked ? Orientation.Horizontal : Orientation.Vertical,
                        margin
                        )
                    );

                using (var form = new PrintPreviewDialog())
                {
                    form.Document = _viewer.Document.CreatePrintDocument(settings);
                    form.ShowDialog(this);
                }

                DialogResult = DialogResult.OK;
            }
        }
Exemplo n.º 5
0
        bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
        {
            if ((int)commandId == ShowDevTools)
            {
                browser.ShowDevTools();
            }
            if ((int)commandId == CloseDevTools)
            {
                browser.CloseDevTools();
            }
            if ((int)commandId == SaveImageAs)
            {
                browser.GetHost().StartDownload(parameters.SourceUrl);
            }
            if ((int)commandId == CopyImage)
            {
                // Clipboard.SetImage(parameters.HasImageContents);
            }
            if ((int)commandId == SaveLinkAs)
            {
                browser.GetHost().StartDownload(parameters.LinkUrl);
            }

            if ((int)commandId == CopyLinkAddress)
            {
                Clipboard.SetText(parameters.LinkUrl);
            }


            if ((int)commandId == SaveAsPdf)
            {
                PdfPrintSettings settings = new PdfPrintSettings();
                settings.Landscape          = true;
                settings.BackgroundsEnabled = false;

                browser.PrintToPdfAsync(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\TorBrowser.pdf", settings);
            }
            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Asynchronously prints the current browser contents to the PDF file specified.
        /// The caller is responsible for deleting the file when done.
        /// </summary>
        /// <param name="browser">The ChromiumWebBrowser instance this method extends</param>
        /// <param name="path">Output file location.</param>
        /// <param name="settings">Print Settings.</param>
        /// <returns>A task that represents the asynchronous print operation.
        /// The result is true on success or false on failure to generate the Pdf.</returns>
        public static Task <bool> PrintToPdfAsync(this IWebBrowser browser, string path, PdfPrintSettings settings = null)
        {
            var cefBrowser = browser.GetBrowser();

            ThrowExceptionIfBrowserNull(cefBrowser);

            return(cefBrowser.PrintToPdfAsync(path, settings));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Asynchronously prints the current browser contents to the PDF file specified.
        /// The caller is responsible for deleting the file when done.
        /// </summary>
        /// <param name="cefBrowser">The <see cref="IBrowser"/> object this method extends.</param>
        /// <param name="path">Output file location.</param>
        /// <param name="settings">Print Settings.</param>
        /// <returns>A task that represents the asynchronous print operation.
        /// The result is true on success or false on failure to generate the Pdf.</returns>
        public static Task <bool> PrintToPdfAsync(this IBrowser cefBrowser, string path, PdfPrintSettings settings = null)
        {
            var host = cefBrowser.GetHost();

            ThrowExceptionIfBrowserHostNull(host);

            return(host.PrintToPdfAsync(path, settings));
        }
Exemplo n.º 8
0
 public PrintDocument CreatePrintDocument(PdfPrintSettings settings)
 {
     return(_document.CreatePrintDocument(settings));
 }
Exemplo n.º 9
0
 public PrintDocument CreatePrintDocument(PdfPrintSettings settings)
 {
     throw new NotImplementedException();
 }