public static void PrintPage(Form caller, string html) { WebBrowser webBrowserForPrinting = new WebBrowser(); webBrowserForPrinting.Size = Size.Empty; webBrowserForPrinting.Visible = false; webBrowserForPrinting.DocumentCompleted += (sender, e) => { webBrowserForPrinting.ShowPrintPreviewDialog(); caller.Controls.Remove(webBrowserForPrinting); }; webBrowserForPrinting.DocumentText = html; caller.Controls.Add(webBrowserForPrinting); }
private void repIn_Click(object sender, EventArgs e) { DataRow row = layoutView1.GetDataRow(layoutView1.FocusedRowHandle); PrintDocument thePrint = new PrintDocument(); PrintDialog print = new PrintDialog(); print.Document = thePrint; thePrint.PrintPage += new PrintPageEventHandler(thePrint_PrintPage); Path_new = FrameworkParams.TEMP_FOLDER + @"\" + row["TEN_FILE"].ToString(); byte[] a = row["NOI_DUNG"] as byte[]; if (a == null || a.Length == 0) return; HelpByte.BytesToFile(a, Path_new); try { pic = Image.FromFile(Path_new); } catch { pic = null; } read = new System.IO.StreamReader(Path_new, System.Text.Encoding.Default, true); #region print word file if (Path.GetExtension(Path_new).ToLower().Equals(".doc") || Path.GetExtension(Path_new).ToLower().Equals(".docx")) { Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); object fileName = Path_new; object nullobj = Missing.Value; wordApp.Visible = true; Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open( ref fileName, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); doc.PrintPreview(); } #endregion #region print excel file else if (Path.GetExtension(Path_new).ToLower().Equals(".xls") || Path.GetExtension(Path_new).ToLower().Equals(".xlsx")) { Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); ExcelApp.Visible = true; ExcelApp.DisplayAlerts = false; Microsoft.Office.Interop.Excel.Workbook WBook = ExcelApp.Workbooks.Open(Path_new, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); object obj = true; WBook.PrintPreview(obj); WBook.Close(false, Missing.Value, Missing.Value); ExcelApp.Quit(); } #endregion #region print .pdf file else if (Path.GetExtension(Path_new).ToLower().Equals(".pdf")) { System.Diagnostics.Process objProcess = new System.Diagnostics.Process(); objProcess.StartInfo.FileName = Path_new; //file to print objProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; objProcess.StartInfo.UseShellExecute = true; objProcess.StartInfo.CreateNoWindow = false; objProcess.StartInfo.ErrorDialog = false; objProcess.StartInfo.Verb = "print"; objProcess.Start(); } #endregion #region print html file else if (Path.GetExtension(Path_new).ToLower().Equals(".htm") || Path.GetExtension(Path_new).ToLower().Equals(".html")) { WebBrowser browser = new WebBrowser(); browser.DocumentText = read.ReadToEnd(); while (browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } browser.Parent = this; browser.ShowPrintPreviewDialog(); browser.Dispose(); } #endregion else if (print.ShowDialog() == DialogResult.OK) { PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = thePrint; ((Form)ppd).WindowState = FormWindowState.Maximized; ppd.ShowDialog(); //thePrint.Print(); } }
public void ShowPrintPreview() { WebBrowser x = new WebBrowser(); x.CreateControl(); x.Visible = false; try { x.Navigate(Url); x.ShowPrintPreviewDialog(); } catch (System.Net.WebException y) { MessageBox.Show(y.Message); } }