2011-08-03 40 views
0

我試圖打印RichTextBox的內容,包括裏面的Adorner圖層。 我使用此代碼打印用Adorner圖層打印RichTextBox內容的問題

 double w = Editor.ExtentWidth; // Editor is the RichTextBox 
     double h = Editor.ExtentHeight; 

     LocalPrintServer ps = new LocalPrintServer(); 
     PrintQueue pq = ps.DefaultPrintQueue; 
     XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(pq); 
     PrintTicket pt = pq.UserPrintTicket; 
     if (xpsdw != null) 
     { 
      pt.PageOrientation = PageOrientation.Portrait; 
      PageMediaSize pageMediaSize = new PageMediaSize(w, h); 
      pt.PageMediaSize = pageMediaSize; 

      xpsdw.Write(Editor); 
     } 

我面臨的問題是,這種代碼只打印顯示在屏幕,而不是編輯器的全部內容上可見的內容。

EDIT圖片是裝飾器層,如果我打印使用上述方法,只打印畫面而不是整個文件上的可見部分。

enter image description here

編輯

我想單獨打印每一個頁面,但做了之後Editor.PageDown();我不能強迫Editor.InvalidateVisual();有沒有辦法,我可以做到這一點在我的方法是什麼?

+0

昨天你不是問這個嗎? – LarsTech

+0

@LarsTech這涵蓋了一個不同的問題。 – raym0nd

+0

你有沒有試過調用xpsdw.Write(Editor.Document)? –

回答

0

我沒有找到任何可以解決此問題的方法,因此我試圖將所有裝飾圖層轉換爲實際圖像。一旦我獲得100%的工作解決方案,我會更新這個問題。

0

我發現這個代碼here

// Serialize RichTextBox content into a stream in Xaml or XamlPackage 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); 

// 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); 
} 
+0

它不打印圖像:( – raym0nd

+0

我用'DataFormats.XamlPackage',但它沒有打印裝飾層 – raym0nd

0

裝飾器層被繪製定向。所以剩下的一個選擇就是將整個RichTextBox轉換爲一張圖形並將其作爲XPS中的圖像進行打印。

儘管這會帶來多種問題...

  1. 將打印佔用或RichTextBox中即編輯器工具欄佔據的(如果是富文本框控件模板的一部分的外部和內部的內容),內部滾動條等。
  2. 如果有滾動條,滾動條的內容將不會被打印,因爲圖像將成爲文本框的精確「快照」(留下的文本被srollbars剪輯)。

你會對此感到滿意嗎?

+0

我該怎麼做選項1? – raym0nd

+0

看到這個創建一個WPF UI控件的快照(在你的情況下,RichTextBox) ... http://mcleodsean.wordpress.com/2008/10/07/bitmap-snapshots-of-wpf-visuals/ –

+0

這隻打印可見內容:(無論如何。 – raym0nd

1

當控件在裝飾圖層上繪圖時,它們搜索樹直到找到裝飾圖層。通常情況下,這是一個窗口級別。在某些情況下,您需要更靠近控件的裝飾層,或直接圍繞控件。在這種情況下,用一個<AdornerDecorator><RichTextBox /></AdornerDecorator>

包裝控件在你的情況下,你可能想要傳遞一個adorner裝飾器的父元素,或者裝飾器本身到打印邏輯。這樣印刷邏輯將包括裝飾層作爲視覺的一部分。也許是這樣的:

<Grid Name="EditorWrapper"> 
    <AdornerDecorator> 
     <RichTextBox /> 
    </AdornerDecorator> 
</Grid> 

然後,通過 「EditorWrapper」 到打印邏輯。

編輯

如果你只想打印RichTextBox的內容,那麼你可能是最好使用內置的FlowDocument的分頁功能。 FlowDocument實現了IDocumentPaginatorSource,它將返回一個可以打印文檔的分頁程序。將該分頁器傳遞給XpsDocumentWriter,它應該正確地轉儲內容。

var doc = Editor.Document; 
var src = doc as IDocumentPaginatorSource; 
var pag = src.DocumentPaginator; 
xpsdw.Write(pag); 
+0

我做了這個' < RichTextBox Name =「Editor」/> ' and I used [this](http://stackoverflow.com/questions/6931526/problem-with-printing-content-of-richtextbox-with-adorner-圖層/ 6931772#6931772)代碼打印,但它沒有工作 – raym0nd

+0

如何將EditorWrapper傳遞給打印邏輯? :s – raym0nd

+0

我也嘗試了上面發佈的代碼並傳遞'xpsdw.Write(EditorWrapper);'但它只在屏幕上打印可見內容。 – raym0nd