You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何简单打印List<object>?示例报错求解决

嘿,我来帮你搞定这个打印List<object>的问题!你遇到的printDlg不包含Document属性的异常,大概率是因为你用的是WPF的PrintDialog(而不是WinForms版本)——这俩框架的打印控件API差别挺大的,WPF里确实没有Document这个属性哦。

下面给你两种最易懂的实现方式,对应不同的项目环境:


方法1:WinForms项目下的标准打印方式

如果你的项目是WinForms,那得确保引用了System.Drawing和System.Windows.Forms组件,正确的步骤应该是这样:

  • 先创建PrintDocument对象,给它绑定PrintPage事件(用来绘制打印内容)
  • 把PrintDocument赋值给PrintDialog的Document属性(WinForms里这个属性是存在的)
  • 弹出打印对话框,用户确认后执行打印

完整代码示例:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

public class PrintListHelper
{
    // 示例List
    private List<object> _myDataList = new List<object> { "草莓", "蓝莓", 789, DateTime.Now };

    public void PrintMyList()
    {
        PrintDialog printDlg = new PrintDialog();
        PrintDocument printDoc = new PrintDocument();
        
        // WinForms中可以正常给Document属性赋值
        printDlg.Document = printDoc;
        // 绑定打印页面的内容绘制事件
        printDoc.PrintPage += DrawListContentOnPrintPage;

        // 弹出打印对话框,用户确认后开始打印
        if (printDlg.ShowDialog() == DialogResult.OK)
        {
            printDoc.Print();
        }
    }

    private void DrawListContentOnPrintPage(object sender, PrintPageEventArgs e)
    {
        int startY = 50; // 打印内容的起始Y坐标
        int lineHeight = 22; // 每行的高度
        Font printFont = new Font("微软雅黑", 12);

        // 遍历List,逐行绘制内容
        foreach (var item in _myDataList)
        {
            // 处理null值,避免报错
            string itemText = item?.ToString() ?? "(空值)";
            e.Graphics.DrawString(itemText, printFont, Brushes.Black, 50, startY);
            startY += lineHeight;
        }
    }
}

方法2:WPF项目下的简单打印方式

WPF的PrintDialog没有Document属性,但我们可以用FlowDocument来承载List内容,再进行打印,步骤更简单:

using System;
using System.Windows;
using System.Windows.Documents;
using System.Collections.Generic;

public class WpfPrintListHelper
{
    // 示例List
    private List<object> _myDataList = new List<object> { "哈士奇", "柯基", 101, DateTime.Now };

    public void PrintMyList()
    {
        PrintDialog printDlg = new PrintDialog();
        
        // 创建一个FlowDocument来存放打印内容
        FlowDocument printDoc = new FlowDocument();
        Paragraph contentPara = new Paragraph();

        // 把List的元素逐个添加到段落中,每行一个元素
        foreach (var item in _myDataList)
        {
            string itemText = item?.ToString() ?? "(空值)";
            contentPara.Inlines.Add(new Run(itemText));
            contentPara.Inlines.Add(new LineBreak()); // 换行
        }

        printDoc.Blocks.Add(contentPara);

        // 设置打印任务名称,弹出对话框
        if (printDlg.ShowDialog() == true)
        {
            printDlg.PrintDocument(((IDocumentPaginatorSource)printDoc).DocumentPaginator, "打印我的List数据");
        }
    }
}

超简化思路:先转成格式化文本再打印

不管是WinForms还是WPF,你都可以先把List转成一个每行一个元素的字符串,再把这个字符串放到打印内容里,比如:

// 把List转成换行分隔的字符串
string formattedContent = string.Join(Environment.NewLine, _myDataList.Select(item => item?.ToString() ?? "(空值)"));
// 之后把这个字符串放到对应的打印载体里即可(WinForms的DrawString,WPF的Run)

简单总结一下:你之前看到的示例是WinForms的代码,如果在WPF项目里直接用就会报错,只要对应自己的项目环境选对方法就行啦!

内容的提问来源于stack exchange,提问作者Max Yotos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 08:08:13