/// <summary> /// Invokes a System.Windows.Controls.PrintDialog to print the TextEditor.Document with specified title. /// </summary> public static void PrintDialog(this TextEditor textEditor, string title, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } settings.DocumentTitle = (title != null) ? title : String.Empty; System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog(); printDialog.PrintQueue = settings.PrintQueue; if (settings.PageSettings.Landscape) { settings.PrintTicket.PageOrientation = PageOrientation.Landscape; } printDialog.PrintTicket = settings.PrintTicket; printDialog.PrintQueue.DefaultPrintTicket.PageOrientation = settings.PrintTicket.PageOrientation; if (printDialog.ShowDialog() == true) { settings.PrintQueue = printDialog.PrintQueue; settings.PrintTicket = printDialog.PrintTicket; printDialog.PrintDocument(CreateDocumentPaginatorToPrint(textEditor, withHighlighting), "PrintJob"); } }
/// <summary> /// Invokes a PrintEngine.PrintPreviewDialog to print preview the TextEditor.Document with specified title. /// </summary> public static void PrintPreviewDialog(this TextEditor textEditor, string title, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } settings.DocumentTitle = (title != null) ? title : String.Empty; PrintEngine.PrintPreviewDialog printPreview = new PrintEngine.PrintPreviewDialog(); printPreview.DocumentViewer.FitToMaxPagesAcross(1); printPreview.DocumentViewer.PrintQueue = settings.PrintQueue; if (settings.PageSettings.Landscape) { settings.PrintTicket.PageOrientation = PageOrientation.Landscape; } printPreview.DocumentViewer.PrintTicket = settings.PrintTicket; printPreview.DocumentViewer.PrintQueue.DefaultPrintTicket.PageOrientation = settings.PrintTicket.PageOrientation; printPreview.LoadDocument(CreateDocumentPaginatorToPrint(textEditor, withHighlighting)); // this is stupid, but must be done to view a whole page: DocumentViewer.FitToMaxPagesAcrossCommand.Execute("1", printPreview.DocumentViewer); // we never get a return code 'true', since we keep the DocumentViewer open, until user closes the window printPreview.ShowDialog(); settings.PrintQueue = printPreview.DocumentViewer.PrintQueue; settings.PrintTicket = printPreview.DocumentViewer.PrintTicket; }
/// <summary> /// Creates a DocumentPaginatorWrapper from TextEditor text to print. /// </summary> static DocumentPaginatorWrapper CreateDocumentPaginatorToPrint(TextEditor textEditor, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } // this baby adds headers and footers IDocumentPaginatorSource dps = CreateFlowDocumentToPrint(textEditor, withHighlighting); DocumentPaginatorWrapper dpw = new DocumentPaginatorWrapper(dps.DocumentPaginator, settings.PageSettings, settings.PrintTicket, textEditor.FontFamily); dpw.Title = settings.DocumentTitle; return(dpw); }
/// <summary> /// Invokes a Windows.Forms.PrintPreviewDialog. /// </summary> public static void PageSetupDialog(this TextEditor textEditor) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } settings.PageSettings.Landscape = (settings.PrintTicket.PageOrientation == PageOrientation.Landscape); PageSetupDialog setup = new PageSetupDialog(); setup.EnableMetric = true; setup.PageSettings = settings.PageSettings; if (setup.ShowDialog() == DialogResult.OK) { settings.PageSettings = setup.PageSettings; settings.PrintTicket.PageOrientation = (settings.PageSettings.Landscape ? PageOrientation.Landscape : PageOrientation.Portrait); settings.PrintTicket.PageMediaSize = ConvertPaperSizeToMediaSize(settings.PageSettings.PaperSize); } }
/// <summary> /// Creates a FlowDocument from TextEditor text to print. /// </summary> static FlowDocument CreateFlowDocumentToPrint(TextEditor textEditor, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } // this baby has all settings to be printed or previewed in the PrintEngine.PrintPreviewDialog FlowDocument doc = CreateFlowDocumentForEditor(textEditor, withHighlighting); doc.ColumnWidth = settings.PageSettings.PrintableArea.Width; doc.PageHeight = (settings.PageSettings.Landscape ? (int)settings.PrintTicket.PageMediaSize.Width : (int)settings.PrintTicket.PageMediaSize.Height); doc.PageWidth = (settings.PageSettings.Landscape ? (int)settings.PrintTicket.PageMediaSize.Height : (int)settings.PrintTicket.PageMediaSize.Width); doc.PagePadding = ConvertPageMarginsToThickness(settings.PageSettings.Margins); doc.FontFamily = textEditor.FontFamily; doc.FontSize = textEditor.FontSize; return(doc); }
/// <summary> /// Creates a FlowDocument from TextEditor text to print. /// </summary> static FlowDocument CreateFlowDocumentToPrint(TextEditor textEditor, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } // this baby has all settings to be printed or previewed in the PrintEngine.PrintPreviewDialog FlowDocument doc = CreateFlowDocumentForEditor(textEditor, withHighlighting); doc.ColumnWidth = settings.PageSettings.PrintableArea.Width; doc.PageHeight = (settings.PageSettings.Landscape ? (int)settings.PrintTicket.PageMediaSize.Width : (int)settings.PrintTicket.PageMediaSize.Height); doc.PageWidth = (settings.PageSettings.Landscape ? (int)settings.PrintTicket.PageMediaSize.Height : (int)settings.PrintTicket.PageMediaSize.Width); doc.PagePadding = ConvertPageMarginsToThickness(settings.PageSettings.Margins); doc.FontFamily = textEditor.FontFamily; doc.FontSize = textEditor.FontSize; return doc; }
/// <summary> /// Creates a DocumentPaginatorWrapper from TextEditor text to print. /// </summary> static DocumentPaginatorWrapper CreateDocumentPaginatorToPrint(TextEditor textEditor, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } // this baby adds headers and footers IDocumentPaginatorSource dps = CreateFlowDocumentToPrint(textEditor, withHighlighting); DocumentPaginatorWrapper dpw = new DocumentPaginatorWrapper(dps.DocumentPaginator, settings.PageSettings, settings.PrintTicket, textEditor.FontFamily); dpw.Title = settings.DocumentTitle; return dpw; }
/// <summary> /// Prints the the TextEditor.Document to the current printer (no dialogs) with specified title. /// </summary> public static void PrintDirect(this TextEditor textEditor, string title, bool withHighlighting) { PrintSettings settings = textEditor.Tag as PrintSettings; if (settings == null) { settings = new PrintSettings(); textEditor.Tag = settings; } settings.DocumentTitle = (title != null) ? title : String.Empty; System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog(); printDialog.PrintQueue = settings.PrintQueue; if (settings.PageSettings.Landscape) { settings.PrintTicket.PageOrientation = PageOrientation.Landscape; } printDialog.PrintTicket = settings.PrintTicket; printDialog.PrintQueue.DefaultPrintTicket.PageOrientation = settings.PrintTicket.PageOrientation; printDialog.PrintDocument(CreateDocumentPaginatorToPrint(textEditor, withHighlighting), "PrintDirectJob"); }