为什么这个流程文档表总是打印 2 列

发布于 2024-08-23 22:54:59 字数 2305 浏览 4 评论 0原文

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

当我尝试使用以下代码打印时,我总是将页面从中间分成两列(每列包含表格的 3 列)。我尝试了不同的 GridLength 值但没有成功。

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

I have a ListView in my WPF app that is bound to a collection of tasks to perform (A to-do list). I want the user to be able to print their list and have created the following code based on the MSDN guidelines. (This my first foray into printing)

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;
} 

When I try to print with the following code I always have my page split down the middle with 2 columns (Each containing the 3 columns of the table). I have tried different GridLength values but had no success.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-08-30 22:54:59

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

问题在于打印页面的行,而不是流程文档本身。默认情况下,它们打印 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”中找到了这个,我强烈推荐它。

I guess the best way to get an answer is to give up and ask, then you find it yourself.

The issue was in the line to print the pages, not the flowdoc itself. By default they print with 2 columns. The corrected code is (this also deals with the margin and printable area):

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");

}

By the way I found this in Matthew MacDonald's "Pro WPF in C# 2008" which I highly recommend.

最偏执的依靠 2024-08-30 22:54:59

谢谢你的信息。我通过设置列宽来修复它,例如:

flowDoc.ColumnWidth = pageSize.Width

仅供参考,不要尝试从 netframeworkdev 或 .Net Framework Develop 获取帮助,因为他们从来没有好的答案。我希望我的搜索引擎能够将我指向 StackOverflow,而不是那个毫无价值的网站。 StackOverflow 总能找到答案。 :) 再次感谢。

(希望您可以阻止网站在搜索结果中显示,您知道该怎么做,请告诉我。)

Thanks for the info. I fixed it by just setting the columnwidth like:

flowDoc.ColumnWidth = pageSize.Width

FYI don't ever try to get help from netframeworkdev or .Net Framework Develop b/c they never have good answers. I wish my search engine would have pointed me at StackOverflow instead of that worthless site. StackOverflow always has the answers. :) Thanks again.

(Wish you could just block sites from ever showing in your search results, you know how to do that please tell me.)

℡Ms空城旧梦 2024-08-30 22:54:59

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

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

说明:

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

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

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

来自微软文档:
"ColumnWidth=Auto" 导致列宽自动计算为当前 FontSize 的 20 倍。

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

肮脏的事实是。
NaN(默认)表现为“Auto”,对于 FontSize=16,它会转换为 ColumnWidth="320px"。这将导致宽度超过 640 像素

  • 字母宽度的页面出现两列:8.5 英寸 == 816 像素
  • A4 宽度:210 毫米 == 794 像素

,当然,如果页面宽度超过 960 像素,甚至会出现更多列。

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

new System.Windows.LengthConverter().ConvertFromInvariantString("8.5in");
> 816d

Microsoft doc is false/incomplete.

tl;dr: default ColumnWidth resolves to: 320px (for FontSize 16px)

Explanation:

I experienced the default "two column behaviour" too.
My flowdocument:

  • PageWidth="8.5in"
  • ColumnWidth="NaN" (default)

8.5in PageWidth translates to (device inpedendent pixels) PageWidth=816px.

From MS docs:
"ColumnWidth=Auto" Causes column width to be automatically calculated to be 20 times the current FontSize.

Default font size is 16, so: 20 * 16px == 320px.

So the dirty truth is.
NaN (default) behaves as "Auto", which translates to ColumnWidth="320px" for FontSize=16. Which will result in two columns for pages wider than around 640px

  • letter width: 8.5in == 816px
  • A4 width: 210mm == 794px

And of course even more columns if you exceed 960px PageWidth.

How does MS convert physical length to pixels?

new System.Windows.LengthConverter().ConvertFromInvariantString("8.5in");
> 816d
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文