15

我的 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

我想获得答案的最好方法是放弃并问,然后你自己找到它。

问题在于打印页面,而不是 flowdoc 本身。默认情况下,它们打印 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 的“C# 2008 中的 Pro WPF”中发现了这一点,我强烈推荐。

于 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 回答
0

Microsoft 文档是错误的/不完整的。

tl;dr:默认 ColumnWidth 解析为:320px(对于 FontSize 16px)

解释:

我也经历了默认的“两列行为”。我的流程文档:

  • PageWidth="8.5in"
  • ColumnWidth="NaN"(默认)

8.5in PageWidth 转换为 (设备独立像素) PageWidth=816px

来自 MS 文档: "ColumnWidth=Auto" Causes column width to be automatically calculated to be 20 times the current FontSize.

默认字体大小为 16,因此:20 * 16px == 320px.

所以肮脏的事实是。NaN(默认)表现为“自动”,转换为ColumnWidth="320px"for FontSize=16。这将导致页面宽度超过 640 像素的两列

  • 字母宽度:8.5in == 816px
  • A4宽度:210mm == 794px

如果页面宽度超过 960 像素,当然还有更多列。

MS如何将物理长度转换为像素?

new System.Windows.LengthConverter().ConvertFromInvariantString("8.5in");
> 816d
于 2021-01-15T22:24:09.917 回答