打印系统开发(18)——C# 直接打印指定路径文件 + 可选择指定打印机


        public void PrinteTicketWithPath(string path)
        {
                try 
            {
                streamToPrint = new StreamReader (path);
                try 
                {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    if (core.PrinterTicket != null)
                    {
                        pd.PrinterSettings.PrinterName = core.PrinterTicket;
                    }
                    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                    // Print the document.
                    pd.Print();
                } 
                finally 
                {
                    streamToPrint.Close() ;
                }
            } 
            catch(Exception ex) 
            { 
                MessageBox.Show(ex.Message);
            }
        }
 
        // The PrintPage event is raised for each page to be printed.
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            String line = null;
 
            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
                printFont.GetHeight(ev.Graphics);
 
            // Iterate over the file, printing each line.
            while (count < linesPerPage &&
                ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                    leftMargin, yPos, new StringFormat());
                count++;
            }
 
            // If more lines exist, print another page.
            if (line != null)
            {
                ev.HasMorePages = true;
            }
            else
            {
                ev.HasMorePages = false;
            } 
        }

core 是个获取本地属性的类, 包括可以读取可用打印机的名字

C# 中,想要使用 WPS (Office处理程序) 将 Excel 文件指定到特定的打印机进行打印,你可以利用 Microsoft.Office.Interop.Words 和 System.Printing 这两个库。下面是一个简单的步骤概述: 1. 首先,你需要添加对 Office 库的支持,这通常是在项目属性(Properties -> Build -> References)中引用 "Microsoft.Office.Interop.Word"。 2. 创建 Word.Application 对象,启动 Word 应用程序: ```csharp Word.Application wordApp = new Word.Application(); ``` 3. 打开 Excel 文件: ```csharp Document excelDoc = wordApp.Documents.Open(@"path\to\your.xlsx"); ``` 4. 获取你要打印的范围,例如整个文档: ```csharp Range printRange = excelDoc.Range(); ``` 5. 创建一个 PrintDocument 类的实例,并设置它的 Document 属性指向 Word 文档: ```csharp PrintDocument pd = new PrintDocument(); pd.Document = excelDoc; ``` 6. 设置打印机: ```csharp PrinterSettings settings = new PrinterSettings(); settings.PrinterName = "Your_Printer_Name"; // 替换为你的打印机名称 pd.DefaultPrintQueue = settings.PrintQueue; ``` 7. 实现 BeginPrint 和 EndPrint 方法来开始和结束打印过程: ```csharp void OnBeginPrint(object sender, PrintEventArgs e) { // 开始打印前的准备,如清空剪贴板等 } void OnEndPrint(object sender, PrintEventArgs e) { // 打印完成后清理资源 } pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); pd.PrinterSettings.PrinterFontCollection.RegisterFont(new Font("Arial", 12)); // 注册字体 pd.BeginPrint += OnBeginPrint; pd.EndPrint += OnEndPrint; pd.Print(); ``` 8. 在 PrintPage 事件处理器 pd_PrintPage 中,实现页面布局和绘制功能,然后返回 true 继续下一页,false 则停止打印。 注意:在实际应用中,你需要确保用户有权限操作 Word 和指定打印机,以及处理可能出现的异常。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值