7

印刷しようとしている任意の数の列と行を含む DataTable があります。これまでのところ、データを Table に入れ、そのテーブルを FlowDocument に追加するのが一番の幸運でした。

ここまでは順調ですね。私が今抱えている問題は、テーブルがドキュメントの幅の約半分しか占有したくないということです。FlowDocument の PageWidth プロパティと ColumnWidth プロパティに適切な値を設定しましたが、割り当てられたスペースを埋めるためにテーブルが引き伸ばされないようです。

4

2 に答える 2

5

FlowDocument コンテンツを利用可能な全幅に設定するには、最初にページの幅を知る必要があります。コンテンツの長さを処理するために設定する必要があるプロパティは、FlowDocument のColumnWidthプロパティです。

通常、「PrintLayout」ヘルパー クラスを作成して、ページの幅/高さとパディングの既知のプリセットを保持します。Ms 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;
于 2012-07-20T17:37:22.427 に答える
0

私はこれで運が良かった: How to set the original width of a WPF FlowDocument、スペースの約90%しか占めていませんでした。

于 2011-04-15T20:33:24.873 に答える