// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Drawing.Printing; using System.Data; namespace Spludlow.Printing { public class PrinterSetting { public static string[] InstalledPrinters() { List list = new List(); foreach (string installedPrinter in PrinterSettings.InstalledPrinters) list.Add(installedPrinter); return list.ToArray(); } public static string DefaultPrinter() { foreach (string installedPrinter in PrinterSettings.InstalledPrinters) { using (PrintDocument printDocument = new PrintDocument()) { printDocument.PrinterSettings.PrinterName = installedPrinter; if (printDocument.PrinterSettings.IsDefaultPrinter == true) return installedPrinter; } } throw new ApplicationException("Default Printer not found."); } public static void LogPrinterReport() { Spludlow.Log.Report("Printer Report; All sources & sizes.", new object[] { PaperSources(), PaperSizes() }); } public static string[] PaperSourceLines(string host) { return (string[])Spludlow.Call.Now(host, "Spludlow", "Spludlow.Printing.PrinterSetting", "PaperSourceLines"); } public static string[] PaperSourceLines() { List results = new List(); foreach (DataRow row in PaperSources().Tables[0].Rows) { StringBuilder line = new StringBuilder(); line.Append((string)row["PrinterName"]); line.Append("\t"); line.Append((string)row["SourceName"]); line.Append("\t"); line.Append((string)row["Kind"]); results.Add(line.ToString()); } return results.ToArray(); } public static DataSet PaperSources(string host) { return (DataSet)Spludlow.Call.Now(host, "Spludlow", "Spludlow.Printing.PrinterSetting", "PaperSources"); } public static DataSet PaperSources() { DataTable table = Spludlow.Data.TextTable.ReadText("PaperSources", new string[] { "PrinterName SourceName Kind", "String String String", }); foreach (string printerName in InstalledPrinters()) { using (PrintDocument printDocument = new PrintDocument()) { SetPrinterName(printDocument, printerName); if (printDocument.PrinterSettings.PaperSources.Count == 0) table.Rows.Add(new object[] { printerName, "", "" }); foreach (PaperSource installedPaperSource in printDocument.PrinterSettings.PaperSources) { string kind = Enum.GetName(typeof(PaperKind), installedPaperSource.Kind); if (kind == null) kind = ""; table.Rows.Add(new object[] { printerName, installedPaperSource.SourceName, kind }); } } } return Spludlow.Data.ADO.WireDataSet(table); } public static string MakePrintableAreaConfigTemplate() { DataTable paperSourceTable = PaperSources().Tables[0]; StringBuilder text = new StringBuilder(); foreach (string printer in InstalledPrinters()) { text.Append(""); text.AppendLine(); text.AppendLine(); foreach (string orientation in new string[] { "P", "L" }) { text.Append(""); text.AppendLine(); } text.AppendLine(); foreach (string orientation in new string[] { "P", "L" }) { foreach (DataRow sourceRow in paperSourceTable.Rows) { if ((string)sourceRow["PrinterName"] != printer) continue; string sourceName = (string)sourceRow["SourceName"]; text.Append(""); text.AppendLine(); } } text.AppendLine(); } string configText = text.ToString(); Spludlow.Log.Report("MakePrintableAreaConfigTemplate", configText); return configText; } public static PointF ConfigPrinterOffset(PageSettings pageSettings) { return ConfigPrinterOffset(pageSettings.PrinterSettings.PrinterName, pageSettings.Landscape, pageSettings.PaperSource.SourceName); } public static PointF ConfigPrinterOffset(string printerName, bool landscape, string paperSource) { StringBuilder configKey = new StringBuilder(); configKey.Append("Spludlow.PrintMargin."); configKey.Append(printerName); configKey.Append("."); configKey.Append((landscape == false ? "P" : "L")); configKey.Append("."); configKey.Append(paperSource); string value; // Printer.Orin.Tray -> Printer.Orin -> Printer foreach (int trimKeyLength in new int[] { 0, paperSource.Length + 1, 2 }) { configKey.Length -= trimKeyLength; value = Spludlow.Config.Get(configKey.ToString(), true); if (value != null) return ParsePoint(value); } return new PointF(-1, -1); } public static PointF ParsePoint(string value) { string[] parts = value.Split(new char[] { ',' }); return new PointF(Single.Parse(parts[0]), Single.Parse(parts[1])); } public static DataSet PaperSizes(string host) { return (DataSet)Spludlow.Call.Now(host, "Spludlow", "Spludlow.Printing.PrinterSetting", "PaperSizes"); } public static DataSet PaperSizes() { DataTable table = Spludlow.Data.TextTable.ReadText("PaperSizes", new string[] { "PrinterName PaperName Kind", "String String String", }); foreach (string printerName in InstalledPrinters()) { using (PrintDocument printDocument = new PrintDocument()) { SetPrinterName(printDocument, printerName); foreach (PaperSize paperSize in printDocument.PrinterSettings.PaperSizes) { string kind = Enum.GetName(typeof(PaperKind), paperSize.Kind); if (kind == null) kind = ""; table.Rows.Add(new object[] { printerName, paperSize.PaperName, kind }); } } } return Spludlow.Data.ADO.WireDataSet(table); } public static PrintDocument CreatePrintDocument(string paperSize, bool landscape, string printerName, string paperSource) { PrintDocument printDocument = new PrintDocument(); SetPrinterName(printDocument, printerName); SetPaperSize(printDocument, paperSize, landscape); SetPaperSource(printDocument, paperSource); return printDocument; } private static void SetPrinterName(PrintDocument printDocument, string printerName) { if (printerName == null || printerName.Length == 0) printerName = DefaultPrinter(); printerName = printerName.ToLower(); string foundPrinterName = null; foreach (string installedPrinter in InstalledPrinters()) { if (installedPrinter.ToLower().Contains(printerName) == true) { if (foundPrinterName != null) throw new ApplicationException("Multiple Printer name matches: " + printerName); foundPrinterName = installedPrinter; } } if (foundPrinterName == null) throw new ApplicationException("Printer not found: " + printerName); else printDocument.PrinterSettings.PrinterName = foundPrinterName; } private static void SetPaperSize(PrintDocument printDocument, string paperSize, bool landscape) { if (paperSize == null || paperSize.Length == 0) // ?? paperSize = "A4"; paperSize = paperSize.ToUpper(); // May also find custom defined in windows, paper size will not have foreach (PaperSize installedPaperSize in printDocument.PrinterSettings.PaperSizes) { if (installedPaperSize.PaperName.ToUpper() == paperSize) { printDocument.DefaultPageSettings.PaperSize = installedPaperSize; printDocument.DefaultPageSettings.Landscape = landscape; return; } } if (paperSize.Contains("X") == true) { System.Drawing.SizeF size = Spludlow.Printing.PageSizes.PageSizeMillimeters(paperSize); // hundredths of an inch. int width = (int)Math.Round(size.Width / 0.254, 0); int height = (int)Math.Round(size.Height / 0.254, 0); PaperSize customSize = new PaperSize(paperSize, width, height); printDocument.DefaultPageSettings.PaperSize = customSize; printDocument.DefaultPageSettings.Landscape = landscape; //if (width > height) // printDocument.DefaultPageSettings.Landscape = true; Spludlow.Log.Info(paperSize + "#" + width + "#" + height + "#" + printDocument.DefaultPageSettings.Landscape); return; } throw new ApplicationException("Unable to set Paper Size: " + printDocument.PrinterSettings.PrinterName + ", \"" + paperSize + "\""); } public static void SetPaperSource(PrintDocument printDocument, string paperSource) { if (printDocument.PrinterSettings.PaperSources.Count == 1) { printDocument.DefaultPageSettings.PaperSource = printDocument.PrinterSettings.PaperSources[0]; return; } if (paperSource == null || paperSource.Length == 0) paperSource = "Auto"; //matically Select"; paperSource = paperSource.ToLower(); PaperSource foundPaperSource = null; foreach (PaperSource installedPaperSource in printDocument.PrinterSettings.PaperSources) { if (installedPaperSource.SourceName.ToLower().Contains(paperSource) == true) { if (foundPaperSource != null) throw new ApplicationException("Found multiple Paper Source matches: " + paperSource); foundPaperSource = installedPaperSource; } } if (foundPaperSource == null) Spludlow.Log.Warning("Unable to set Paper Source: " + printDocument.PrinterSettings.PrinterName + ", " + paperSource); else printDocument.DefaultPageSettings.PaperSource = foundPaperSource; } } }