15

実行するタスクのコレクション (To Do リスト) にバインドされている WPF アプリに ListView があります。ユーザーがリストを印刷できるようにして、MSDN ガイドラインに基づいて次のコードを作成できるようにします。(これが私の最初の印刷への進出です)

public FlowDocument GetPrintDocument()
{
    FlowDocument flowDoc = new FlowDocument();
    Table table = new Table();

    int numColumns = 3;

    flowDoc.Blocks.Add(table);

    for(int x=0;x<numColumns;x++)
    {
        table.Columns.Add(new TableColumn());
    }
    GridLengthConverter glc = new GridLengthConverter();
    table.Columns[0].Width = (GridLength)glc.ConvertFromString("300");
    table.Columns[1].Width = (GridLength)glc.ConvertFromString("50");
    table.Columns[2].Width = (GridLength)glc.ConvertFromString("50");

    table.RowGroups.Add(new TableRowGroup());

    table.RowGroups[0].Rows.Add(new TableRow());
    // store current working row for reference
    TableRow currentRow = table.RowGroups[0].Rows[0];

    currentRow.FontSize = 16;
    currentRow.FontWeight = FontWeights.Bold;

    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Subject"))));
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Due Date"))));
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Urgency"))));

    for (int i = 1; i < issues.Count+1; i++)
    {
        table.RowGroups[0].Rows.Add(new TableRow());
        currentRow = table.RowGroups[0].Rows[i];
        currentRow.FontSize = 12;
        currentRow.FontWeight = FontWeights.Normal;

        currentRow.Cells.Add(new TableCell
                            (new Paragraph
                            (new Run
                            (issues[i - 1].IssSubject))));
        currentRow.Cells.Add(new TableCell
                            (new Paragraph
                            (new Run
                            (issues[i - 1].IssDueDate.Date.ToString()))));
        currentRow.Cells.Add(new TableCell
                            (new Paragraph
                            (new Run
                            (issues[i - 1].IssUrgency.ToString()))));
    }
    return flowDoc;
} 

次のコードで印刷しようとすると、常にページが中央で 2 列に分割されます (それぞれにテーブルの 3 列が含まれます)。さまざまな GridLength 値を試しましたが、成功しませんでした。

printDialog.PrintDocument(((IDocumentPaginatorSource)StatusBoardViewModel
               .GetPrintDocument())
               .DocumentPaginator 
            ,"Flow Document Print Job");
4

3 に答える 3

20

答えを得る最善の方法は、あきらめて尋ねることだと思います。

問題は、フロードキュメント自体ではなく、ページを印刷する行にありました。デフォルトでは、2 列で印刷されます。修正されたコードは次のとおりです (これは余白と印刷可能領域も扱います)。

PrintDialog printDialog = new PrintDialog();

if (printDialog.ShowDialog() == true)
{

    FlowDocument flowDoc = statusBoardViewModel.GetPrintDocument();

    flowDoc.PageHeight = printDialog.PrintableAreaHeight;
    flowDoc.PageWidth = printDialog.PrintableAreaWidth;
    flowDoc.PagePadding = new Thickness(25);

    flowDoc.ColumnGap = 0;

    flowDoc.ColumnWidth = (flowDoc.PageWidth - 
                           flowDoc.ColumnGap - 
                           flowDoc.PagePadding.Left -  
                           flowDoc.PagePadding.Right);

    printDialog.PrintDocument(((IDocumentPaginatorSource)flowDoc)
                             .DocumentPaginator,
                             "Task Manager Print Job");

}

ちなみに、これは Matthew MacDonald の「Pro WPF in C# 2008」で見つけたので、強くお勧めします。

于 2010-03-01T21:48:43.397 に答える
3

情報をありがとう。次のように列幅を設定するだけで修正しました。

flowDoc.ColumnWidth = pageSize.Width

参考までに、netframeworkdevまたは.Net Framework Develop b / cから助けを得ようとはしませんが、良い答えは得られません。私の検索エンジンが、その価値のないサイトではなく、StackOverflowを指し示していたらよかったのにと思います。StackOverflowには常に答えがあります。:) 再度、感謝します。

(サイトが検索結果に表示されないようにブロックできれば、その方法を教えてください。)

于 2010-11-29T21:03:18.280 に答える