public WebPrinter(String url, TiffImagePrinter.PrintSession printSession) { Spooled = false; this.url = url; this.printSession = printSession; }
public void Convert(String inputFile, ConversionOptions options) { using (var printSession = new TiffImagePrinter.PrintSession(options)) { if (printSession.SetDefaultPrinter()) { var url = Path.ChangeExtension(inputFile, options.Extension); try { File.Copy(inputFile, url, true); WebPrinter printer = new WebPrinter(url, printSession); if (printer.Print(TimeoutValues.JobSpooled)) { // Now that its started spooling, wait for the job to complete // Don't need to do this as the Web Browser can be closed, this is to ensure the job // completes successfully before returning control to the main thread if (!printSession.WaitForJobsCompleted()) { printSession.Cancel(); throw new TimeoutException("Timeout waiting for print job to complete."); } } else { printSession.Cancel(); throw new TimeoutException("Timeout waiting for print job to spool."); } } finally { File.Delete(url); } } else { printSession.Cancel(); throw new Exception("Unable to set the system default printer."); } } }
public void Convert(String inputFile, ConversionOptions options) { Object oMissing = Type.Missing; MODI.Document oMdiDoc = null; try { // Create an instance on a MODI document oMdiDoc = new MODI.Document(); // Open the input file oMdiDoc.Create(inputFile); // For some unknown reason I need to do this otherwise pages 2+ // will print tiny, shrunk pages in the top left corner, its either // this or calling SaveAs, this is quicker and easier... for (int i = 0; i < oMdiDoc.Images.Count; i++) { var image = (MODI.Image)oMdiDoc.Images[i]; int tmp = image.PixelHeight; Marshal.ReleaseComObject(image); } using (var printSession = new TiffImagePrinter.PrintSession(options)) { try { oMdiDoc.PrintOut(0, -1, 1, printSession.PrinterName, "", false, MODI.MiPRINT_FITMODES.miPRINT_PAGE); } catch { printSession.Cancel(); throw; } // Wait for the jobs to start spooling to the printer // TODO: make timeout parameterised if (printSession.WaitForJobsSpooling()) { // Now that its started spooling, wait for the job to complete // Don't need to do this as MODI can be closed, this is to ensure the job // completes successfully before returning control to the main thread if (!printSession.WaitForJobsCompleted()) { printSession.Cancel(); throw new TimeoutException("Timeout waiting for print job to complete."); } } else { // The document did not enter the print queue so we need to cancel this // printSession to allow the printer to return to the printer pool. printSession.Cancel(); // Throw a TimeoutException throw new TimeoutException("Timeout waiting for print job to spool."); } } } finally { try { if (oMdiDoc != null) { oMdiDoc.Close(false); oMdiDoc = null; } } catch { // TODO: Log the error here... } } // Force a Garbage Collection GC.Collect(); GC.WaitForPendingFinalizers(); }