Another Printing Question

ThomasM

Member
Joined
Apr 5, 2006
Messages
17
Programming Experience
Beginner
I have created a WebForm that contains a Table with empty Textboxes and Labels. I would like to print ONLY the tabels and there lables/textboxes. I have other buttons at the top of the page as well as a header but would prefer to NOT have them included in print job. I have searched everywhere but can only find printing information on WindowsForms.

I did find some code that does allow for webcontrol printing...One problem is that it will not keep the same layout as my form and I cant figure out why it wont print the Table.

On my Form I included a button "Print" and make a call such as
WebPrinting.PrintWebControl(myemptyTextbox)

Again.. it will print the text in the texbox but not the textbox.
Is there another way to print my form without including my buttons/Header as well as keeping the page format in place?

Any help would be greatly appricated....
Oh.. By the way... Did I mention that I am new to VS :-)


Below is the WebPrinting class I found
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls

Namespace Jawad.Utils.Printing
_
'/ <summary>
'/ Summary description for WebPrinting.
'/ </summary>
PublicClass WebPrinting
PublicSubNew()
EndSub'New
'
' TODO: Add constructor logic here
'
'/ <summary>
'/ Prints any Control i.e. User Control, DataGrid, Page , Panel etc.
'/ </summary>
'/ <param name="ctrl">Control to be printed</param>
'/ <param name="StyleText">Style to be added in the Head Section</param>
PublicOverloadsSharedSub PrintWebControl(ByVal ctrl As Control)
PrintWebControl(ctrl,
String.Empty)
EndSub'PrintWebControl
'/ <summary>
'/ Prints any Control i.e. User Control, DataGrid, Page , Panel etc.
'/ </summary>
'/ <param name="ctrl">Control to be printed</param>
PublicOverloadsSharedSub PrintWebControl(ByVal ctrl As Control, ByVal Script AsString)
Dim stringWrite AsNew StringWriter()
Dim htmlWrite AsNew HtmlTextWriter(stringWrite)
IfTypeOf ctrl Is WebControl Then
Dim w AsNew Unit(50, UnitType.Pixel)
CType(ctrl, WebControl).Width = w
EndIf
Dim pg AsNew Page()
If Script <> String.Empty Then
pg.RegisterStartupScript("PrintJavaScript", Script)
EndIf
Dim frm AsNew HtmlForm()
pg.Controls.Add(frm)
frm.Attributes.Add(
"runat", "server")
frm.Controls.Add(ctrl)
Dim scr AsString = "<script>function window.onafterprint(){history.back(1);}</script>"
htmlWrite.Write(scr)

pg.DesignerInitialize()
pg.RenderControl(htmlWrite)
Dim strHTML AsString = stringWrite.ToString()
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Write(strHTML)
HttpContext.Current.Response.Write(
"<script>window.print();</script>")
HttpContext.Current.Response.End()
EndSub'PrintWebControl
EndClass'WebPrinting
EndNamespace'Jawad.Utils.Printing
 
Back
Top