【问题标题】:Set printer tray when printing excel document打印excel文档时设置打印机托盘
【发布时间】:2014-11-10 20:24:14
【问题描述】:

我看过很多关于在 c# 中为 word 文档设置打印机托盘的帖子。我需要 Excel 的解决方案。

如果可能的话,为任何文档提供更好的解决方案。某种方法我可以传递文件路径和托盘。

编辑 到目前为止,我已经尝试了以下方法,但在打印机设置中没有进行任何可见的更改。

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = @"\\localhost\HP-4515n";
var dps = ps.DefaultPageSettings;
dps.PaperSource.RawKind = 260;

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = @"\\localhost\HP-4515n";
PaperSource psrc = new PaperSource();
psrc.RawKind = 260;
psrc.SourceName = "unknown";
dps.PaperSource = psrc;

编辑 2

我正在对 RawKind 进行硬编码,因为纸盘不知何故未显示在纸源中。

目前当我打印例如。 Excel 文档我显示 PrinterDialog,获取所选打印机的名称并将其传递给互操作 Excel 活动打印机属性。但是现在我需要打印大量文档,并且需要以编程方式设置所选打印机及其属性特别是托盘。

【问题讨论】:

标签: c# printing interop


【解决方案1】:

@sysboard,我从MSDN page on the PrinterSettings class 看到 DefaultPageSettings 属性没有 set 方法,只有 get 方法。我不确定这是否可以从外部类访问...您可能会查看PageSettings class,因为它看起来有一个重载的构造函数,允许您传递指定的打印机,并且它在 PaperSource 上有一个 set 方法。

【讨论】:

    【解决方案2】:

    您可以使用以下代码获取可用的文件资源:

    PrintDocument printDoc1 = new PrintDocument();
    List<PaperSource> psList = new List<PaperSource>();
    PaperSource pkSource;
    for (int i = 0; i < printDoc1.PrinterSettings.PaperSources.Count; i++)
    {
        pkSource = printDoc1.PrinterSettings.PaperSources[i];
        psList.Add(pkSource);
    }
    

    现在向用户展示这些选项,并获得关于使用哪种纸张来源的输入,比如第一个,你可以这样做:

    PrintDocument doc = new PrintDocument();
    doc.DefaultPageSettings.PaperSource = psList[0];
    doc.Print();
    

    【讨论】:

      【解决方案3】:

      为什么要硬编码psrc.RawKind = 260; 要为 Paper Source 设置 RawKind,可以使用 PaperSourceKind 枚举。试试下面的代码

      PrintDocument doc = new PrintDocument();
      PaperSource pSource = new PaperSource();
      pSource.RawKind = (int)PaperSourceKind.Middle;
      doc.DefaultPageSettings.PaperSource = pSource;
      

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2014-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-04
        • 1970-01-01
        相关资源
        最近更新 更多