void IStringSettingConvertible.Convert(string settingValue) { XDocument doc = XDocument.Parse(settingValue); foreach (XElement pqe in doc.Root.Elements("PrintingQueue")) { // Sanity-check name first to avoid exception. string pqName = pqe.TryGetAttributeValue("Name", null); if (string.IsNullOrWhiteSpace(pqName)) { Logger.Instance.LogFormat(LogType.Warning, this, Resources.PrintingQueueParseErrorNameIsInvalid); continue; } PrintingQueue pq = new PrintingQueue(); pq.Name = pqName; pq.PrintServer = pqe.TryGetAttributeValue("PrintServer", null); pq.PrinterName = pqe.TryGetAttributeValue("PrinterName", null); pq.IsEnabled = pqe.TryGetAttributeValue("IsEnabled", true); pq.UseAlternativeCopyingMethod = pqe.TryGetAttributeValue("UseAlternativeCopyingMethod", false); // Sanity-check copy count to avoid exception. int pqCopyCount = pqe.TryGetAttributeValue("CopyCount", 1); if (pqCopyCount < 1) { pqCopyCount = 1; Logger.Instance.LogFormat(LogType.Warning, this, Resources.PrintingQueueCopyCountMustBeGreaterThanZero, pqCopyCount); } pq.CopyCount = pqCopyCount; this.Entries.Add(pq); } }
/// <summary> /// Executes a printing operation using a specific <see cref="PrintingQueue"/> and action. /// </summary> /// <param name="queue">The printing queue to use. Must not be null.</param> /// <param name="printAction">The printing action. Must not be null.</param> public static void Print(PrintingQueue queue, PrintDelegate printAction) { Assertions.AssertNotNull(queue, "queue"); Assertions.AssertNotNull(printAction, "printAction"); if (!queue.IsValid) { Logger.Instance.LogFormat(LogType.Warning, typeof(GdiPrinter), Resources.GdiPrinterPrintingQueueIsNotValid, queue.Name); return; } PrintDocument doc = new PrintDocument(); if (!queue.IsDefaultPrinter) { doc.PrinterSettings.PrinterName = queue.GetPrinterName(); } int desiredCopyCount = queue.CopyCount; int maxSupportedCopyCount = doc.PrinterSettings.MaximumCopies; int requiredPrintIterations = 1; if (desiredCopyCount <= maxSupportedCopyCount && !queue.UseAlternativeCopyingMethod) { doc.PrinterSettings.Copies = (short)desiredCopyCount; } else { //Check of the user has requested using this way of printing copies! if (!queue.UseAlternativeCopyingMethod) { // It appears that some printers don't support the CopyCount-feature (notably Microsoft XPS Writer or perhaps PDF-Writers in general?). // In this case we simply repeat printing until we have reached our copy count. Logger.Instance.LogFormat(LogType.Warning, typeof(GdiPrinter), Resources.UsedPrinterDoesNotSupportThatMuchCopies, maxSupportedCopyCount, desiredCopyCount); } requiredPrintIterations = desiredCopyCount; } for (int i = 0; i < requiredPrintIterations; i++) { Logger.Instance.LogFormat(LogType.Trace, typeof(GdiPrinter), Resources.PrintIterationStart, i + 1, requiredPrintIterations); PrintTask task = new PrintTask(); try { task.Print(doc, printAction); } catch (Exception ex) { Logger.Instance.LogFormat(LogType.Error, typeof(GdiPrinter), Resources.GdiPrinterPrintTaskException); Logger.Instance.LogException(typeof(GdiPrinter), ex); } Logger.Instance.LogFormat(LogType.Trace, typeof(GdiPrinter), Resources.PrintIterationEnd); } }
/// <summary> /// Executes a printing operation using a specific <see cref="PrintingQueue"/> and action. /// </summary> /// <param name="queue">The printing queue to use. Must not be null.</param> /// <param name="printAction">The printing action. Must not be null.</param> public static void Print(PrintingQueue queue, PrintDelegate printAction) { Print(queue, printAction, null); }