.NET查找纸盘大小

.NET查找纸盘大小,.net,printing,.net,Printing,是否有办法查找打印机上的所有打印托盘并获取其纸张大小,例如 Tray1 = A4 Tray2 = A3 Tray3 = Letter 我可以用这样的方法来获取纸盘,但它似乎没有纸张大小信息 using (System.Drawing.Printing.PrintDocument doc = new PrintDocument()) { foreach (System.Drawing.Printing.PaperSource paperSource in doc.PrinterSetti

是否有办法查找打印机上的所有打印托盘并获取其纸张大小,例如

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;
    }
}
我想做的是在默认情况下为打印作业选择最好的纸盘

  • 您可以使用windows api获取打印机属性,其中包括打印机的纸盒(纸盘): http://www.thedbcommunity.com/index.php?option=com_content&task=view&id=218&Itemid=56

  • 您可以在ArcObjects SDK 10 Microsoft.NET Framework中使用IPaper类(我不太清楚它是如何工作的)

  • 编辑:刚刚深入查看了我给你的一个链接,这应该是你想要的答案:

    // 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;
            }
    
    //使用DeviceCapability获取用于字打印的打印机托盘代码
    //作者:BB
    //
    //输入:1)strTrayName-要搜索的托盘名称,即“托盘1”、“托盘2”、“手动”
    /// 
    ///根据Word文档的当前打印机,获取指定纸盘的打印机代码
    ///各种HP打印机上的打印机托盘在型号和Word文档之间变化
    ///在不同的打印机上打印时,将不会执行相同的操作。这让我们
    ///为要打印到的纸盘获取正确的代码。
    /// 
    ///要搜索的信盒名称,即“信盒1”、“信盒2”、“手册”
    ///未找到时要返回的默认存储箱,即(int)WdPaperTray.wdPrinterUpperBin
    ///要检查打印机的word文档对象
    /// 
    public int GetBinNumber(字符串strTrayName、int iDefaultBin、Word.Document wordDoc)
    {
    //代码改编自Microsoft知识库文章Q194789
    //如何:使用DeviceCapabilities API确定可用纸盒
    //获取打印机&当前打印机的端口名称和编号
    // 
    //一旦我们有了打印机箱,我们就可以使用了
    //在wordDoc.PageSetup对象上调用Member以设置bin。
    //
    //此方法的示例调用为
    //int Tray1Code=GetBinNumber(“纸盘1”,(int)WdPaperTray.wdPrinterUpperBin);
    int BinRes;
    BinRes=iDefaultBin;//将默认bin设置为较低的
    尝试
    {
    字符串sPort=wordDoc.Application.ActivePrinter.Substring(wordDoc.Application.ActivePrinter.IndexOf(“on”)+3.Trim();
    字符串sCurrentPrinter=wordDoc.Application.ActivePrinter.Substring(0,wordDoc.Application.ActivePrinter.IndexOf(“on”).Trim();
    //'找出有多少个打印机箱
    Int32 iBinCnt=设备容量(sCurrentPrinter、sPort、DC_BINS、null、IntPtr.Zero);
    如果(iBinCnt>-1)
    {
    //'将仓位编号数组设置为正确的大小
    UInt16[]iBinArray=新UInt16[iBinCnt];
    //设置缓冲区以接收bin名称-每个名称最多24个字符,以null结尾
    字节[]缓冲区=新字节[iBinCnt*24];
    //将存储箱编号加载到阵列中
    iBinCnt=设备容量A(当前打印机、运动、DC_箱、iBinArray、IntPtr.Zero);
    如果(iBinCnt>-1)
    {
    //加载bin名称数组
    iBinCnt=设备容量A(当前打印机、运动、DC_BINNAMES、缓冲区、IntPtr.Zero);
    如果(iBinCnt>-1)
    {
    string sBinNames=Encoding.ASCII.GetString(buffer,0,buffer.Length);
    //将以null结尾的字符串拆分为字符串数组以进行搜索
    while(sBinNames.IndexOf(“\0\0”)>-1)
    {
    sBinNames=sBinNames.Replace(“\0\0”和“\0”);
    }
    字符串[]arrBinNames=sBinNames.Split('\0');
    //System.Diagnostics.Debug.WriteLine(“prn nams=“+res”);
    int-idx=0;
    foreach(arrBinNames中的字符串BinNam)
    {
    if(BinNam.Trim().StartsWith(strTrayName))
    {
    BinRes=iBinArray[idx];
    打破
    }
    idx++;
    }
    }
    }
    }
    }
    捕获(例外情况除外)
    {
    System.Diagnostics.Debug.WriteLine(“错误-”+ex.消息);
    }
    返回BinRes;
    }
    
    由于受到限制,部分复制自网站,您可以在此处查看完整代码:


    从MSDN检查此代码。你试过了吗?可能您可以将纸张尺寸名称与纸盘名称匹配

       // 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);
    
    //添加打印机上支持的纸张尺寸列表。
    //DisplayMember属性用于标识将提供显示字符串的属性。
    comboPaperSize.DisplayMember=“PaperName”;
    纸张尺寸;
    对于(int i=0;i
    我认为在C#中更简单的方法是查询打印文档打印机设置

    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());
    
    PrintDocument printDoc=新的PrintDocument();
    PrinterSettings打印设置=printDoc.PrinterSettings;
    对于(int i=0;i