【问题标题】:Print a string object using System.Windows.Controls.PrintDialog使用 System.Windows.Controls.PrintDialog 打印字符串对象
【发布时间】:2018-08-17 03:24:56
【问题描述】:

我的项目需要使用 WPF System.Windows.Controls.PrintDialog 之类的东西将一个简单的字符串对象打印到选择的打印机。

我创建了一个标准 System.Drawing.Printing.PrintDocument,但它没有属于 PrintDialog 类的 PrintDocument() 方法所需的 DocumentPaginator。

将字符串打印到打印机并带有显示打印机选择的对话框似乎是一项微不足道的任务,但结果却要困难得多。请帮忙!

【问题讨论】:

  • 我认为您需要为 PrintDocument msdn.microsoft.com/es-es/library/…987654321@的 printPage 事件添加句柄
  • this 例子怎么样?
  • 该示例显示了 XpsDocument 的打印。你有一个如何从字符串制作 XpsDocument 的例子吗?
  • Henoc 这就是我目前使用 PrintDocument 类进行打印的方式。您向其添加处理程序并提供图形。我无法使用 PrintDialog 和 PrintDocument

标签: c# wpf printing


【解决方案1】:

由于您使用的是 WPF,而不是使用 PrintDocument,因此只需创建一个 FlowDocument。我认为这要容易得多。

var dlg = new PrintDialog();
dlg.PageRangeSelection = PageRangeSelection.AllPages;
dlg.UserPageRangeEnabled = false;
if(dlg.ShowDialog() == true)
{                
    var doc = new FlowDocument();
    // For normal A4&/letter pages, the defaults will print in two columns
    doc.ColumnWidth = dlg.PrintableAreaWidth;
    doc.Blocks.Add(new Paragraph(new Run("My arbitrary long string to print.\nNewlines work. Pages will break as needed.")));
    dlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Simple document");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 2021-12-13
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多