private static void UpdatePrintJob(PrintJob job, IntPtr p) { JOB_INFO_1 obj = (JOB_INFO_1)Marshal.PtrToStructure(p, typeof(JOB_INFO_1)); job.Datatype = obj.pDatatype; job.Document = obj.pDocument; job.JobId = obj.JobId; job.MachineName = obj.pMachineName; job.PagesPrinted = obj.PagesPrinted; job.Position = obj.Position; job.NativePrinterName = obj.pPrinterName; job.Priority = obj.Priority; job.Status = (PrintJobStatus)obj.Status; job.StatusText = obj.pStatus; FILETIME ft = new FILETIME(); SystemTimeToFileTime(ref obj.Submitted, ref ft); FILETIME ft2 = new FILETIME(); FileTimeToLocalFileTime(ref ft, ref ft2); SYSTEMTIME st2 = new SYSTEMTIME(); FileTimeToSystemTime(ref ft2, ref st2); job.Submitted = new DateTime(st2.wYear, st2.wMonth, st2.wDay, st2.wHour, st2.wMinute, st2.wSecond); job.TotalPages = obj.TotalPages; job.UserName = obj.pUserName; }
/// <summary> /// 控制打印任务 /// </summary> /// <param name="job"></param> /// <param name="command"></param> /// <param name="throwException"></param> /// <returns></returns> internal static bool ControlJob(PrintJob job, PrintJobControlCommand command, bool throwException) { if (job == null) { throw new ArgumentNullException("job"); } int printer = 0; Win32Exception ext = null; if (OpenPrinterA(job.PrinterName, ref printer, 0) != 0) { bool result = SetJob(printer, job.JobId, 0, IntPtr.Zero, ( int )command); if (UpdatePrintJob(printer, job, throwException) == false) { result = false; } ClosePrinter(printer); return(result); } else { ext = new Win32Exception(Marshal.GetLastWin32Error()); if (throwException) { throw ext; } else { return(false); } } }
/// <summary> /// 刷新对象数据 /// </summary> /// <param name="printerName">打印机名称</param> public void Refresh() { myJobs = new PrintJobList(); int printer = 0; Win32Exception ext = null; int result = OpenPrinterA(strPrinterName, ref printer, 0); if (result != 0) { int bufferSize = 0; int recordNumber = 0; GetPrinter(printer, 2, IntPtr.Zero, 0, ref bufferSize); if (bufferSize > 0) { IntPtr buf = Marshal.AllocHGlobal(bufferSize); if (GetPrinter(printer, 2, buf, bufferSize, ref bufferSize) != 0) { this.myInfo = (PRINTER_INFO_2)Marshal.PtrToStructure(buf, typeof(PRINTER_INFO_2)); } Marshal.FreeHGlobal(buf); } int structureSize = Marshal.SizeOf(typeof(JOB_INFO_1)); EnumJobsA(printer, 0, 10000, 1, IntPtr.Zero, 0, ref bufferSize, ref recordNumber); if (bufferSize > 0) { IntPtr p2 = Marshal.AllocHGlobal(bufferSize); if (EnumJobsA(printer, 0, 1000, 1, p2, bufferSize, ref bufferSize, ref recordNumber) != 0) { for (int iCount = 0; iCount < recordNumber; iCount++) { IntPtr p3 = new IntPtr((long)p2 + iCount * structureSize); PrintJob job = new PrintJob(); UpdatePrintJob(job, p3); job.PrinterName = strPrinterName; this.Jobs.Add(job); } } else { Marshal.FreeHGlobal(p2); ClosePrinter(printer); ext = new Win32Exception(Marshal.GetLastWin32Error()); throw ext; } Marshal.FreeHGlobal(p2); } ClosePrinter(printer); } else { ext = new Win32Exception(Marshal.GetLastWin32Error()); throw ext; } }
//private bool bolPrintSuccess = false; ///// <summary> ///// 打印是否成功 ///// </summary> //[System.ComponentModel.Browsable( false )] //public bool PrintSuccess //{ // get // { // return bolPrintSuccess; // } //} protected override void OnEndPrint(PrintEventArgs e) { base.OnEndPrint(e); if (this.PreparePrintJob) { this.myPrintJob = null; PrinterInformation info = new PrinterInformation(this.PrinterSettings.PrinterName); foreach (PrintJob job in info.Jobs) { if (job.Document == this.DocumentName) { this.myPrintJob = job; break; } } } }
private static bool UpdatePrintJob(int printer, PrintJob job, bool throwException) { Win32Exception ext = null; int size = 0; GetJobA(printer, job.JobId, 1, IntPtr.Zero, 0, ref size); bool result = false; if (size > 0) { IntPtr p = Marshal.AllocHGlobal(size); if (GetJobA(printer, job.JobId, 1, p, size, ref size) != 0) { UpdatePrintJob(job, p); Marshal.FreeHGlobal(p); result = true; } else { Marshal.FreeHGlobal(p); ext = new Win32Exception(Marshal.GetLastWin32Error()); } } else { ext = new Win32Exception(Marshal.GetLastWin32Error()); if (ext.NativeErrorCode == 87) { ext = null; } } ClosePrinter(printer); if (ext != null) { if (throwException) { throw ext; } else { return(false); } } return(result); }
internal static bool RefreshPrintJob(PrintJob job, bool throwException) { int printer = 0; Win32Exception ext = null; if (OpenPrinterA(job.PrinterName, ref printer, 0) != 0) { return(UpdatePrintJob(printer, job, throwException)); } else { if (throwException) { ext = new Win32Exception(Marshal.GetLastWin32Error()); throw ext; } else { return(false); } } }
/// <summary> /// 已重载:开始打印文档 /// </summary> /// <param name="e">事件参数</param> protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e) { if (CheckContent()) { // 指定打印机 string printerName = this.PrinterName; if (printerName == null || printerName.Trim().Length == 0) { if (this.CurrentDocumentPage != null) { printerName = this.CurrentDocumentPage.PageSettings.PrinterName; } } if (printerName != null && printerName.Trim().Length > 0) { printerName = printerName.Trim(); foreach (string name in PrinterSettings.InstalledPrinters) { if (string.Compare(printerName, name, true) == 0) { //this.PrinterSettings.PrinterName = name; base.DefaultPageSettings.PrinterSettings.PrinterName = name; base.PrinterSettings.PrinterName = name; break; } } } // 指定纸张设置 string paperSource = this.PaperSourceName; if (paperSource != null && paperSource.Trim().Length > 0) { foreach (System.Drawing.Printing.PaperSource source in base.DefaultPageSettings.PrinterSettings.PaperSources) { if (source.SourceName != null && string.Compare( source.SourceName.Trim(), paperSource.Trim(), true) == 0) { base.DefaultPageSettings.PaperSource = source; break; } } //e.PageSettings.PaperSource.SourceName = paperSource; } int startIndex = 0; if (this.JumpPrint.Enabled && this.JumpPrint.Page != null) { startIndex = myPages.IndexOf(this.JumpPrint.Page); } System.Collections.ArrayList pages = new System.Collections.ArrayList(); switch (this.PrinterSettings.PrintRange) { case PrintRange.Selection: case PrintRange.AllPages: for (int iCount = startIndex; iCount < myPages.Count; iCount++) { pages.Add(myPages[iCount]); } break; case PrintRange.CurrentPage: pages.Add(this.CurrentDocumentPage); break; case PrintRange.SomePages: int endIndex = Math.Min(myPages.Count - 1, this.PrinterSettings.ToPage); for (int iCount = Math.Max(startIndex, this.PrinterSettings.FromPage); iCount <= endIndex; iCount++) { pages.Add(myPages[iCount]); } break; } if (pages.Count == 0) { // 没有打印页,立即取消打印操作 e.Cancel = true; return; } else { myPageEnumerator = pages.GetEnumerator(); myPageEnumerator.MoveNext(); } } else { //intCurrentPrintPageIndex = 0 ; } if (this.PreparePrintJob) { this.myPrintJob = null; this.DocumentName = this.DocumentName + "$" + System.Environment.TickCount; } base.OnBeginPrint(e); }
public int Add(PrintJob item) { return(this.List.Add(item)); }