2016-07-05 135 views
1

我通過使用FlowDocument,FlowDocumentPaginator和PrintDialog在WPF中打印純文本。我的做法是基於this article並實現如下:WPF FlowDocument僅打印到小區域

 var printDialog = new PrintDialog(); 
     if (printDialog.ShowDialog() == true) 
     { 
      var flowDocument = new FlowDocument(); 

      var paragraph = new Paragraph(); 
      paragraph.FontFamily = new FontFamily("Courier New"); 
      paragraph.FontSize = 10; 
      paragraph.Margin = new Thickness(0); 
      paragraph.Inlines.Add(new Run(this.textToPrint)); 

      flowDocument.FontSize = 10; 
      flowDocument.Blocks.Add(paragraph); 

      var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator; 
      printDialog.PrintDocument(paginator, "Chit"); 
     } 

這個工程很好的寬度窄印刷的東西。但是,當我嘗試打印一個長字符串,這一切都被塞在一個小區域:

enter image description here

我在打印對話框中的PrintTicket並在分頁程序覈對尺寸,他們似乎是好的:

enter image description here

那麼,是什麼原因導致這個問題,我該如何解決?

+0

我不在工作,但我可以告訴你它正在創建兩列。製作一個長文件,你會看到它。您需要告訴flowdocument它是一列,並告訴flowdocument打印機的寬度。 – Paparazzi

+0

良好的觀察。您能否在您方便的情況下擴展如何在答案中實施您的建議? [編輯:我看你已經做到了。謝謝。] – Gigi

回答

3

這是一些代碼,我用

flowDocument.PagePadding = new Thickness(standardThickness); 
flowDocument.ColumnGap = 0; 
flowDocument.ColumnWidth = printDialog.PrintableAreaWidth; 

你需要告訴FlowDocument的是一列,並告訴FlowDocument的打印機的寬度。

+0

完美,這工作。我只是建議你在回答中添加你在評論中寫的內容(兩列內容),以便人們可以在其中找到所有相關信息。 – Gigi