Problem Printing objects from web page

I

Iain Wilson

I am pulling my hair out trying to print various objects from a .net
web page
My apologies for cross posting but I need an answer and my previous
post has attracted no interest.

ASP.Net 2.0 using C#

This is a simple web page on our intranet (written as a test) and all
I am trying to do is print a document to a named printer.
If I use a StreamReader then I get an output to the printer. This
prints all the lines in the text file to the printer. However, this
only works for basic text files. I cannot achieve an output for an
Excel spreadsheet, a MS Word document or a pdf document.

If I choose not to use a StreamReader then all I get is a blank sheet
of paper no matter what type of file I try to print.

Can anyone please help as I am getting desperate as I need to create a
complex web based printing application very soon.


// A Simple HTML Page For A test web based application
<body>
<form runat="server">
<p>
<ASP:DropDownList id="ddlDebug"
runat="server"
AppendDataBoundItems="True"
AutoPostBack="True">
<ASP:ListItem value="Debug">Stream
</ASP:ListItem>
<ASP:ListItem value="NoDebug">No Stream
</ASP:ListItem>
</ASP:DropDownList>
</p>
<p>
<ASP:Button id="button2" runat="server" text="Button">
</ASP:Button>
</p>
<p>
<ASP:ListBox id="listBox1" runat="server" width="1156px"
height="442px">
</ASP:ListBox>
</p></font>
</form>
</body>
</html>

// Some of the code behind

if (! IsPostBack)
{
if(impersonateValidUser("Administrator", "domain",
"password"))
{
//Insert your code that runs under the security context of a
specific user here.

}
else
{
//Your impersonation failed. Therefore, include a fail-safe
mechanism here.
}
}
else
{
}
}


void myDoc_PrintPage(object sender, PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
topMargin = topMargin + 50;

// When I try a direct print without Streaming I remove this
block of code
using (StreamReader streamToPrint = new
StreamReader(myDoc.DocumentName))
{
string Lline;
// Read and display lines from the file until the end of the
file is reached.
while ((Lline = streamToPrint.ReadLine()) != null)
{
listBox1.Items.Add(Lline); // For debug purposes - DIsplay
to browser listbox - Remove Later
topMargin = topMargin + 20;
yPos = topMargin + (count *
printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(Lline, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
}
}

// e.HasMorePages = true;
}

private void button2_Click(object sender, System.EventArgs e)
{

listBox1.Items.Clear();
// Try and print the document
myDoc.DocumentName = @"\\MyPc\Temp\MyTestDoc.txt";

listBox1.Items.Add("Document Name - " + myDoc.DocumentName);

myDoc.PrinterSettings.PrinterName = LPrinterName;
listBox1.Items.Add("Printer Name - " +
myDoc.PrinterSettings.PrinterName);

foreach (System.Drawing.Printing.PaperSize paperSize in
myDoc.PrinterSettings.PaperSizes)
{
if (paperSize.PaperName == "A3")
{

myDoc.DefaultPageSettings.PaperSize
= paperSize;

myDoc.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape
= true;
}
}

myDoc.PrinterSettings.FromPage = 1;
myDoc.PrinterSettings.MinimumPage = 1;
myDoc.PrintPage += new PrintPageEventHandler(myDoc_PrintPage);
myDoc.Print();
}
 
H

Hans Kesting

Iain Wilson expressed precisely :
I am pulling my hair out trying to print various objects from a .net
web page
My apologies for cross posting but I need an answer and my previous
post has attracted no interest.

ASP.Net 2.0 using C#

This is a simple web page on our intranet (written as a test) and all
I am trying to do is print a document to a named printer.
If I use a StreamReader then I get an output to the printer. This
prints all the lines in the text file to the printer. However, this
only works for basic text files. I cannot achieve an output for an
Excel spreadsheet, a MS Word document or a pdf document.

You can't just throw "some" file at the printer and expect that the
printer will make a nice printout.
For an Excel spreadsheet you need some program (Excel probably, but
OperOffice.org would also work) that knows that filetype and can
generate the correct print commands. And so on for other filetypes.
So at least you needs those programs installed on the server, find the
correct one to use and have that print your file.

A guess: Explorer sometimes has a "print" option in the context menu
that does this. Maybe you can make use of that?

Hans Kesting
 
R

rosshinkley

I am pulling my hair out trying to print various objects from a .net
web page
My apologies for cross posting but I need an answer and my previous
post has attracted no interest.

ASP.Net 2.0 using C#

This is a simple web page on our intranet (written as a test) and all
I am trying to do is print a document to a named printer.
If I use a StreamReader then I get an output to the printer. This
prints all the lines in the text file to the printer. However, this
only works for basic text files. I cannot achieve an output for an
Excel spreadsheet, a MS Word document or a pdf document.

If I choose not to use a StreamReader then all I get is a blank sheet
of paper no matter what type of file I try to print.

Can anyone please help as I am getting desperate as I need to create a
complex web based printing application very soon.

  // A Simple HTML Page For A test web based application
  <body>
         <form runat="server">
           <p>
                 <ASP:DropDownList id="ddlDebug"
                                                   runat="server"
                                                   AppendDataBoundItems="True"
                                                   AutoPostBack="True">
                   <ASP:ListItem value="Debug">Stream
                   </ASP:ListItem>
                   <ASP:ListItem value="NoDebug">No Stream
                   </ASP:ListItem>
                 </ASP:DropDownList>
           </p>
           <p>
                 <ASP:Button id="button2" runat="server" text="Button">
                 </ASP:Button>
           </p>
           <p>
                 <ASP:ListBox id="listBox1" runat="server" width="1156px"
height="442px">
                 </ASP:ListBox>
           </p></font>
         </form>
  </body>
</html>

  // Some of the code behind

      if (! IsPostBack)
        {
        if(impersonateValidUser("Administrator", "domain",
"password"))
          {
          //Insert your code that runs under the security context of a
specific user here.

          }
        else
          {
          //Your impersonation failed. Therefore, include a fail-safe
mechanism here.
          }
        }
      else
        {
        }
      }

    void myDoc_PrintPage(object sender, PrintPageEventArgs e)
      {
      float linesPerPage = 0;
      float yPos = 0;
      int count = 0;
      float leftMargin = e.MarginBounds.Left;
      float topMargin = e.MarginBounds.Top;
      topMargin = topMargin + 50;

      // When I try a direct print without Streaming I remove this
block of code
      using (StreamReader streamToPrint = new
StreamReader(myDoc.DocumentName))
        {
        string Lline;
        // Read and display lines from the file until the end of the
file is reached.
        while ((Lline = streamToPrint.ReadLine()) != null)
          {
          listBox1.Items.Add(Lline); // For debug purposes - DIsplay
to browser listbox - Remove Later
          topMargin = topMargin + 20;
          yPos = topMargin + (count *
printFont.GetHeight(e.Graphics));
          e.Graphics.DrawString(Lline, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
          }
        }

//      e.HasMorePages = true;
      }

    private void button2_Click(object sender, System.EventArgs e)
      {

      listBox1.Items.Clear();
      // Try and print the document
      myDoc.DocumentName = @"\\MyPc\Temp\MyTestDoc.txt";

      listBox1.Items.Add("Document Name - " + myDoc.DocumentName);

      myDoc.PrinterSettings.PrinterName  = LPrinterName;
      listBox1.Items.Add("Printer Name - " +
myDoc.PrinterSettings.PrinterName);

      foreach (System.Drawing.Printing.PaperSize paperSize in
myDoc.PrinterSettings.PaperSizes)
        {
        if (paperSize.PaperName == "A3")
          {

myDoc.DefaultPageSettings.PaperSize
= paperSize;

myDoc.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape
= true;
          }
        }

      myDoc.PrinterSettings.FromPage = 1;
      myDoc.PrinterSettings.MinimumPage = 1;
      myDoc.PrintPage += new PrintPageEventHandler(myDoc_PrintPage);
      myDoc.Print();
      }

Honestly, it depends on what you're attempting to accomplish. If a
modal print screen does not affect your workflow, then I would move to
put a snippet of javascript in that calls the print method. I can't
remember how to do it off the top of my head, but I could look it up
if you'd like.

I spent a _LOT_ of time trying to get IE (or any web browser) to print
via some sort of automated process in landscape. With CSS 3 (IIRC)
you can specify orientation as a style tag, but it's not implemented
as far as I know.

The easiest way I found was to spool the page to PDF and printing that
using abcPDF. Again, I can't remember how to do it off the top of the
ol' noggin, but if you're interested, I'll dig up an example.

You could also try to use Windows handles to IE if you're uneasy about
the PDF route. But I will warn you: code you write for IE6 will not
work for IE7 and vice versa. You'll have to do some sort of logic to
decide which set of handles to use based on the IE version (which you
can get from either FileInfo or the registry). This is hairy, and I
decided after thinking about it that the PDF route would work no
matter what.

Hope this helps,
Ross
 
F

Family Tree Mike

Did you try the previous response I gave regarding System.Diagnostic.Process
using the file and having StartInfo.Verb="Print"?
 
I

Iain Wilson

Hi Mike

No I have not. I lost track of where I had posted the question and
only discovered your answer this afternoon after I had left the
office. I will try it on Monday. Thanks for the advice.

Iain
 
H

Hans Kesting

Iain Wilson wrote on 16-6-2008 :
Hi Mike

I was looking at the online documentation/examples. See the link

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.startinfo.aspx

Is this not for win32 applications ? I do not see any mention of web
based compatability.

What I am looking for is to print from a web based application.

Am I mis-understanding something here ?

Many thanks for your reply and assistance

Best regards

Iain

Where are you trying to print?
On the server - you can use the same code as for a "winform"
application, just no dialog boxes and similar UI-specific things.
On the client (browser) - you can't do much and you need at least a
viewer-application for the types of documents you want to print.

Hans Kesting
 
I

Iain Wilson

Hello Hans

Thanks for that.

It clears up a bit of confusion for me.

I will print from the server.

I will play around with a few examples and see how I get on

Thanks to all who have contributed.
I will post back here when I work out the solution

Best regards

Iain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top