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();
}
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();
}
Comment