Esempio n. 1
0
        private void OnPrintAll(object sender, PrintPageEventArgs e)
        {
            try
            {
                Byte[]       bitmapData   = File.ReadAllBytes(_url);
                MemoryStream streamBitmap = new MemoryStream(bitmapData);
                if (this.width != default(float) && this.width != 0)
                {
                    Rectangle m = e.MarginBounds;
                    m.Height   = Convert.ToInt32(this.height);
                    m.Width    = Convert.ToInt32(this.width);
                    m.Location = new Point(0, 0);
                    e.Graphics.DrawImage(new Bitmap((Bitmap)Image.FromStream(streamBitmap)), m);
                }
                else
                {
                    e.Graphics.DrawImage(new Bitmap((Bitmap)Image.FromStream(streamBitmap)), 0, 0);
                }

                e.HasMorePages = false;
            }
            catch (Exception ex)
            {
                _filePrint.SendErrorMsg(_session, "Print", "Print all image error", ex);
                Logger.Error("IMAGEPrinting.OnPrintAll", ex);
            }
        }
Esempio n. 2
0
 public new bool Print(string printer, string url, WebSocketSession session, FilePrintHelper filePrint)
 {
     try
     {
         if (base.useRaw)
         {
             return(base.Print(printer, url, session, filePrint));
         }
         _session   = session;
         _filePrint = filePrint;
         _url       = url;
         PrintDocument printDocument = new PrintDocument();
         printDocument.PrintPage += new PrintPageEventHandler(OnPrintAll);
         if (!String.IsNullOrEmpty(printer))
         {
             printDocument.PrinterSettings.PrinterName = printer;
         }
         printDocument.Print();
         return(true);
     }
     catch (Exception ex)
     {
         filePrint.SendErrorMsg(session, "Print", "Print image error", ex);
         Logger.Error("IMAGEPrinting.print =>", ex);
     }
     return(false);
 }
Esempio n. 3
0
        private bool TryOncePrintToSpecificPrinter(string fileName, string printer, int retry)
        {
            Microsoft.Office.Interop.Word.Document doc = null;

            try
            {
                doc = OpenDocument(fileName);

                if (!SetActivePrinter(doc, printer))
                {
                    return(false);
                }

                Print(doc);

                if (doc != null)
                {
                    object saveOptionsObject = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                    object missingValue      = Missing.Value;
                    doc.Close(ref saveOptionsObject, ref missingValue, ref missingValue);

                    // Application
                    if (word != null)
                    {
                        word.Quit(saveOptionsObject, missingValue, missingValue);
                    }
                }

                return(true); // we did what we wanted to do here
            }
            catch (Exception e)
            {
                if (retry == _exceptionLimit)
                {
                    throw new Exception("Word printing failed.", e);
                }
                _filePrint.SendErrorMsg(_session, "Print", "Print word error", e);
                // restart Word, remembering to keep an appropriate delay between Quit and Start.
                // this should really be handled by wrapper classes
            }
            finally
            {
                if (doc != null)
                {
                    // release your doc (COM) object and do whatever other cleanup you need
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    if (word != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
                    }
                    doc  = null;
                    word = null;
                    GC.Collect();
                }
            }

            return(false);
        }
Esempio n. 4
0
        public new bool Print(string printer, string url, WebSocketSession session, FilePrintHelper filePrint)
        {
            try
            {
                if (base.useRaw)
                {
                    return(base.Print(printer, url, session, filePrint));
                }
                if (useBrowserPrint)
                {
                    var infoBrowserConsole = new ProcessStartInfo();
                    infoBrowserConsole.Arguments = " \"" + url + "\"   \"" + printer + "\"";
                    //info.Arguments = "printername=\"" + printer + "\"   url=\"" + url + "\"";
                    var pathToBrowserExe = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    infoBrowserConsole.FileName = Path.Combine(pathToBrowserExe, "BrowserPrint.exe");
                    Logger.Log("Starting browser-print:" + infoBrowserConsole.FileName);
                    using (var p = Process.Start(infoBrowserConsole))
                    {
                        // Wait until it is finished
                        while (!p.HasExited)
                        {
                            System.Threading.Thread.Sleep(10);
                        }
                        // Return the exit code
                        return(p.ExitCode == 0);
                    }

                    return(true);
                }
                // Spawn the code to print the packing slips
                var info = new ProcessStartInfo();
                info.Arguments = margins + " -p \"" + printer + "\" \"" + url + "\"";
                //info.Arguments = "printername=\"" + printer + "\"   url=\"" + url + "\"";
                var pathToExe = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                info.FileName = Path.Combine(pathToExe, "PrintHtml.exe");
                using (var p = Process.Start(info))
                {
                    // Wait until it is finished
                    while (!p.HasExited)
                    {
                        System.Threading.Thread.Sleep(10);
                    }
                    // Return the exit code
                    return(p.ExitCode == 0);
                }
            }
            catch (Exception ex)
            {
                filePrint.SendErrorMsg(session, "Print", "Print html error", ex);
                Logger.Error("HTMLPrinting.print =>", ex);
                return(false);
            }
        }
Esempio n. 5
0
        public bool Print(string printer, string url, WebSocketSession session, FilePrintHelper filePrint)
        {
            try
            {
                // Create an instance of the Printer
                IPrinter printerRaw = new Printer();

                // Print the file
                printerRaw.PrintRawFile(printer, url, Path.GetFileName(url));

                return(true);
            }
            catch (Exception ex)
            {
                filePrint.SendErrorMsg(session, "Print", "Print raw error", ex);
                Logger.Error("RAWPrinting.print =>", ex);
                return(false);
            }
        }
 public new bool Print(string printer, string url, WebSocketSession session, FilePrintHelper filePrint)
 {
     try
     {
         if (base.useRaw)
         {
             return(base.Print(printer, url, session, filePrint));
         }
         Process.Start(
             Registry.LocalMachine.OpenSubKey(
                 @"SOFTWARE\Microsoft\Windows\CurrentVersion" +
                 @"\App Paths\AcroRd32.exe").GetValue("").ToString(),
             string.Format("/h /t \"{0}\" \"{1}\"", url, printer));
         return(true);
     }
     catch (Exception ex)
     {
         filePrint.SendErrorMsg(session, "Print", "Print pdf error", ex);
         Logger.Error("PDFPrinting.print =>", ex);
         return(false);
     }
 }
Esempio n. 7
0
        //private Microsoft.Office.Interop.Word.Document doc;
        // where did you get this file name?


        //public bool Print(string printer, string url)
        //{
        //    try
        //    {
        //        doc = word.Documents.Open(url, ReadOnly: true, Visible: false);

        //        object copies = "1";
        //        object pages = "";
        //        object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
        //        object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
        //        object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
        //        object oTrue = true;
        //        object oFalse = false;


        //        if (doc != null)
        //        {
        //            doc.Application.ActivePrinter = printer;
        //            doc.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
        //                    ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
        //                    ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
        //        }

        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Error("WORDPrinting.print =>", ex);
        //        return false;
        //    }
        //}



        // should be a singleton instance of wrapper for Word
        // the code below assumes this was set beforehand
        // (e.g. from another helper method)
        //private static Microsoft.Office.Interop.Word._Application _app;

        public new bool Print(string printer, string fileName, WebSocketSession session, FilePrintHelper filePrint)
        {
            try
            {
                if (base.useRaw)
                {
                    return(base.Print(printer, fileName, session, filePrint));
                }
                _session     = session;
                _filePrint   = filePrint;
                word.Visible = false;
                // Sometimes Word fails, so needs to be restarted.
                // Sometimes it's not Word's fault.
                // Either way, having this in a retry-loop is more robust.
                for (int retry = 0; retry < _exceptionLimit; retry++)
                {
                    if (TryOncePrintToSpecificPrinter(fileName, printer, retry))
                    {
                        break;
                    }

                    if (retry == _exceptionLimit - 1) // this was our last chance
                    {
                        // if it didn't have actual exceptions, but was not able to change the printer, we should notify somebody:
                        throw new Exception("Failed to change printer.");
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                filePrint.SendErrorMsg(session, "Print", "Print word error", ex);
                Logger.Error("WORDPrinting.print =>", ex);
                return(false);
            }
        }