/// <summary> /// Prints the specified plot model. /// </summary> /// <param name="model">The model.</param> /// <param name="width">The width (using the actual media width if not specified).</param> /// <param name="height">The height (using the actual media height if not specified).</param> public static void Print(PlotModel model, double width = double.NaN, double height = double.NaN) { PrintDocumentImageableArea area = null; var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area); if (xpsDocumentWriter != null) { if (double.IsNaN(width)) { width = area.MediaSizeWidth; } if (double.IsNaN(height)) { height = area.MediaSizeHeight; } var canvas = new Canvas { Width = width, Height = height }; canvas.Measure(new Size(width, height)); canvas.Arrange(new Rect(0, 0, width, height)); var rc = new ShapesRenderContext(canvas); model.Update(); model.Render(rc, width, height); canvas.UpdateLayout(); xpsDocumentWriter.Write(canvas); } }
public void PrintReport() { try { FlowDocument fd = this.CloneCurrentReportDocument(false); PrintDocumentImageableArea pd_ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref pd_ia); if (docWriter != null && pd_ia != null) { DocumentPaginator d_paginator = ((IDocumentPaginatorSource)fd).DocumentPaginator; d_paginator.PageSize = new Size(pd_ia.MediaSizeWidth, pd_ia.MediaSizeHeight); Thickness pagePadding = fd.PagePadding; fd.PagePadding = new Thickness( Math.Max(pd_ia.OriginWidth, (pagePadding.Left + 29)), Math.Max(pd_ia.OriginHeight, (pagePadding.Top + 29)), Math.Max(pd_ia.MediaSizeWidth - (pd_ia.OriginWidth + pd_ia.ExtentWidth), (pagePadding.Right + 29)), Math.Max(pd_ia.MediaSizeHeight - (pd_ia.OriginHeight + pd_ia.ExtentHeight), (pagePadding.Bottom + 29))); fd.ColumnWidth = double.PositiveInfinity; docWriter.Write(d_paginator); } } catch (Exception) { MessageBox.Show(Application.Current.Resources["EX_ReportPrintFailed"] as string, Application.Current.Resources["Str_ErrorDlgTitle"] as string, MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// https://blogs.msdn.microsoft.com/prajakta/2007/01/02/printing-contents-of-wpf-richtextbox/ /// を参考にした。感謝したい。 /// もう少し余白を空けたい。 /// </summary> private void PrintRichTextContent() { var sourceDocument = new TextRange(richTextBox_Body.Document.ContentStart, richTextBox_Body.Document.ContentEnd); var memstrm = new MemoryStream(); //flowDocumentを複製するために、一度MemoryStreamに書き込んでいる。 sourceDocument.Save(memstrm, DataFormats.XamlPackage); var flowDocumentCopy = new FlowDocument(); var copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd); //MemoryStreamから読み込み copyDocumentRange.Load(memstrm, DataFormats.XamlPackage); PrintDocumentImageableArea ia = null; var docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { var paginator = ((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator; paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); //var pagePadding = flowDocumentCopy.PagePadding; ////pagePaddingの中身がNaNになっている場合があるので、その場合はNaNを0とする。 //if (double.IsNaN(pagePadding.Left)) // pagePadding.Left = 0; //if (double.IsNaN(pagePadding.Top)) // pagePadding.Top = 0; //if (double.IsNaN(pagePadding.Right)) // pagePadding.Right = 0; //if (double.IsNaN(pagePadding.Bottom)) // pagePadding.Bottom = 0; //flowDocumentCopy.PagePadding = new Thickness( // Math.Max(ia.OriginWidth, pagePadding.Left), // Math.Max(ia.OriginHeight, pagePadding.Top), // Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), // Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom) // ); flowDocumentCopy.PagePadding = new Thickness(50, 50, 50, 50); flowDocumentCopy.ColumnWidth = double.PositiveInfinity; docWriter.Write(paginator); } }
/// <summary> /// The print. /// </summary> public void Print() { PrintDocumentImageableArea area = null; XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(ref area); if (xpsdw != null) { xpsdw.Write(this.CreateFixedDocument(new Size(area.ExtentWidth, area.ExtentHeight))); } }
private void DoThePrint(System.Windows.Documents.FlowDocument document) { // Clone the source document's content into a new FlowDocument. // This is because the pagination for the printer needs to be // done differently than the pagination for the displayed page. // We print the copy, rather that the original FlowDocument. MemoryStream s = new MemoryStream(); TextRange source = new TextRange(document.ContentStart, document.ContentEnd); source.Save(s, DataFormats.Xaml); FlowDocument copy = new FlowDocument(); TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd); dest.Load(s, DataFormats.Xaml); string[] address = Address.Split(','); Paragraph price = new Paragraph(new Run("Total Price: " + m_Order.TotalPrice + "€")); price.TextAlignment = TextAlignment.Right; price.Margin = new Thickness(0, 30, 0, 0); Paragraph customerInfo = new Paragraph(new Run(String.Format("{0} {1}\n{2}\n{3}", FirstName, LastName, address[0].Trim(), address[1].Trim()))); customerInfo.Margin = new Thickness(0, 0, 0, 50); copy.Blocks.Add(price); copy.Blocks.InsertBefore(copy.Blocks.FirstBlock, customerInfo); // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. // get information about the dimensions of the seleted printer+media. PrintDocumentImageableArea ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness t = new Thickness(72); // copy.PagePadding; copy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); copy.ColumnWidth = double.PositiveInfinity; // Send content to the printer. docWriter.Write(paginator); } }
private static void Print(FrameworkElement kundeInfo, ScrollViewer scrollViewer) { if (kundeInfo == null && scrollViewer == null) { return; } PrintDocumentImageableArea area = null; PageRangeSelection selection = PageRangeSelection.AllPages; PageRange range = new PageRange(); XpsDocumentWriter xpsdw1 = PrintQueue.CreateXpsDocumentWriter("Corinor prisforslag", ref area, ref selection, ref range); if (xpsdw1 == null) { return; } //TODO: DEBUG //if (File.Exists("D:\\test.xps")) File.Delete("D:\\test.xps"); //XpsDocument _xpsDocument = new XpsDocument("D:\\test.xps", FileAccess.ReadWrite); //XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(_xpsDocument); Thickness Margins = new Thickness(96); double leftMargin = Margins.Left - area.OriginWidth; double topMargin = Margins.Top - area.OriginHeight; double rightMargin = Margins.Right - (area.MediaSizeWidth - area.ExtentWidth - area.OriginWidth); double bottomMargin = Margins.Bottom - (area.MediaSizeHeight - area.ExtentHeight - area.OriginHeight); Size outputSize = new Size( area.MediaSizeWidth - leftMargin - rightMargin, area.MediaSizeHeight - topMargin - bottomMargin); SerializerWriterCollator batchPrinter = xpsdw1.CreateVisualsCollator(); batchPrinter.BeginBatchWrite(); if (kundeInfo != null) { printKundeinfo(batchPrinter, kundeInfo, outputSize, leftMargin, topMargin); } if (scrollViewer != null && scrollViewer.Content != null) { printScrollViewer(batchPrinter, scrollViewer, outputSize, leftMargin, topMargin); } batchPrinter.EndBatchWrite(); //TODO: Debug //_xpsDocument.Close(); }
private static void SendFixedDocumentToPrinter(FixedDocument fixedDocument) { XpsDocumentWriter xpsdw; PrintDocumentImageableArea imgArea = null; //こちらのオーバーロードだと、プリンタ選択ダイアログが出る。 xpsdw = PrintQueue.CreateXpsDocumentWriter(ref imgArea); //var ps = new LocalPrintServer(); //var pq = ps.DefaultPrintQueue; //こちらのオーバーロードだと、プリンタ選択ダイアログを飛ばして既定のプリンタにスプールされる //xpsdw = PrintQueue.CreateXpsDocumentWriter(pq); xpsdw.Write(fixedDocument); }
private void SendFixedDocumentToPrinter(FixedDocument fixedDocument) { XpsDocumentWriter xpsdw; if (pq == null) { PrintDocumentImageableArea imgArea = null; //こちらのオーバーロードだと、プリンタ選択ダイアログが出る。 xpsdw = PrintQueue.CreateXpsDocumentWriter(ref imgArea); } else { xpsdw = PrintQueue.CreateXpsDocumentWriter(pq); } xpsdw.Write(fixedDocument); }
private void Imprimer() { DocumentPaginator Doc = MEP.Paginer(_Doc); // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. // get information about the dimensions of the seleted printer+media. PrintDocumentImageableArea ia = null; System.Windows.Xps.XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { docWriter.Write(Doc); } }
// Print - brings up the print dialogue and starts printing public static bool Print(Project project, Lessee lessee = null, bool overview = false) { PrintDocumentImageableArea area = null; var writer = PrintQueue.CreateXpsDocumentWriter(ref area); var document = CreateDocument(project, lessee, overview); document.PageWidth = area.MediaSizeWidth; document.PageHeight = area.MediaSizeHeight; document.ColumnWidth = document.PageWidth; document.PagePadding = new Thickness(40); IDocumentPaginatorSource source = document; try { writer.WriteAsync(source.DocumentPaginator); } catch (Exception) { return(false); } return(true); }
/// <summary> /// XpsDocumentWriterを使用した印刷を行います。 /// </summary> private void PrintXpsDocument() { var page = new FixedPage(); page.Children.Add(new SampleReport()); var cont = new PageContent(); cont.Child = page; var doc = new FixedDocument(); doc.Pages.Add(cont); PrintDocumentImageableArea area = null; XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area); xps.Write(doc.DocumentPaginator); }
private static bool SendFixedDocumentToPrinter(FixedDocument fixedDocument) { XpsDocumentWriter xpsdw; PrintDocumentImageableArea imgArea = null; xpsdw = PrintQueue.CreateXpsDocumentWriter(ref imgArea); // cancel on printdialog if (xpsdw == null) { return(false); } // out put to the printer xpsdw.Write(fixedDocument); return(true); }
/// <summary> /// Prints the specified plot model. /// </summary> /// <param name="model">The model.</param> public void Print(IPlotModel model) { PrintDocumentImageableArea area = null; var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area); if (xpsDocumentWriter != null) { if (double.IsNaN(this.Width)) { this.Width = area.ExtentWidth; } if (double.IsNaN(this.Height)) { this.Height = area.ExtentHeight; } this.Write(model, xpsDocumentWriter); } }
private void printButton_Click(object sender, RoutedEventArgs e) { try { FlowDocument flowDocumentCopy = new FlowDocument(); TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd); String filePath = String.Empty; SetupFileValidation.FindAgreementFile(ref filePath, this.currentAgreementType); using (FileStream eulaStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { copyDocumentRange.Load(eulaStream, System.Windows.DataFormats.Rtf); eulaStream.Close(); } // Create a XpsDocumentWriter object, open a Windows common print dialog. // This methods returns a ref parameter that represents information about the dimensions of the printer media. PrintDocumentImageableArea ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness pagePadding = flowDocumentCopy.PagePadding; flowDocumentCopy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, pagePadding.Left), Math.Max(ia.OriginHeight, pagePadding.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom)); flowDocumentCopy.ColumnWidth = double.PositiveInfinity; // Send DocumentPaginator to the printer. docWriter.Write(paginator); } } catch (Exception ex) { SetupHelpers.ShowError(ex.Message); } }
private void SendFixedDocumentToPrinter(FixedDocument fixedDocument) { XpsDocumentWriter xpsdw; if (pq == null) { PrintDocumentImageableArea imgArea = null; //こちらのオーバーロードだと、プリンタ選択ダイアログが出る。 xpsdw = PrintQueue.CreateXpsDocumentWriter(ref imgArea); } else { xpsdw = PrintQueue.CreateXpsDocumentWriter(pq); } // これをやらないとMediaSizeが反映されない。参考:https://stackoverflow.com/a/66852287 xpsdw.WritingPrintTicketRequired += (s, e) => { e.CurrentPrintTicket = pq.UserPrintTicket; }; xpsdw.Write(fixedDocument); }
private static void OnPrint(object sender, ExecutedRoutedEventArgs e) { RichTextEditor control = (RichTextEditor)sender; RichTextBox richTextBox = control.RichTextBox; // Serialize RichTextBox content into a stream in Xaml format. Note: XamlPackage format isn't supported in partial trust. TextRange sourceDocument = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); MemoryStream stream = new MemoryStream(); sourceDocument.Save(stream, DataFormats.Xaml); // Clone the source document's content into a new FlowDocument. FlowDocument flowDocumentCopy = new FlowDocument(); TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd); copyDocumentRange.Load(stream, DataFormats.Xaml); // Creates a XpsDocumentWriter object, opens a Windows common print dialog and // returns a ref parameter that represents information about the dimensions of the media. PrintDocumentImageableArea ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness pagePadding = flowDocumentCopy.PagePadding; flowDocumentCopy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, pagePadding.Left), Math.Max(ia.OriginHeight, pagePadding.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom)); flowDocumentCopy.ColumnWidth = double.PositiveInfinity; // Send DocumentPaginator to the printer. docWriter.Write(paginator); } }
/* Written By Jordan Leibman * 12/5/17 * PrintRef_Executed is a refined Printing function that prevents the Editor from halting. * It functions by using the XPS class features and copying the printed document into a new document that is then printed * Reference for pseudocode was pulled from the MSDN forums. * TODO: Full Testing and integration with other features. */ private void PrintRef_Executed(object sender, ExecutedRoutedEventArgs e) { //makes a new text range copy TextRange source = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd); MemoryStream stream = new MemoryStream(); source.Save(stream, DataFormats.Xaml); //saves the copy into new FlowDocument FlowDocument print = new FlowDocument(); TextRange copy = new TextRange(print.ContentStart, print.ContentEnd); copy.Load(stream, DataFormats.Xaml); //Makes a new PrintDocument based on the selected printing size PrintDocumentImageableArea area = null; XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(ref area); if (writer != null && area != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)print).DocumentPaginator; paginator.PageSize = new Size(area.MediaSizeWidth, area.MediaSizeHeight); Thickness padding = print.PagePadding; //sets margins within the printed document print.PagePadding = new Thickness( Math.Max(area.OriginWidth, padding.Left), Math.Max(area.OriginHeight, padding.Top), Math.Max(area.MediaSizeWidth - (area.OriginWidth + area.ExtentWidth), padding.Right), Math.Max(area.MediaSizeWidth - (area.OriginHeight + area.ExtentHeight), padding.Bottom) ); //removes columns from the document print.ColumnWidth = double.PositiveInfinity; //XPS write writer.Write(paginator); } }
private void PrintPlaintext(string text, string docName, int copies) { // Clone the source document's content into a new FlowDocument. FlowDocument flowDocumentCopy = new FlowDocument(); flowDocumentCopy.Blocks.Add(new Paragraph(new Run(docName))); flowDocumentCopy.Blocks.Add(new Paragraph(new Run(text))); // Create a XpsDocumentWriter object, open a Windows common print dialog. // This methods returns a ref parameter that represents information about the dimensions of the printer media. PrintDocumentImageableArea ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter == null || ia == null) { throw new NullReferenceException(); } DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocumentCopy).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness pagePadding = flowDocumentCopy.PagePadding; flowDocumentCopy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, pagePadding.Left), Math.Max(ia.OriginHeight, pagePadding.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom)); flowDocumentCopy.ColumnWidth = double.PositiveInfinity; PrintDialog dialog = new PrintDialog(); for (int i = 0; i < copies; i++) { dialog.PrintDocument(paginator, "asfd"); } }
public void Print(FlowDocument doc) { // Clone the source doc's content into a new FlowDocument. // This is because the pagination for the printer needs to be // done differently than the pagination for the displayed page. // We print the copy, rather that the original FlowDocument. FlowDocument copy = DuplicateDocument(doc); // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, // and allowing the user to select a printer. // get information about the dimensions of the seleted printer+media. PrintDocumentImageableArea ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter == null || ia == null) { return; } DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator; // Change the PageSize and PagePadding for the doc to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); var t = new Thickness(72); // copy.PagePadding; copy.PagePadding = new Thickness( Math.Max(ia.OriginWidth, t.Left), Math.Max(ia.OriginHeight, t.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); copy.ColumnWidth = double.PositiveInfinity; //copy.PageWidth = 528; // allow the page to be the natural with of the output device // Send content to the printer. docWriter.Write(paginator); }
/// <summary> /// Prints the specified plot model. /// </summary> /// <param name="model">The model.</param> public void Print(IPlotModel model) { PrintDocumentImageableArea area = null; var xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ref area); if (xpsDocumentWriter != null) { var width = this.Width; var height = this.Height; if (double.IsNaN(width)) { width = area.MediaSizeWidth; } if (double.IsNaN(height)) { height = area.MediaSizeHeight; } var canvas = new Canvas { Width = width, Height = height, Background = this.Background.ToBrush() }; canvas.Measure(new Size(width, height)); canvas.Arrange(new Rect(0, 0, width, height)); var rc = new ShapesRenderContext(canvas) { TextFormattingMode = this.TextFormattingMode }; model.Update(true); model.Render(rc, width, height); canvas.UpdateLayout(); xpsDocumentWriter.Write(canvas); } }
public void PrintReport() { try { FlowDocument fd = this.CloneCurrentReportDocument(false); PrintDocumentImageableArea pd_ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref pd_ia); if (docWriter != null && pd_ia != null) { DocumentPaginator d_paginator = ((IDocumentPaginatorSource)fd).DocumentPaginator; d_paginator.PageSize = new Size(pd_ia.MediaSizeWidth, pd_ia.MediaSizeHeight); Thickness pagePadding = fd.PagePadding; fd.PagePadding = new Thickness( Math.Max(pd_ia.OriginWidth, pagePadding.Left), Math.Max(pd_ia.OriginHeight, pagePadding.Top), Math.Max(pd_ia.MediaSizeWidth - (pd_ia.OriginWidth + pd_ia.ExtentWidth), pagePadding.Right), Math.Max(pd_ia.MediaSizeHeight - (pd_ia.OriginHeight + pd_ia.ExtentHeight), pagePadding.Bottom)); fd.ColumnWidth = double.PositiveInfinity; docWriter.Write(d_paginator); } } catch (Exception ex) { SiliconStudio.DebugManagers.DebugReporter.Report( SiliconStudio.DebugManagers.MessageType.Error, "EjpControls - Report Editor", "Failed to print Report" + "\nParent Study ID: " + this.ParentStudyId.ToString() + "\nReport ID: " + this._reportObject.Id.ToString() + "\nError: " + ex.Message); MessageBox.Show("印刷する際に失敗しました。", "エラー", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void PrintCmdExecuted(object sender, ExecutedRoutedEventArgs e) { //Printstuff FlowDocument printDoc = new FlowDocument(); printDoc.Blocks.Add(new Paragraph(new Run(stickyWindow.sTextArea.Text))); printDoc.PagePadding = new Thickness(25); TextRange source = new TextRange(printDoc.ContentStart, printDoc.ContentEnd); MemoryStream stream = new MemoryStream(); source.Save(stream, DataFormats.Xaml); // Create a XpsDocumentWriter object, open a Windows common print dialog. // This methods returns a ref parameter that represents information about the dimensions of the printer media. PrintDocumentImageableArea ia = null; XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia); if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)printDoc).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); Thickness pagePadding = printDoc.PagePadding; printDoc.PagePadding = new Thickness( Math.Max(ia.OriginWidth, pagePadding.Left), Math.Max(ia.OriginHeight, pagePadding.Top), Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom)); printDoc.ColumnWidth = double.PositiveInfinity; // Send DocumentPaginator to the printer. docWriter.Write(paginator); } }
void PrintContent(Object sender, RoutedEventArgs args) { //Printing with Paginator //set pagination with printer's margins and size PrintDocumentImageableArea area = null; XpsDocumentWriter xdwriter = PrintQueue.CreateXpsDocumentWriter(ref area); //make a copy to print with pagginator w/o crashing TextPointer position1 = mainRichTextBox.Document.ContentStart; TextPointer position2 = mainRichTextBox.Document.ContentEnd; TextRange sourceDocumentRange = new TextRange(position1, position2); MemoryStream tempstream = new MemoryStream(); sourceDocumentRange.Save(tempstream, System.Windows.DataFormats.Xaml); FlowDocument sourceDocumentCopy = new FlowDocument(); TextPointer position3 = sourceDocumentCopy.ContentStart; TextPointer position4 = sourceDocumentCopy.ContentEnd; TextRange copyDocumentRange = new TextRange(position3, position4); copyDocumentRange.Load(tempstream, System.Windows.DataFormats.Xaml); if ((xdwriter != null) && (area != null)) { DocumentPaginator paginator = ((IDocumentPaginatorSource)sourceDocumentCopy).DocumentPaginator; paginator.PageSize = new Size(area.MediaSizeWidth, area.MediaSizeHeight); Thickness pageBounds = sourceDocumentCopy.PagePadding; double leftmargin, topmargin, rightmargin, bottommargin; if (area.OriginWidth > pageBounds.Left) { leftmargin = area.OriginWidth; } else { leftmargin = pageBounds.Left; } if (area.OriginHeight > pageBounds.Top) { topmargin = area.OriginHeight; } else { topmargin = pageBounds.Top; } double printerRightMargin = area.MediaSizeWidth - (area.OriginWidth + area.ExtentWidth); if (printerRightMargin > pageBounds.Right) { rightmargin = printerRightMargin; } else { rightmargin = pageBounds.Right; } double printerBottomMargin = area.MediaSizeHeight - (area.OriginHeight + area.ExtentHeight); if (printerBottomMargin > pageBounds.Bottom) { bottommargin = printerBottomMargin; } else { bottommargin = pageBounds.Bottom; } sourceDocumentCopy.PagePadding = new Thickness(leftmargin, topmargin, rightmargin, bottommargin); //can be used to set columns sourceDocumentCopy.ColumnWidth = double.PositiveInfinity; xdwriter.Write(paginator); } }