private void AddPrintingStatus(string printerPath, IDocumentContract document, Notification notification) { var status = new DocumentPrintingStatusContract { Id = notification.JobId, DocumentName = document.Name, PrinterName = printerPath, Status = DocumentStatus.Pending.ToString() }; this.documentStatusManager.AddPrintingStatus(status); }
/// <summary> /// Handles the notification. /// </summary> /// <param name="notification">The notification.</param> /// <returns>The task</returns> public async Task HandleNotification(Notification notification) { try { IDocumentContract document = null; using (var client = this.documentRoutingODataClientFactory.Create()) { var uri = await client.GetDownloadDocumentUri(notification.JobId); using (var stream = new MemoryStream()) { var blob = new CloudBlockBlob(uri); await blob.DownloadToStreamAsync(stream).ConfigureAwait(false); stream.Seek(0, SeekOrigin.Begin); document = this.DeserializeDocument(stream, notification.DocumentContractType); } } var printerPath = this.GetPrinterPath(document, notification.DocumentContractType); this.AddPrintingStatus(printerPath, document, notification); if (!await this.IsPrinterInstalled(printerPath)) { this.documentStatusManager.UpdatePrintingStatus(notification.JobId, DocumentStatus.Failure.ToString()); using (var client = this.documentRoutingODataClientFactory.Create()) { await client.UnregisterPrinters(new List <DocumentRoutingPrinter> { new DocumentRoutingPrinter { Path = printerPath } }); } return; } this.documentStatusManager.UpdatePrintingStatus(notification.JobId, DocumentStatus.Printing.ToString()); IDocumentPrinter documentPrinter = this.GetDocumentPrinter(notification.DocumentContractType); var printResult = documentPrinter.Print(document); this.documentStatusManager.UpdatePrintingStatus(notification.JobId, printResult ? DocumentStatus.Success.ToString() : DocumentStatus.Failure.ToString()); } catch { this.documentStatusManager.UpdatePrintingStatus(notification.JobId, DocumentStatus.Failure.ToString()); // TODO Error Log } }
private string GetPrinterPath(IDocumentContract document, DocumentContractType documentContractType) { switch (documentContractType) { case DocumentContractType.Metafile: return(PageSettingsHelper.PrinterNameFromPageSettings(document.Settings)); case DocumentContractType.Pdf: return(PageSettingsHelper.PrinterNameFromPageSettings(document.Settings)); case DocumentContractType.Zebra: return(document.Settings); default: throw new ArgumentException("Unsupported value", nameof(documentContractType)); } }
/// <summary> /// Prints the specified document. /// </summary> /// <param name="document">The document.</param> /// <returns>True if document was printed successfully.</returns> public bool Print(IDocumentContract document) { var documentContract = document as PdfDocumentContract ?? throw new ArgumentException("Invalid type", nameof(document)); // Validate the document contract if (documentContract == null) { throw new ArgumentNullException(nameof(documentContract)); } if (documentContract.Contents == null || documentContract.Contents.Length == 0) { throw new ArgumentNullException("documentContract.Contents"); } if (string.IsNullOrWhiteSpace(documentContract.Settings)) { throw new ArgumentNullException("documentContract.Settings"); } // Get the printer from the settings var printer = PageSettingsHelper.PrinterNameFromPageSettings(documentContract.Settings); if (string.IsNullOrWhiteSpace(printer)) { throw new ArgumentNullException("PageSettings.PrinterName"); } // Get the pdf file to send to the printer var pdfFilePath = this.DocumentContractToTempFile(documentContract); try { this.AdobeReaderPrint(pdfFilePath, printer); return(true); } catch (Exception) { // TODO Error Log return(false); } }
/// <summary> /// Prints the specified document. /// </summary> /// <param name="document">The document.</param> /// <returns> /// True if document was printed successfully. /// </returns> public bool Print(IDocumentContract document) { var documentContract = document as ZebraDocumentContract ?? throw new ArgumentException("Invalid type", nameof(document)); Debug.Assert(documentContract != null, "Parameter 'documentContract' must not be null."); Debug.Assert(documentContract.Contents != null && documentContract.Contents.Length > 0, "Parameter 'documentContract.Contents' must not be null or empty."); Debug.Assert(!string.IsNullOrEmpty(documentContract.Settings), "Parameter 'documentContract.Settings' must not be null or empty."); // The printer name is the only thing in settings when printing via Zebra string printerName = documentContract.Settings; string contents = Encoding.ASCII.GetString(documentContract.Contents); string errorMessage = Printer.SendStringToPrinter(printerName, contents); if (!string.IsNullOrEmpty(errorMessage)) { // TODO Error log return(false); } return(true); }
/// <summary> /// Prints the specified document. /// </summary> /// <param name="document">The document.</param> public bool Print(IDocumentContract document) { var documentContract = document as MetafileDocumentContract ?? throw new ArgumentException("Invalide type", nameof(document)); if (documentContract.PageSizes == null || documentContract.PageSizes.Count() == 0) { throw new ArgumentNullException(Resources.NoPageSizes); } var pageSizesList = documentContract.PageSizes.ToList(); this.emfImages = MetafileHelper.GetEmfImagesToPePrinted(pageSizesList, documentContract.Contents); if (this.emfImages == null || this.emfImages.Count == 0) { // Issue an instrumentation error and return as there is nothing to print throw new ArgumentException(Resources.NoMetafilesToPrint); } // Now determine the printer settings necessary PageSettings pageSettingsOfPrinter = null; var pageSettingsHelper = new PageSettingsHelper(); if (!string.IsNullOrWhiteSpace(documentContract.Settings)) { pageSettingsHelper.IsLandscapeSetOnReportDesign = this.emfImages[0].Height < this.emfImages[0].Width; try { pageSettingsOfPrinter = pageSettingsHelper.ProcessPageSettings(documentContract.Settings); } catch (Exception) { // TODO Error log Resources.UnableToInitializePrinterSettings return(false); } } // PageSettings returned after processing should not be null. Throw if it is. if (pageSettingsOfPrinter == null) { throw new ArgumentNullException(Resources.NoPageSettings); } // Send the print job to printer with the pagesettings obtained int retryCount = 1; try { while (retryCount <= Constants.MaximumRetry) { try { this.currentPageNumber = 0; using (var printDocument = new PrintDocument()) { printDocument.DocumentName = documentContract.Name; printDocument.DefaultPageSettings = pageSettingsOfPrinter; printDocument.PrinterSettings = pageSettingsOfPrinter.PrinterSettings; printDocument.PrintPage += this.PrintEachPage; // use standard print controller instead of print controller with status dialog. printDocument.PrintController = new StandardPrintController(); printDocument.Print(); } break; } catch (InvalidPrinterException) { if (retryCount < Constants.MaximumRetry) { retryCount++; } else { string message = string.Format(CultureInfo.InvariantCulture, Resources.InvalidPrinter, pageSettingsOfPrinter.PrinterSettings.PrinterName); //TODO Error log return(false); } } } } finally { if (this.emfImages != null && this.emfImages.Count > 0) { foreach (var page in this.emfImages) { if (page != null) { page.Dispose(); } } } } return(true); }