Exemplo n.º 1
0
        // ========== Print File ==========

        /*public void PrintFile(Byte[] data, string printerProfileName = "Default")
         * {
         *      PrinterProfile printerProfile = this.GetPrinterProfile(printerProfileName);
         *      Program.LogAlert("Printer", "Printing label with printer: " + printerProfile.GetPrintQueue().Name + "...");
         *      Application.Invoke(delegate
         *      {
         *              using (PrintSystemJobInfo job = printerProfile.GetPrintQueue().AddJob())
         *              using (Stream stream = job.JobStream)
         *              {
         *                      stream.Write(data, 0, data.Length);
         *              }
         *      });
         * }*/


        // ========== Print PDF ==========
        public void PrintPDF(string filePath, string printerProfileName = "Default")
        {
            PrinterProfile printerProfile = this.GetPrinterProfile(printerProfileName);

            Program.LogAlert("Printer", "Printing PDF label with printer: " + printerProfile.GetPrinterName() + "...");
            Application.Invoke(delegate {
                try {
                    if (System.Environment.OSVersion.ToString().ToLower().Contains("windows"))
                    {
                        PdfDocument pdf        = PdfDocument.Load(filePath);
                        PrintDocument printDoc = pdf.CreatePrintDocument();
                        if (printerProfile.PageWidth > 0 && printerProfile.PageHeight > 0)
                        {
                            Program.Log("Printer", "Using custom paper size: " + printerProfile.PageWidth + "x" + printerProfile.PageHeight + " (100th of an inch).");
                            printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom Size", printerProfile.PageWidth, printerProfile.PageHeight);
                        }
                        printDoc.PrinterSettings.PrinterName = printerProfile.GetPrinterName();
                        printDoc.Print();
                        pdf.Dispose();
                    }
                    else
                    {
                        Process process            = new Process();
                        process.StartInfo.FileName = "lp";
                        if (printerProfile.GetPrinterName() == "System Default")
                        {
                            process.StartInfo.Arguments = filePath;
                        }
                        else
                        {
                            process.StartInfo.Arguments = "-d \"" + printerProfile.GetPrinterName() + "\" \"" + filePath + "\"";
                        }
                        process.StartInfo.UseShellExecute        = false;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError  = true;
                        process.StartInfo.RedirectStandardInput  = true;
                        Program.LogAlert("Printer", "Starting Linux Print: " + process.StartInfo.FileName + " " + process.StartInfo.Arguments);
                        process.Start();
                        process.WaitForExit();
                    }
                }
                catch (Exception e) {
                    Program.LogError("Printer", "An error occured when attempting to print PDF.");
                    Program.LogException(e);
                }
            });
        }
Exemplo n.º 2
0
        // ========== Print PNG ==========
        public void PrintPNG(string filePath, string printerProfileName = "Default")
        {
            PrinterProfile printerProfile = this.GetPrinterProfile(printerProfileName);

            printerProfileName = printerProfile.Name;
            string printerName = printerProfile.GetPrinterName();

            Program.LogAlert("Printer", "Printing PNG label with printer: " + printerName + "...");
            Application.Invoke(delegate
            {
                try
                {
                    PrintDocument printDoc = new PrintDocument();
                    printDoc.DefaultPageSettings.PrinterSettings.PrinterName = printerName;
                    printDoc.DefaultPageSettings.Landscape = false;
                    printDoc.PrintPage += new PrintPageEventHandler(delegate(object o, PrintPageEventArgs e) {
                        System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);
                        Point p = new Point(2, 2);
                        e.Graphics.DrawImage(img, p);
                    });

                    /*printDoc.PrintPage += (sender, args) => {
                     *      double scale = printerProfile.ImageScale;
                     *      System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);
                     *      //Point m = new Point(printerProfile.ImageOffsetX, printerProfile.ImageOffsetY);
                     *      Rectangle m = args.MarginBounds;
                     *      if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) {
                     *              m.Height = (int)(((double)img.Height / (double)img.Width * (double)m.Width) * scale);
                     *      }
                     *      else {
                     *              m.Width = (int)(((double)img.Width / (double)img.Height * (double)m.Height) * scale);
                     *      }
                     *      args.Graphics.DrawImage(img, m);
                     * };*/
                    printDoc.Print();
                }
                catch (Exception e)
                {
                    Program.LogError("Printer", "An error occured when attempting to print PNG. Profile: " + printerProfileName + " Printer Name: " + printerName + " File Path: " + filePath);
                    Program.LogException(e);
                }
            }
                               );
        }