473,544 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Printing objects from 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
<body>
<form runat="server">
<p>
<ASP:DropDownLi st id="ddlDebug"
runat="server"
AppendDataBound Items="True"
AutoPostBack="T rue">
<ASP:ListItem value="Debug">S tream
</ASP:ListItem>
<ASP:ListItem value="NoDebug" >No Stream
</ASP:ListItem>
</ASP:DropDownLis t>
</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(impersonateV alidUser("Admin istrator", "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, PrintPageEventA rgs 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(my Doc.DocumentNam e))
{
string Lline;
// Read and display lines from the file until the end of the
file is reached.
while ((Lline = streamToPrint.R eadLine()) != null)
{
listBox1.Items. Add(Lline); // For debug purposes - DIsplay
to browser listbox - Remove Later
topMargin = topMargin + 20;
yPos = topMargin + (count *
printFont.GetHe ight(e.Graphics ));
e.Graphics.Draw String(Lline, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat()) ;
}
}

// e.HasMorePages = true;
}

private void button2_Click(o bject sender, System.EventArg s e)
{

listBox1.Items. Clear();
// Try and print the document
myDoc.DocumentN ame = @"\\MyPc\Temp\M yTestDoc.txt";

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

myDoc.PrinterSe ttings.PrinterN ame = LPrinterName;
listBox1.Items. Add("Printer Name - " +
myDoc.PrinterSe ttings.PrinterN ame);

foreach (System.Drawing .Printing.Paper Size paperSize in
myDoc.PrinterSe ttings.PaperSiz es)
{
if (paperSize.Pape rName == "A3")
{

myDoc.DefaultPa geSettings.Pape rSize
= paperSize;

myDoc.DefaultPa geSettings.Prin terSettings.Def aultPageSetting s.Landscape
= true;
}
}

myDoc.PrinterSe ttings.FromPage = 1;
myDoc.PrinterSe ttings.MinimumP age = 1;
myDoc.PrintPage += new PrintPageEventH andler(myDoc_Pr intPage);
myDoc.Print();
}
Jun 27 '08 #1
7 2551
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
Jun 27 '08 #2
On Jun 13, 8:10*am, Iain Wilson <Email.IainWil. ..@gmail.comwro te:
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:DropDownL ist id="ddlDebug"
* * * * * * * * * * * * * * * * * * * * * * * * * *runat="server"
* * * * * * * * * * * * * * * * * * * * * * * * * *AppendDataBoun dItems="True"
* * * * * * * * * * * * * * * * * * * * * * * * * *AutoPostBack=" True">
* * * * * * * * * *<ASP:ListItem value="Debug">S tream
* * * * * * * * * *</ASP:ListItem>
* * * * * * * * * *<ASP:ListItem value="NoDebug" >No Stream
* * * * * * * * * *</ASP:ListItem>
* * * * * * * * *</ASP:DropDownLis t>
* * * * * *</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(impersonateV alidUser("Admin istrator", "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, PrintPageEventA rgs 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(my Doc.DocumentNam e))
* * * * {
* * * * string Lline;
* * * * // Read and display lines from the file until the end of the
file is reached.
* * * * while ((Lline = streamToPrint.R eadLine()) != null)
* * * * * {
* * * * * listBox1.Items. Add(Lline); // For debug purposes - DIsplay
to browser listbox - Remove Later
* * * * * topMargin = topMargin + 20;
* * * * * yPos = topMargin + (count *
printFont.GetHe ight(e.Graphics ));
* * * * * e.Graphics.Draw String(Lline, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat()) ;
* * * * * }
* * * * }

// * * *e.HasMorePages = true;
* * * }

* * private void button2_Click(o bject sender, System.EventArg s e)
* * * {

* * * listBox1.Items. Clear();
* * * // Try and print the document
* * * myDoc.DocumentN ame = @"\\MyPc\Temp\M yTestDoc.txt";

* * * listBox1.Items. Add("Document Name - " + myDoc.DocumentN ame);

* * * myDoc.PrinterSe ttings.PrinterN ame *= LPrinterName;
* * * listBox1.Items. Add("Printer Name - " +
myDoc.PrinterSe ttings.PrinterN ame);

* * * foreach (System.Drawing .Printing.Paper Size paperSize in
myDoc.PrinterSe ttings.PaperSiz es)
* * * * {
* * * * if (paperSize.Pape rName == "A3")
* * * * * {

myDoc.DefaultPa geSettings.Pape rSize
= paperSize;

myDoc.DefaultPa geSettings.Prin terSettings.Def aultPageSetting s.Landscape
= true;
* * * * * }
* * * * }

* * * myDoc.PrinterSe ttings.FromPage = 1;
* * * myDoc.PrinterSe ttings.MinimumP age = 1;
* * * myDoc.PrintPage += new PrintPageEventH andler(myDoc_Pr intPage);
* * * 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
Jun 27 '08 #3
Did you try the previous response I gave regarding System.Diagnost ic.Process
using the file and having StartInfo.Verb= "Print"?

"Iain Wilson" wrote:
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:DropDownLi st id="ddlDebug"
runat="server"
AppendDataBound Items="True"
AutoPostBack="T rue">
<ASP:ListItem value="Debug">S tream
</ASP:ListItem>
<ASP:ListItem value="NoDebug" >No Stream
</ASP:ListItem>
</ASP:DropDownLis t>
</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(impersonateV alidUser("Admin istrator", "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, PrintPageEventA rgs 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(my Doc.DocumentNam e))
{
string Lline;
// Read and display lines from the file until the end of the
file is reached.
while ((Lline = streamToPrint.R eadLine()) != null)
{
listBox1.Items. Add(Lline); // For debug purposes - DIsplay
to browser listbox - Remove Later
topMargin = topMargin + 20;
yPos = topMargin + (count *
printFont.GetHe ight(e.Graphics ));
e.Graphics.Draw String(Lline, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat()) ;
}
}

// e.HasMorePages = true;
}

private void button2_Click(o bject sender, System.EventArg s e)
{

listBox1.Items. Clear();
// Try and print the document
myDoc.DocumentN ame = @"\\MyPc\Temp\M yTestDoc.txt";

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

myDoc.PrinterSe ttings.PrinterN ame = LPrinterName;
listBox1.Items. Add("Printer Name - " +
myDoc.PrinterSe ttings.PrinterN ame);

foreach (System.Drawing .Printing.Paper Size paperSize in
myDoc.PrinterSe ttings.PaperSiz es)
{
if (paperSize.Pape rName == "A3")
{

myDoc.DefaultPa geSettings.Pape rSize
= paperSize;

myDoc.DefaultPa geSettings.Prin terSettings.Def aultPageSetting s.Landscape
= true;
}
}

myDoc.PrinterSe ttings.FromPage = 1;
myDoc.PrinterSe ttings.MinimumP age = 1;
myDoc.PrintPage += new PrintPageEventH andler(myDoc_Pr intPage);
myDoc.Print();
}
Jun 27 '08 #4
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
Jun 27 '08 #5
Hi Mike

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

http://msdn.microsoft.com/en-us/libr...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
Jun 27 '08 #6
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/libr...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
Jun 27 '08 #7
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
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
10078
by: Patrick De Ridder | last post by:
How can I turn what I want to print 90 degrees using the logic below? Please tell me the code with which to make the modification. Many thanks, Patrick. using System.ComponentModel; using System.Drawing; using System.Drawing.Printing; using System.IO;
3
2185
by: Chrisitiaan | last post by:
Hi, I want to develop a 'truly' object oriented application, that is, my business logic objects handle the business logic (of course) and the persistence to a MS SQL server database. The user interface only deals with objects (sometimes using reflection to make them 'editable'). I want to know if anybody has a good suggestion how to print...
4
3111
by: zz | last post by:
Sorry for posting this here but unfortunatly I couldn't find any appropriate newsgroup for this and since I've been active here with .net question I figured you guys might be able to help. This is a link to a very simple page that contains 2 DIV statements: http://xsinx.no-ip.com:8880/TestEmbedDiv.html One of them is embeded in the...
7
1750
by: Jon B | last post by:
Hi There! I'm trying to print ASP.NET pages to the printer. I know I can grab the output contents of the ASP.NET page by using HttpWebRequest and HttpWebResponse objects. However, once I got the contents, it is just HTML. How do I go about printing that HTML as a graphical document to the printer??? Many thanks!!!!
2
2530
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the locations. But After printing first page it increase paper 3cm vertically/Height so on second
6
3231
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument (much like the current model), but my PrintDocument would have a Pages collection such that each time you need to have an additional page, you would...
0
1233
by: Iain Wilson | last post by:
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...
0
190
by: Iain Wilson | last post by:
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...
43
2322
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
7429
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7777
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5916
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5301
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1844
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.