Printing a document without viewing

Last post 08-18-2003 5:32 AM by aw13. 10 replies.

Sort Posts:

  • Printing a document without viewing

    07-04-2003, 6:49 PM
    I have a document that I want to print, but I dont want to display it on the screen, but just give its name or something. How do I send the command to process this request? Please help !
  • Re: Printing a document without viewing

    07-05-2003, 11:30 AM
    • Contributor
      3,505 point Contributor
    • firoz.ansari
    • Member since 11-25-2002, 9:29 AM
    • Pune, India
    • Posts 694
    You just have to embed WebBrowser object into the page. Here is the code for that. It will not promt user for any print dialog box and will print the document to the default page.


    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>;';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = "Test Content";
    }

    Through above code you can achieve print the content which is not displayed on the browser. In above case its "Test Content".

    Change the value of the outerHTML property based on the content you want to print. Please NOTE that it will not prompt for any print dialog box. It will simply print the content to the default printer.


    Regards,

    Firoz
  • Re: Printing a document without viewing

    07-05-2003, 1:59 PM
    how do i get it to do the same thing, but provide the print dialog box?
  • Re: Printing a document without viewing

    07-05-2003, 2:23 PM
    I have never used the web broser object before, so when I tried it, I couldnt get it to work. Ive included my code below, which is just supposed to print the Test Content when the submit button is clicked. Can you please point me in the right direction? Thanks !



    <%@ Page Explicit="True" Language="VB" Debug="True" %>
    <html>
    <script runat="server">
    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>;';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = "Test Content";
    }
    </script>
    <body>
    <form>
    WEB PAGE CONTENT
    <asp:imagebutton id="Submit" imageurl="submit.gif" onclick="Submit_Click" runat="server" />
    </form>
    </body>

  • Re: Printing a document without viewing

    07-05-2003, 2:32 PM
    • Contributor
      3,505 point Contributor
    • firoz.ansari
    • Member since 11-25-2002, 9:29 AM
    • Pune, India
    • Posts 694
    You need to set one attribute in the Page_Load event of the page

    Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Submit.Attributes.Add("onclick", "return PrintIt();")
    End Sub


    And modifiy ASPX file script as

    <script runat="server">
    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>;';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = "Test Content";
    return false;
    }
    </script>


    With that remove onclick="Submit_Click" event from the Submit button
    I am sure this will work now
  • Re: Printing a document without viewing

    07-05-2003, 4:04 PM
    I think I am starting to understand now. But I am getting an error on the "{" line:

    Compiler Error Message: BC30035: Syntax error.
    Source Error:
    Line 5:
    Line 6: function PrintIt()
    Line 7: {

    My code is as follows (and thank you for your help so far):


    <%@ Page Explicit="True" Language="VB" Debug="True" %>
    <html>
    <script runat="server">
    Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Submit.Attributes.Add("onclick", "return PrintIt();")
    End Sub

    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>;';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = "Test Content";
    return false;
    }
    </script>
    <body>
    <form>
    WEB PAGE CONTENT
    <asp:imagebutton id="Submit" imageurl="submit.gif" runat="server" />
    </form>
    </body>



  • Re: Printing a document without viewing

    07-06-2003, 1:36 AM
    • Contributor
      3,505 point Contributor
    • firoz.ansari
    • Member since 11-25-2002, 9:29 AM
    • Pune, India
    • Posts 694
    Modify the code as

    <script runat="server">
    Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Submit.Attributes.Add("onclick", "return PrintIt();")
    End Sub
    </script>


    <script>
    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>;';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = "Test Content";
    return false;
    }
    </script>


    PrintIt() is the javascript function. So it must NOT run with the runat="server".
  • Re: Printing a document without viewing

    07-06-2003, 2:09 AM
    works great ! now I have an additional question - can this same code be used so that a document located on the server can be printed, or is different code required to do so? how can that be done?
  • Re: Printing a document without viewing

    07-06-2003, 2:41 AM
    • Contributor
      3,505 point Contributor
    • firoz.ansari
    • Member since 11-25-2002, 9:29 AM
    • Pune, India
    • Posts 694
    Yes! I am sure the same code can be use to print document located at the server.
    What you have to do is to put one IFRAME (that should be invisible) in the page. and set the src of that IFRAME to the document url which is located at the server.

    IFRAME can be make invisible by setting its HEIGHT and WIDTH to 0.

    <iframe id="fraDocument" style="WIDTH: 0px; HEIGHT: 0px" src="document1.htm"></iframe>


    Now
    Modify the previous javascript code as

    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>;';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = fraDocument.innerHTML;
    }
    </script>



    Though I didnt test above "hypothesis" :) . But I am sure that above approach will work.
    Please let me know if you have succeed in printing server docuement without "displaying" it on the browser.

    Regards
  • Re: Printing a document without viewing

    07-06-2003, 8:43 AM
    • Contributor
      3,505 point Contributor
    • firoz.ansari
    • Member since 11-25-2002, 9:29 AM
    • Pune, India
    • Posts 694
    There is little modification in the above code

    <script>
    function PrintIt()
    {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, -1);
    WebBrowser1.outerHTML = fraDocument.document.body.innerHTML;
    return false;
    }
    </script>


    I have tested the above code and its working perfectly.
  • Re: Printing a document without viewing

    08-18-2003, 4:45 AM
    • Member
      5 point Member
    • aw13
    • Member since 08-18-2003, 12:16 AM
    • Posts 1
    Hi there,
    I've tried ur codes but it prints the page displayed on screen instead of printing the page inside the IFRAME. Do u have any idea why? Thanks alot.
Page 1 of 1 (11 items)