public static string GetPrinterPath() { Console.WriteLine("{0}: GetPrinterPath printer com exists : {1}", DateTime.Now.ToString("HH:mm:ss fff"), objUDC != null); object objPrn = null; object objPrf = null; if (objUDC == null || objUDC.Version < 6) { Type objPrnType = Type.GetTypeFromCLSID(new Guid(WripperGuid)); if (objPrnType == null) { objPrnType = Type.GetTypeFromCLSID(new Guid(PrinterGuid)); } if (objPrnType == null) { return(null); } try { objPrn = Activator.CreateInstance(objPrnType); if (objPrn == null) { return(null); } var ver = RunCom.GetProperty(objPrn, "Version"); int v = 0; if (ver is float) { v = (int)(float)ver; } else { v = (int)ver; } if (v < 6) { var str = (string)RunCom.GetProperty(objPrn, "DefaultProfile"); objPrf = RunCom.GetProperty(objPrn, "Profile", str); var path = (string)RunCom.GetProperty(objPrf, "PreDefinedImageFilePath"); if (!string.IsNullOrEmpty(path)) { return(path); } } else { objUDC = (IUDC)objPrn; objPrf = RunCom.GetProperty(objPrn, "Printers", Environment.PrinterName); objPrn = null; if (objPrf == null) { return(null); } object Profile = RunCom.GetProperty(objPrf, "Profile"); if (Profile == null) { return(null); } object OutputLocation = RunCom.GetProperty(Profile, "OutputLocation"); Marshal.ReleaseComObject(Profile); Profile = null; if (OutputLocation == null) { return(null); } var s = RunCom.GetProperty(OutputLocation, "FolderPath"); string path = s as string; Marshal.ReleaseComObject(OutputLocation); OutputLocation = null; if (string.IsNullOrEmpty(path)) { return(null); } else { path = Path.GetFullPath(path.Replace("&[TEMP]", Path.GetTempPath()).Replace("&[Documents]", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments))); } return(path); } } catch (Exception ex) { Data.Env.WriteToLog(ex, "TestPrinter.GetPrinterPath"); } finally { objPrnType = null; if (objPrn != null) { Marshal.ReleaseComObject(objPrn); objPrn = null; } if (objPrf != null) { Marshal.ReleaseComObject(objPrf); objPrf = null; } } } else { try { Printer pr = objUDC.Printers[Environment.PrinterName]; string path = pr.Profile.OutputLocation.FolderPath; Marshal.ReleaseComObject(pr); pr = null; if (string.IsNullOrEmpty(path)) { return(null); } else { path = Path.GetFullPath(path.Replace("&[TEMP]", Path.GetTempPath()).Replace("&[Documents]", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments))); } return(path); } catch (Exception ex) { Log.Logger.WriteEx(ex); } } return(null); }
/// <summary> /// Установить профиль принтера /// Этот метод может вызвать ошибку RPC_E_CANTCALLOUT_ININPUTSYNCCALL если вызвается WndProc /// </summary> /// <param name="profile"></param> public static void SetPrinterProfile(ProfileType profile) { Console.WriteLine("{0}: SetPrinterProfile printer com exists : {1}", DateTime.Now.ToString("HH:mm:ss fff"), objUDC != null); if (objUDC != null) { Printer pr = objUDC.Printers[Environment.PrinterName]; pr.Profile.PageSetup.ResolutionX = 200; pr.Profile.PageSetup.ResolutionY = 200; pr.Profile.OutputLocation.FolderPath = "&[TEMP]\\Documents"; pr.Profile.OutputLocation.FileName = "&[DocName(0)].&[ImageType]"; pr.Profile.OutputLocation.Mode = LocationModeID.LM_PREDEFINED; pr.Profile.OutputLocation.OverwriteExistingFile = true; pr.Profile.PageSetup.Units = UnitID.UNIT_IN; pr.Profile.Advanced.ShowNotifications = false; pr.Profile.Advanced.ShowProgressWnd = false; if (pr.Profile.FileFormat.TIFF.ColorSpace == ColorSpaceID.CS_TRUECOLOR || pr.Profile.FileFormat.TIFF.ColorSpace == ColorSpaceID.CS_COLOR_256) { pr.Profile.FileFormat.TIFF.ColorSpace = ColorSpaceID.CS_TRUECOLOR; pr.Profile.FileFormat.TIFF.Compression = CompressionID.CMP_FLATE; } else { pr.Profile.FileFormat.TIFF.ColorSpace = ColorSpaceID.CS_BLACKWHITE; pr.Profile.FileFormat.TIFF.Compression = CompressionID.CMP_CCITTGR4; } if (pr.Profile.FileFormat.PDF.ColorSpace == ColorSpaceID.CS_TRUECOLOR || pr.Profile.FileFormat.PDF.ColorSpace == ColorSpaceID.CS_COLOR_256) { pr.Profile.FileFormat.PDF.ColorSpace = ColorSpaceID.CS_TRUECOLOR; pr.Profile.FileFormat.PDF.Compression = CompressionID.CMP_FLATE; } else { pr.Profile.FileFormat.PDF.ColorSpace = ColorSpaceID.CS_BLACKWHITE; pr.Profile.FileFormat.PDF.Compression = CompressionID.CMP_CCITTGR4; } pr.Profile.FileFormat.ActualFormat = ((profile == ProfileType.PDFProfile) ? FormatID.FMT_PDF : FormatID.FMT_TIFF); pr.Profile.PostProcessing.Mode = PostProcessingModeID.PP_OPEN_CUSTOM_APP; pr.Profile.PostProcessing.CustomAppPath = System.Windows.Forms.Application.ExecutablePath; pr.Profile.PostProcessing.CustomAppParameters = "\"&[OutFile(0)]\""; Marshal.ReleaseComObject(pr); pr = null; return; } Type objPrnType = Type.GetTypeFromCLSID(new Guid(PrinterGuid)); if (objPrnType == null) { return; } if (!objPrnType.HasElementType) { return; } object objPrn = null; object objPrf = null; try { objPrn = Activator.CreateInstance(objPrnType); if (objPrn == null) { return; } if (string.IsNullOrEmpty(Environment.PrinterName)) { return; } RunCom.SetProperty(objPrn, "PrinterName", Environment.PrinterName); var str = (string)RunCom.GetProperty(objPrn, "DefaultProfile"); string profStr = profile == ProfileType.TiffProfile ? TiffProfile : PDFProfile; if (str != profStr) { objPrf = RunCom.GetProperty(objPrn, "Profile", profStr); if (objPrn != null) { Marshal.ReleaseComObject(objPrn); objPrn = null; } if (objPrf != null) { RunCom.SetProperty(objPrf, "Default", 0); } } } catch (Exception ex) { } finally { if (objPrnType != null) { objPrnType = null; } if (objPrn != null) { Marshal.ReleaseComObject(objPrn); objPrn = null; } if (objPrf != null) { Marshal.ReleaseComObject(objPrf); objPrf = null; } } Environment.PrinterPath = null; }