需要调用两种动态库
这里是用的三星打印机,各位如果需要自己装驱动。亲测可用
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); } } }