// Print a very simplistic label with the persons name on it private async void PrintLabel(string personName) { if (!string.IsNullOrWhiteSpace(personName)) { // license the app so we can use advanced features if (await ApplyScriptXLicenseAsync()) { try { // create a scriptx factory var factory = new ScriptX.Factory(); // get the printing object and configure required parameters ScriptX.printing printer = factory.printing; // select the printer and paper size. printer.printer = CmbPrinters.SelectedValue.ToString(); printer.paperSize = "A4"; printer.header = this.Title; printer.footer = "&D&b&b&P of &p"; // use some advanced features ... printer.SetMarginMeasure(2); // set units to inches printer.leftMargin = 1.5f; printer.topMargin = 1; printer.rightMargin = 1; printer.bottomMargin = 1; // build the html to print StringBuilder sHtml = new StringBuilder("html://"); sHtml.Append( "<!DOCTYPEe html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><title>ScriptX Sample</title></head><body>"); sHtml.Append("<table border=\"0\" width=\"100%\" height=\"100%\"><tr>"); sHtml.Append("<td align=\"center\">"); sHtml.Append("<h1>ScriptX Printing of HTML</h1><p>This label is for:</p><h2>"); sHtml.Append(personName); sHtml.Append("</h2><p>and was printed on:</p><h4>"); sHtml.Append(System.DateTime.Now.ToLongDateString()); sHtml.Append("</h4></td></tr></table></body></html>"); // and print it -- this job will be queued and printed // in an external process (so this call returns as soon // as the content has been stored in the queue). printer.PrintHTML(sHtml.ToString(), 0); } catch (COMException e) { MessageBox.Show("Printing failed: " + e.Message, this.Title); } } } else { MessageBox.Show("Please enter a name to print on the label", this.Title); } }
/// <summary> /// PrintOrPreview the document displayed in the WebBrowser. /// Adds the ScriptX factory to the page if required, or uses it if already there /// Defines custom headers and footers for the print and as we are licensed /// we can set margin measure (and could set a whole lot of other things!) /// /// Note that we dont add security manager to the html document, the app is licensed /// so it isnt needed. /// </summary> /// <param name="operation">Print or Preview?</param> private void PrintOrPreview(PrintOperation operation) { var document = (IHTMLDocument3)WebBrowser.Document; // 'de-facto' id is 'factory' var factoryElement = document.getElementById("factory"); // does the factory object exist? if (factoryElement == null) { // If not then create it. ((IHTMLDocument2)WebBrowser.Document).body.insertAdjacentHTML("beforeEnd", _factoryObjectHtml); factoryElement = document.getElementById("factory"); } if (factoryElement != null) { var factoryObject = (IHTMLObjectElement)factoryElement; // an element 'factory' exists, but is the object loaded (it may not be installed)? ScriptX.Factory factory = (ScriptX.Factory)factoryObject.@object; if (factory != null) { ScriptX.printing printer = factory.printing; printer.header = this.Title; printer.footer = "&D&b&b&P of &p"; // use some advanced features ... printer.SetMarginMeasure(2); // set units to inches printer.leftMargin = 1.5f; printer.topMargin = 1; printer.rightMargin = 1; printer.bottomMargin = 1; // and html headers/footer .... v7.7 and earlier only support allPagesHeader/Footer and firstPageHeader/Footer from // applications. var ef = factory.printing.enhancedFormatting; ef.allPagesHeader = "<div style='border: 1pt solid red; font: bold 12pt Arial; background: threedface; color: navy; padding-top: 5px; padding-bottom: 6px; background-image: url(http://www.meadroid.com/images/non_act_bg.jpg)'><i><center> --- Header for page <b> &p </b> ---</i></center></div>"; ef.allPagesFooter = "<div style='border: 1pt solid red; font: bold 12pt Arial; background: threedface; color: navy; padding-top: 5px; padding-bottom: 6px; background-image: url(http://www.meadroid.com/images/non_act_bg.jpg)'><i><center> --- Footer for page <b> &p </b> ---</i></center></div>"; switch (operation) { case PrintOperation.Print: printer.Print(false); // prompt will only be obeyed on intranet break; case PrintOperation.Preview: printer.Preview(); break; } // and we can wait untiol the job is spooled and so let the user know its on its way printer.WaitForSpoolingComplete(); MessageBox.Show("The print has completed.", this.Title); } else { MessageBox.Show("Unable to find or create MeadCo ScriptX.\n\nIs MeadCo ScriptX installed?", this.Title); } } }