// ========== Save ========== protected void Save() { PrinterProfile printerProfile = Program.printer.GetPrinterProfile("Default"); //printerProfile.SetPrintQueue (this.PrinterSelectionCombo.ActiveText); printerProfile.SetPrinterName(this.PrinterSelectionCombo.ActiveText); Program.printer.SaveConfig(); }
// ========== Select Profile ========== protected void SelectProfile(string profileName) { this.SelectedProfile = Program.printer.GetPrinterProfile (profileName); this.PrinterProfileNameEntry.Text = this.SelectedProfile.Name; this.PrinterProfileScaleEntry.Text = this.SelectedProfile.ImageScale.ToString (); this.PrinterProfilePageWidthEntry.Text = this.SelectedProfile.PageWidth.ToString(); this.PrinterProfilePageHeightEntry.Text = this.SelectedProfile.PageHeight.ToString(); this.PrinterProfileNameEntry.IsEditable = this.SelectedProfile.Name != "Default"; this.LoadPrinters (); }
// ========== Add Profile ========== protected void OnPrinterProfileAddReleased(object sender, EventArgs e) { try { PrinterProfile newProfile = new PrinterProfile ("New Profile " + ++this.NewProfileCount); Program.printer.AddPrinterProfile (newProfile); this.PrinterProfileSelectionCombo.AppendText (newProfile.Name); this.SelectProfile (newProfile.Name); TreeIter iter; this.PrinterProfileSelectionCombo.Model.IterNthChild(out iter, this.PrinterProfileSelectionCombo.Model.IterNChildren () - 1); this.PrinterProfileSelectionCombo.SetActiveIter(iter); } catch (Exception ex) { Program.LogException(ex); } }
// ========== Remove Profile ========== protected void OnPrinterProfileRemoveButtonReleased(object sender, EventArgs e) { try { string removeProfileName = this.SelectedProfile.Name; if (removeProfileName == "Default") { Program.LogWarning ("Printer", "Cannot remove the Default printer profile."); return; } Program.printer.RemovePrinterProfile(removeProfileName); this.SelectedProfile = null; this.LoadProfiles (); } catch (Exception ex) { Program.LogException(ex); } }
// ========== 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); } }); }
// ========== 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); } } ); }
// ========== Load Profiles ========== protected void LoadProfiles() { if (this.SelectedProfile == null) this.SelectedProfile = Program.printer.GetPrinterProfile("Default"); this.PrinterProfileSelectionCombo.Model = new ListStore(typeof(string), typeof(string)); this.PrinterProfileSelectionCombo.AppendText("Default"); int profileIndex = 0; int index = 0; foreach (string profileName in Program.printer.PrinterProfiles.Keys) { if (profileName != "Default") this.PrinterProfileSelectionCombo.AppendText(profileName); if (profileName == this.SelectedProfile.Name) profileIndex = index; index++; } TreeIter iter; this.PrinterProfileSelectionCombo.Model.IterNthChild(out iter, profileIndex); this.PrinterProfileSelectionCombo.SetActiveIter(iter); }
// ========== Load Config ========== public void LoadConfig() { this.Config = ConfigPrinter.LoadFile(); // Create Default If Empty: if (this.Config.PrinterProfileEntries.Length == 0) { PrinterProfile.ConfigPrinterProfile printerProfileConfig = new PrinterProfile.ConfigPrinterProfile(); this.Config.PrinterProfileEntries = new PrinterProfile.ConfigPrinterProfile[] { printerProfileConfig }; } // Load Profiles: bool hasDefault = false; foreach (PrinterProfile.ConfigPrinterProfile printerProfileConfig in this.Config.PrinterProfileEntries) { if (printerProfileConfig.ProfileName == "Default") { hasDefault = true; } PrinterProfile printerProfile = new PrinterProfile(printerProfileConfig.ProfileName); //printerProfile.SetPrintQueue (printerProfileConfig.PrinterName); printerProfile.SetPrinterName(printerProfileConfig.PrinterName); printerProfile.ImageScale = printerProfileConfig.ImageScale; printerProfile.PageWidth = printerProfileConfig.PageWidth; printerProfile.PageHeight = printerProfileConfig.PageHeight; this.PrinterProfiles.Add(printerProfile.Name, printerProfile); } // Create Default If Missing: if (!hasDefault) { PrinterProfile printerProfile = new PrinterProfile("Default"); //printerProfile.SetPrintQueue(DefaultPrinterName); printerProfile.SetPrinterName(DefaultPrinterName); this.PrinterProfiles.Add(printerProfile.Name, printerProfile); } }
// ========== Add Printer Profile ========== public void AddPrinterProfile(PrinterProfile profile) { this.PrinterProfiles.Add(profile.Name, profile); this.SaveConfig(); }