【问题标题】:.NET find paper tray size.NET 查找纸盘尺寸
【发布时间】:2011-10-06 06:28:21
【问题描述】:

有没有办法找到打印机上的所有打印托盘并获取它们的纸张尺寸,例如

Tray1 = A4
Tray2 = A3
Tray3 = Letter

我可以看到一种使用类似方法获取托盘的方法,但它似乎没有纸张尺寸信息。

using (System.Drawing.Printing.PrintDocument doc = new PrintDocument())
{
    foreach (System.Drawing.Printing.PaperSource paperSource in doc.PrinterSettings.PaperSources)
    {
        string trayName = paperSource.SourceName;
    }
}

我想要做的是默认为打印作业选择最佳托盘。

【问题讨论】:

  • 是网页应用还是WPF?
  • 只是在想.. 如果您只是在寻找最适合打印作业的纸盘,为什么不直接使用最大的纸盘并指定纸张尺寸??
  • @opatachibueze 我不确定你的意思。如果我有一个 A4 文档,我想打印到已经有 A4 纸的托盘。如果它是 A3 文档打印到 A3 托盘。
  • 好的,你看看这两个链接了吗? DeviceCapabilities API 函数应该能够解决您的问题。你也可以看看这个链接:planet-source-code.com/vb/scripts/…

标签: .net printing


【解决方案1】:
  1. 您可以使用 windows api 获取打印机属性,其中包括打印机的纸盒(托盘): http://msdn.microsoft.com/en-us/library/bb258176(v=office.12).aspx http://www.thedbcommunity.com/index.php?option=com_content&task=view&id=218&Itemid=56

  2. 您可以在 ArcObjects SDK 10 Microsoft.NET Framework 中使用 IPaper 类(我不太确定它是如何工作的) http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000002wn000000

编辑:刚刚深入查看了我给你的一个链接,这应该是你正在寻找的答案:

// Get Printer Tray Codes for Word Printing using DeviceCapabilities
// By: BB
//
// Inputs:1)strTrayName - Name of the tray to search for, ie "Tray 1", "Tray 2", "Manual"

        /// <summary>
        /// Gets the printer code for the specified tray, based on the current printer for the Word Document
        /// Printer trays on various HP printers change between models and Word documents 
        /// will not act the same when printed on different printers. This allows us
        /// to get the right code for the tray we want to print to.
        /// </summary>
        /// <param name="strTrayName">Name of the tray to search for, ie "Tray 1", "Tray 2", "Manual"</param>
        /// <param name="iDefaultBin">A default bin to return if not found ie. (int)WdPaperTray.wdPrinterUpperBin</param>
        /// <param name="wordDoc">A word document object that we want to check the printer for</param>
        /// <returns></returns>
        public int GetBinNumber(string strTrayName, int iDefaultBin, Word.Document wordDoc)
        {
            //Code adapted from Microsoft KB article Q194789
            //HOWTO: Determine Available PaperBins with DeviceCapabilities API
            //Get the printer & port names and numbers of the current printer
            // 
            // Once we have the printer bin we can then use 
            // InvokeMember on the wordDoc.PageSetup object to set the bin.
            //
            // Example call to this method is 
            // int Tray1Code = GetBinNumber("Tray 1",(int)WdPaperTray.wdPrinterUpperBin);
            int BinRes;
            BinRes = iDefaultBin; //set up a default bin as the lower
            try
            {
                string sPort = wordDoc.Application.ActivePrinter.Substring(wordDoc.Application.ActivePrinter.IndexOf(" on ") + 3).Trim();
                string sCurrentPrinter = wordDoc.Application.ActivePrinter.Substring(0, wordDoc.Application.ActivePrinter.IndexOf(" on ")).Trim();
                //'Find out how many printer bins there are
                Int32 iBinCnt = DeviceCapabilities(sCurrentPrinter, sPort, DC_BINS, null, IntPtr.Zero);
                if (iBinCnt > -1)
                {
                    //'Set the array of bin numbers to the right size
                    UInt16[] iBinArray = new UInt16[iBinCnt];
                    //set up a buffer to receive the bin names - each name is up to 24 chars, null terminated
                    byte[] buffer = new byte[iBinCnt * 24];
                    // Load the array with the bin numbers
                    iBinCnt = DeviceCapabilitiesA(sCurrentPrinter, sPort, DC_BINS, iBinArray, IntPtr.Zero);
                    if (iBinCnt > -1)
                    {
                        // Load the array of bin names
                        iBinCnt = DeviceCapabilitiesA(sCurrentPrinter, sPort, DC_BINNAMES, buffer, IntPtr.Zero);
                        if (iBinCnt > -1)
                        {
                            string sBinNames = Encoding.ASCII.GetString(buffer, 0, buffer.Length);
                            //split the null terminated strings into a string array for searching
                            while (sBinNames.IndexOf("\0\0") > -1) 
                            {
                                sBinNames = sBinNames.Replace("\0\0", "\0");
                            }
                            string[] arrBinNames = sBinNames.Split('\0');
                            //System.Diagnostics.Debug.WriteLine("prn nams = " + res );
                            int idx = 0;
                            foreach (string BinNam in arrBinNames)
                            {
                                if( BinNam.Trim().StartsWith(strTrayName))
                                {
                                    BinRes = iBinArray[idx];
                                    break;
                                }
                                idx++;
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error - " + ex.Message );
            }
            return BinRes;
        }

由于限制,部分从网站复制,您可以在此处查看完整代码:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3687&lngWId=10

【讨论】:

    【解决方案2】:

    从 MSDN 检查此代码。你试过了吗?也许您可以将 PaperSize 名称与托盘名称匹配。

       // Add list of supported paper sizes found on the printer. 
        // The DisplayMember property is used to identify the property that will provide the display string.
        comboPaperSize.DisplayMember = "PaperName";
    
        PaperSize pkSize;
        for (int i = 0; i < printDoc.PrinterSettings.PaperSizes.Count; i++){
            pkSize = printDoc.PrinterSettings.PaperSizes[i];
            comboPaperSize.Items.Add(pkSize);
        }
    
        // Create a PaperSize and specify the custom paper size through the constructor and add to combobox.
        PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200);
    
        comboPaperSize.Items.Add(pkCustomSize1);
    

    【讨论】:

    • 你知道comboPaperSize实际上是什么吗?
    【解决方案3】:

    我认为 C# 中更简单的方法是查询 PrintDocument 打印机设置。

    PrintDocument printDoc = new PrintDocument();
    PrinterSettings printSettings = printDoc.PrinterSettings;
    
    for (int i=0; i < printSettings.PaperSources.Count; i++)
    {
        sTmp += printSettings.PaperSources[i].SourceName;
        sTmp += " : ";
        sTmp += printSettings.PaperSources[i].Kind.ToString();
        sTmp += " : ";
        sTmp += printSettings.PaperSources[i].RawKind.ToString();
        sTmp += "\n";
    }
    
    MessageBox.Show(sTmp, "Paper Sources for " + printSettings.PrinterName+ " : " + 
                        printSettings.IsDefaultPrinter.ToString());
    

    如果我在 SourceName 中查找“Tray 2”并使用 RawKind,我会得到与上面的 GetBinNumber 相同的答案,因为必须导入 winspool.drv。

    我错过了什么吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2017-01-27
      • 2011-02-14
      • 2019-06-06
      相关资源
      最近更新 更多