468,769 Members | 2,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

Problem Printing Objects From A Web Page

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
<form runat="server">
<ASP:DropDownList id="ddlDebug"


<ASP:ListItem value="Debug">Stream
<ASP:ListItem value="NoDebug">No Stream
<ASP:Button id="button2" runat="server"
<ASP:ListBox id="listBox1" runat="server"

// Some of the code behind

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

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

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
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 *
e.Graphics.DrawString(Lline, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());

// e.HasMorePages = true;

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

// 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 - " +

foreach (System.Drawing.Printing.PaperSize paperSize in
if (paperSize.PaperName == "A3")

= paperSize;

myDoc.DefaultPageSettings.PrinterSettings.DefaultP ageSettings.Landscape
= true;

myDoc.PrinterSettings.FromPage = 1;
myDoc.PrinterSettings.MinimumPage = 1;
myDoc.PrintPage += new PrintPageEventHandler(myDoc_PrintPage);
Jun 27 '08 #1
0 1023

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Patrick De Ridder | last post: by
3 posts views Thread by Chrisitiaan | last post: by
7 posts views Thread by Amirallia | last post: by
2 posts views Thread by Sukh | last post: by
6 posts views Thread by Chris Dunaway | last post: by
7 posts views Thread by Iain Wilson | last post: by
43 posts views Thread by John | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.