// 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.IO; namespace Spludlow.Printing { public class Printer { private static Dictionary PrintMethods; static Printer() { PrintMethods = new Dictionary(); PrintMethods.Add("system", new string[] { "Spludlow", "Spludlow.Printing.GraphicsPrinter", "PrintPaper" }); PrintMethods.Add("bitmap", new string[] { "Spludlow", "Spludlow.Printing.GraphicsPrinter", "PrintBitmap" }); PrintMethods.Add("pdf", new string[] { "Spludlow.Drawing.PDF", "Spludlow.Drawing.PDF", "Print" }); } private static string[] SplitPrinterTypeInfo(string printerTypeInfo) { if (printerTypeInfo == null || printerTypeInfo.Length == 0) return new string[] { null, null }; int index = printerTypeInfo.IndexOf(":"); if (index == -1) return new string[] { printerTypeInfo, null }; string printerType = printerTypeInfo.Substring(0, index).Trim(); string printerInfo = printerTypeInfo.Substring(index + 1).Trim(); return new string[] { printerType, printerInfo }; } public static void PrintAt(string address, PrintDoc printDoc) { PrintAt(address, printDoc, null, null, false); } public static void PrintAt(string address, PrintDoc printDoc, string printerTypeInfo) { string[] parts = SplitPrinterTypeInfo(printerTypeInfo); PrintAt(address, printDoc, parts[0], parts[1], false); } public static void PrintAt(string address, PrintDoc printDoc, string printerType, string printerInfo) { PrintAt(address, printDoc, printerType, printerInfo, false); } public static void PrintAt(string address, PrintDoc printDoc, string printerType, string printerInfo, bool ignoreHistory) { string printData = printDoc.Save(); PrintAt(address, printData, printerType, printerInfo, ignoreHistory); } public static string[] Print(PrintDoc printDoc) { return Print(printDoc, null, null, false); } public static string[] Print(PrintDoc printDoc, string printerTypeInfo) { string[] parts = SplitPrinterTypeInfo(printerTypeInfo); return Print(printDoc, parts[0], parts[1], false); } public static string[] Print(PrintDoc printDoc, string printerType, string printerInfo) { return Print(printDoc, printerType, printerInfo, false); } public static string[] Print(PrintDoc printDoc, string printerType, string printerInfo, bool ignoreHistory) { string printData = printDoc.Save(); return Print(printData, printerType, printerInfo, ignoreHistory); } public static void PrintAt(string address, string printData) { PrintAt(address, printData, null, null, false); } public static void PrintAt(string address, string printData, string printerTypeInfo) { string[] parts = SplitPrinterTypeInfo(printerTypeInfo); PrintAt(address, printData, parts[0], parts[1], false); } public static void PrintAt(string address, string printData, string printerType, string printerInfo) { PrintAt(address, printData, printerType, printerInfo, false); } public static void PrintAt(string address, string printData, string printerType, string printerInfo, bool ignoreHistory) { // Ignore result as not returning filesnames array. Should do ??? Spludlow.Call.Queue(address, "Spludlow", "Spludlow.Printing.Printer", "Print", new object[] { printData, printerType, printerInfo, ignoreHistory }, CallFlags.LargeParameters | CallFlags.IgnoreResult); } public static string[] Print(string printData) { return Print(printData, null, null, false); } public static string[] Print(string printData, string printerTypeInfo) { string[] parts = SplitPrinterTypeInfo(printerTypeInfo); return Print(printData, parts[0], parts[1], false); } public static string[] Print(string printData, string printerType, string printerInfo) { return Print(printData, printerType, printerInfo, false); } public static string[] Print(string printData, string printerType, string printerInfo, bool ignoreHistory) { if (printerType == null || printerType.Length == 0) printerType = "dummy"; printerType = printerType.ToLower(); object result = null; if (printerType != "dummy") { if (PrintMethods.ContainsKey(printerType) == false) throw new ApplicationException("Printer Type unkown:\t" + printerType); string[] atm = PrintMethods[printerType]; result = Spludlow.Reflections.Invoke(atm[0], atm[1], atm[2], new object[] { printData, printerInfo }, null); } if (ignoreHistory == false) Spludlow.Printing.PrinterHistory.Submit(printData); if (result != null) return (string[])result; // ??? what does resulkt do ???? return null; } public static byte[] PreviewPage(string printDataFilename, int pageIndex, int dpi) { printDataFilename = Spludlow.WebServices.GetProgramDataTempFile(printDataFilename); try { string printData = File.ReadAllText(printDataFilename); printData = PickPages(printData, new int[] { pageIndex }); using (Spludlow.TempDirectory tempDir = new TempDirectory()) // Build further down ability to print to byte[] { string printerInfo = tempDir.Path + @"\Preview.png, " + dpi; string[] pageFilenames = Print(printData, "bitmap", printerInfo, true); if (pageFilenames.Length != 1) throw new ApplicationException("PreviewPage; Did not get 1 filename." + pageFilenames.Length); return File.ReadAllBytes(pageFilenames[0]); } } catch (Exception ee) { Spludlow.Log.Error("Print Preview Page", ee); return Spludlow.Drawing.Bitmaps.TextBitmap(ee.ToString(), "A4", dpi); } } public static string PickPages(string printData, int[] pageIndexes) { StringBuilder final = new StringBuilder(); List list = new List(pageIndexes); Spludlow.Printing.PrintDocHeader header = null; int sourcePageIndex = 0; int finalPageIndex = 0; using (StringReader reader = new StringReader(printData)) { string line; while ((line = reader.ReadLine()) != null) { if (header == null) { header = new PrintDocHeader(line); continue; } if (line.StartsWith("NewPage") == true) { ++sourcePageIndex; if (list.Contains(sourcePageIndex) == true && final.Length > 0) { final.AppendLine("NewPage\t" + (finalPageIndex + 1)); ++finalPageIndex; } } else { if (list.Contains(sourcePageIndex) == true) { final.AppendLine(line); continue; } } } } header.PageCount = finalPageIndex + 1; final.Insert(0, Environment.NewLine); final.Insert(0, header.Write()); string result = final.ToString(); return result; } } }