2011-02-01 70 views
4

我有一個DataTable包含任意數量的列和行,我試圖打印出來。到目前爲止,最好的運氣是將數據放入表中,然後將表添加到FlowDocument中。WPF - FlowDocument - 將表格拉伸至整個寬度?

到目前爲止這麼好。我現在遇到的問題是表格只「想」佔據文檔寬度的大約一半。我已經爲FlowDocument的PageWidth和ColumnWidth屬性設置了適當的值,但是表似乎不想伸縮來填充分配的空間?

+0

我已經結束了使用FixedDocument來獲得這個工作,但如果任何人有使用FlowDocument的一些見解,我仍然喜歡聽到它。 – 2011-02-04 20:12:31

回答

0

我對此有一些好運:How to set the original width of a WPF FlowDocument,儘管它只佔用了大約90%的空間。

+0

實際上,10%未使用的空間是因爲我在FlowDocument中添加了PagePadding =「0.5in」,這是在打印頁面時保持頁面不被切斷的必要條件。 – 2011-04-15 20:41:40

4

爲了將您的FlowDocument內容設置爲完整的可用widh,您必須首先知道頁面的寬度。您需要設置的屬性需要處理內容長度,FlowDocument上的支柱是ColumnWidth

我通常會創建一個「PrintLayout」輔助類來保留Page width/hight和Padding的已知預設。你可以從Word女士嗅探預設並填寫更多內容。

爲PrintLayout

public class PrintLayout 
{ 
    public static readonly PrintLayout A4 = new PrintLayout("29.7cm", "42cm", "3.18cm", "2.54cm"); 
    public static readonly PrintLayout A4Narrow = new PrintLayout("29.7cm", "42cm", "1.27cm", "1.27cm"); 
    public static readonly PrintLayout A4Moderate = new PrintLayout("29.7cm", "42cm", "1.91cm", "2.54cm"); 

    private Size _Size; 
    private Thickness _Margin; 

    public PrintLayout(string w, string h, string leftright, string topbottom) 
     : this(w,h,leftright, topbottom, leftright, topbottom) { 
    } 

    public PrintLayout(string w, string h, string left, string top, string right, string bottom) { 
     var converter = new LengthConverter(); 
     var width = (double)converter.ConvertFromInvariantString(w); 
     var height = (double)converter.ConvertFromInvariantString(h); 
     var marginLeft = (double)converter.ConvertFromInvariantString(left); 
     var marginTop = (double)converter.ConvertFromInvariantString(top); 
     var marginRight = (double)converter.ConvertFromInvariantString(right); 
     var marginBottom = (double)converter.ConvertFromInvariantString(bottom); 
     this._Size = new Size(width, height); 
     this._Margin = new Thickness(marginLeft, marginTop, marginRight, marginBottom); 

    } 


    public Thickness Margin { 
     get { return _Margin; } 
     set { _Margin = value; } 
    } 

    public Size Size { 
     get { return _Size; } 
    } 

    public double ColumnWidth { 
     get { 
      var column = 0.0; 
      column = this.Size.Width - Margin.Left - Margin.Right; 
      return column; 
     } 
    } 
} 

下一個對你的FlowDocument,你可以設置預置

在XAML中

<FlowDocument x:Class="WpfApp.MyPrintoutView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:WpfApp" 
     mc:Ignorable="d" 
     PageHeight="{Binding Height, Source={x:Static local:PrintLayout.A4}}" 
     PageWidth="{Binding Width, Source={x:Static local:PrintLayout.A4}}" 
     PagePadding="{Binding Margin, Source={x:Static local:PrintLayout.A4}}" 
     ColumnWidth="{Binding ColumnWidth, Source={x:Static local:PrintLayout.A4}}" 
     FontFamily="Segoe WP" 
     FontSize="16" ColumnGap="4"> 
     <!-- flow elements --> 
</FlowDocument> 

通過代碼的類

FlowDocument result = new WpfApp.MyPrintoutView(); 
result.PageWidth = PrintLayout.A4.Size.Width; 
result.PageHeight = PrintLayout.A4.Size.Height; 
result.PagePadding = PrintLayout.A4.Margin; 
result.ColumnWidth = PrintLayout.A4.ColumnWidth; 
+1

非常感謝你的幫助。只是一個小小的更正:A4紙尺寸爲21釐米x 29.7釐米,A3爲29.7釐米x 42釐米。 – solarc 2016-06-13 20:47:09