// 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.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Data; using System.Text; public partial class Print : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Spludlow.WebControls.DisableOnClick(this); if (this.Page.IsPostBack == false) { foreach (string host in Spludlow.Config.Hosts()) { this.DropDownListHost.Items.Add(host); this.DropDownListTargetHost.Items.Add(host); } foreach (string printOutputType in new string[] { "system", "bitmap", "pdf" }) this.DropDownListPrintOutputTypes.Items.Add(printOutputType); foreach (int dpi in new int[] { 10, 15, 25, 50, 75, 150, 300, 450, 600 }) this.DropDownListDpi.Items.Add(dpi.ToString()); this.DropDownListDpi.SelectedIndex = 2; this._ShowGroups(); this._ShowReprint(); } } protected void ButtonRefreshHost_Click(object sender, EventArgs e) { this._ShowGroups(); } protected void DropDownListHost_SelectedIndexChanged(object sender, EventArgs e) { this._ShowGroups(); } private void _ShowGroups() { string host = this.DropDownListHost.SelectedValue; DataTable table = Spludlow.Printing.PrinterHistory.Query(host).Tables[0]; this.DropDownListGroup.DataSource = table; this.DropDownListGroup.DataValueField = "Filename"; this.DropDownListGroup.DataBind(); this.DropDownListGroup.SelectedIndex = table.Rows.Count - 1; // auto select last this.TextBoxPage.Text = "1"; this.ImagePreview2.Src = ""; this._ShowJobs(); } protected void DropDownListGroup_SelectedIndexChanged(object sender, EventArgs e) { this._ShowJobs(); } protected void ButtonRefreshGroup_Click(object sender, EventArgs e) { this._ShowJobs(); } private void _ShowJobs() { if (this.DropDownListGroup.SelectedIndex == -1) { this.DropDownListGroup.DataSource = null; this.DropDownListGroup.DataBind(); this.GridViewJobs.DataSource = null; this.GridViewJobs.DataBind(); return; } string filename = this.DropDownListGroup.SelectedValue; string host = this.DropDownListHost.SelectedValue; DataSet dataSet = Spludlow.Printing.PrinterHistory.QueryDay(host, filename, 0, 25); DataTable table = dataSet.Tables["Result"]; DataRow infoRow = dataSet.Tables["Info"].Rows[0]; table.Columns.Add("PageSizeName", typeof(string)); foreach (DataRow row in table.Rows) row["PageSizeName"] = Spludlow.Printing.PageSizes.PaperName((float)row["Width"], (float)row["Height"]); if (table.Rows.Count > 0) this.GridViewJobs.SelectedIndex = 0; // table.Rows.Count - 1; // auto select last this.GridViewJobs.DataSource = table; this.GridViewJobs.DataBind(); this.TextBoxPage.Text = "1"; this._ShowJob(); } protected void GridViewJobs_SelectedIndexChanged(object sender, EventArgs e) { this.TextBoxPage.Text = "1"; this._ShowJob(); } protected void ButtonPrevPage_Click(object sender, EventArgs e) { this._SetPageIndex(this._GetPageIndex() - 1); this._ShowJob(); } protected void ButtonNextPage_Click(object sender, EventArgs e) { this._SetPageIndex(this._GetPageIndex() + 1); this._ShowJob(); } protected void DropDownListDpi_SelectedIndexChanged(object sender, EventArgs e) { this._ShowJob(); } private void _ShowJob() { if (this.GridViewJobs.SelectedIndex == -1) { this.ImagePreview2.Src = ""; return; } int index = this.GridViewJobs.SelectedIndex; string filename = Spludlow.WebControls.KeyString(this.GridViewJobs, index, "Filename"); int pageCount = Spludlow.WebControls.KeyInt32(this.GridViewJobs, index, "PageCount"); string host = this.DropDownListHost.SelectedValue; int dpi = Int32.Parse(this.DropDownListDpi.SelectedValue); int pageIndex = this._GetPageIndex(); string name = Path.GetFileNameWithoutExtension(filename) + ".page" + (pageIndex + 1) + ".png"; string previewHostAddress = this._PreviewAddress(); StringBuilder url = new StringBuilder(); url.Append("Generic.aspx?"); url.Append("A=Spludlow&T=Spludlow.Printing.Printer&M=PreviewPage&MIME=png"); url.Append("&INLINE"); url.Append("&FILENAME="); url.Append(name); url.Append("&P0="); url.Append(HttpUtility.UrlEncode("@" + Path.GetFileName(filename))); url.Append("&P1="); url.Append(pageIndex); url.Append("&P2="); url.Append(dpi); this.ImagePreview2.Src = url.ToString(); this.TextBoxPage.Text = (pageIndex + 1).ToString(); this.LiteralPage.Text = "/" + pageCount; } private string _PreviewAddress() { int index; string requestHost = this.Request.Url.AbsoluteUri; index = requestHost.IndexOf("/", 8); requestHost = requestHost.Substring(0, index); string previewHostAddress = Spludlow.Config.HostAddress(Environment.MachineName, true); index = previewHostAddress.IndexOf("/", 8); previewHostAddress = previewHostAddress.Substring(index); return requestHost + previewHostAddress; } protected void DropDownListTargetHost_SelectedIndexChanged(object sender, EventArgs e) { this._ShowReprint(); } protected void DropDownListPrintOutputTypes_SelectedIndexChanged(object sender, EventArgs e) { this._ShowReprint(); } private void _ShowReprint() { string printHost = this.DropDownListTargetHost.SelectedValue; string outputType = this.DropDownListPrintOutputTypes.SelectedValue; this.DropDownListPrintSource.Visible = false; this.TextBoxPrintInfo.Visible = false; this.DropDownListPrintSource.Items.Clear(); switch (outputType) { case "system": this.DropDownListPrintSource.Visible = true; foreach (string paperSource in Spludlow.Printing.PrinterSetting.PaperSourceLines(printHost)) this.DropDownListPrintSource.Items.Add(paperSource); break; case "bitmap": this.TextBoxPrintInfo.Visible = true; this.TextBoxPrintInfo.Text = Spludlow.Config.ProgramData + @"\RePrint.png"; break; case "pdf": this.TextBoxPrintInfo.Visible = true; this.TextBoxPrintInfo.Text = Spludlow.Config.ProgramData + @"\RePrint.pdf"; break; } } protected void ButtonRePrint_Click(object sender, EventArgs e) // Put repint logic in Printing class { if (this.GridViewJobs.SelectedIndex == -1) { this.LabelError.Text = "Please select an item to reprint."; return; } int index = this.GridViewJobs.SelectedIndex; string host = this.DropDownListHost.SelectedValue; string filename = Spludlow.WebControls.KeyString(this.GridViewJobs, index, "Filename"); int pageCount = Spludlow.WebControls.KeyInt32(this.GridViewJobs, index, "PageCount"); string printHost = this.DropDownListTargetHost.SelectedValue; string outputType = this.DropDownListPrintOutputTypes.SelectedValue; string printData = Spludlow.Call.DownloadText(host, filename); if (this.CheckBoxSinglePage.Checked == true) printData = Spludlow.Printing.Printer.PickPages(printData, new int[] { this._GetPageIndex() }); string printInfo; if (outputType == "system") { string source = this.DropDownListPrintSource.SelectedValue; string[] words = Spludlow.Text.Split(source, '\t'); if (words.Length < 2) throw new ApplicationException("Did not find printer and paper source in text:\t" + source); printInfo = words[0] + ", " + words[1]; } else { printInfo = this.TextBoxPrintInfo.Text; } Spludlow.Printing.Printer.PrintAt(printHost + ":Run", printData, outputType, printInfo); this.LabelError.Text = "Re-Print Sent: " + DateTime.Now; } private int _GetPageIndex() { return Int32.Parse(this.TextBoxPage.Text) - 1; } private void _SetPageIndex(int index) { this.TextBoxPage.Text = (index + 1).ToString(); } }