C# WPF 调用打印机的两种方法
2021/11/5 14:39:44
本文主要是介绍C# WPF 调用打印机的两种方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C# WPF 调用打印机的两种方法
最近在调试打印机,为了方便测试写了一个小demo。为了更好的判断是打印机硬件的问题还是动态库的问题,设定了定时器不间断打印来进行测试。现来分享记录一下。
需要调用两种动态库
- PdfPrintingNet.dll动态库
- O2S.Components.PDFRender4NET.dll and O2S.Components.PDFView4NET.dll方法引用。直接调用路径
动态库和demo都会上传到资源里,以便大家参考。
这里是用的三星打印机,各位如果需要自己装驱动。亲测可用
using PdfPrintingNet; using System; using System.Timers; using System.Windows; using System.Drawing.Printing; using System.Diagnostics; using System.Collections.Specialized; using O2S.Components.PDFRender4NET; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public System.Timers.Timer timer = new System.Timers.Timer();//定时器的引用 string pathe = AppDomain.CurrentDomain.BaseDirectory + "1.pdf";//物理路径 直接到Debug下 PdfPrint pdfprint = new PdfPrint("", ""); public MainWindow() { InitializeComponent(); //定时器调用循环 timer.Interval = 1000*20;//这里设置的间隔时间为毫秒 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);//执行方法 timer.AutoReset = true;//设置timer_Elapsed是执行一次(false)还是一直执行(true) timer.Enabled = true; timer.Start(); } //方法一------O2S.Components.PDFRender4NET.dll and O2S.Components.PDFView4NET.dll方法引用。直接调用路径 private int printShow(string url) { int isOK = 0; PDFFile file = PDFFile.Open(url); PrinterSettings settings = new PrinterSettings(); System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); //settings.PrinterName = "hp LaserJet 1160 PCL 5e"; 打印机的型号 settings.PrintToFile = false; //设置纸张大小(可以不设置,取默认设置)3.90 in, 8.65 in PaperSize ps = new PaperSize("test", 4, 9); ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx) O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings); pdfPrintSettings.PaperSize = ps; pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional; pdfPrintSettings.PrinterSettings.Copies = 1; try { file.Print(pdfPrintSettings); isOK = 1; } catch (Exception) { isOK = -1; throw; } finally { file.Dispose(); } return isOK; } /// <summary> /// 按钮单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, RoutedEventArgs e) { pdfPrint(pathe); } ///方法二 打开WPS的直接打印 自动打开关闭 private void pdfPrint(string filePath) { PrintDocument pd = new PrintDocument(); Process p = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true; startInfo.WindowStyle = ProcessWindowStyle.Maximized; startInfo.UseShellExecute = true; startInfo.FileName = filePath; startInfo.Verb = "print"; startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\""; p.StartInfo = startInfo; p.Start(); p.WaitForExit(); } /// <summary> /// 倒计时结束 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Elapsed(object sender, ElapsedEventArgs e) { // pdfprint.Print(pathe); //PdfPrint打印 printShow(pathe); } } }
这篇关于C# WPF 调用打印机的两种方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具