/// <summary> /// Gets the image from the downloaded stream /// </summary> /// <param name="pageSizesList">List of page sizes</param> /// <param name="allPages">Report that needs to be printed</param> /// <returns>Returns the images</returns> public static List <Metafile> GetEmfImagesToPePrinted(List <int> pageSizesList, byte[] allPages) { List <Metafile> emfImages = null; Debug.Assert(pageSizesList != null && pageSizesList.Count > 0, "Parameter 'pageSizesList' must not be null or empty."); // everything matches up, so split up the array into individual pages emfImages = MetafileHelper.SplitByteArrayIntoPages(allPages, pageSizesList); if (emfImages == null || emfImages.Count == 0) { throw new ArgumentNullException(Resources.NoEmfImage); } return(emfImages); }
/// <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); }