Navigation:  »No topics above this level«

Examples

Previous pageReturn to chapter overviewNext page

 

Below are a few examples that demonstrate how to use the ASP Printer object (COM) to print from your ASP pages, HTML pages and other programming languages such as VB (Visual Basic), VBA, VC++, Delphi, MS Office (Word, Excel, PowerPoint), etc., using just a single line of code!

 

For ready-to-use examples and sample code that demonstrate the use of all ASP Printer object features, please refer to the demo projects supplied with the component. Other sample code or projects may be available on the VBGold web site.

 

Hint: All the examples below can be used exactly 'as is' in other programming languages (e.g. VB, VB Script, Java Script, etc.), by just modifying the line that creates the object. For example:

 

replace Set Prn = Server.CreateObject("ASPPrinterCOM.ASPPrinter")

with Set Prn = CreateObject("ASPPrinterCOM.ASPPrinter") (for VB or VB Script)

or with var Prn = new ActiveXObject('ASPPrinterCOM.ASPPrinter') (for Java Script)

 

 

 

red_bullet Example 1: Printing text from an HTML TextArea field or INPUT field posted from an HTML form to an ASP page


'Get the text from the TextArea HTML field (named 'TextString') of the posted HTML form

TextString = Request.Form("TextString")        

 

'Get font settings from an HTML INPUT text field (named 'FontSettings')

FontSettings = Request.Form("FontSettings")        

 

'Create the ASP Printer object (ASPPrinterCOM.ASPPrinter) on the web server

Set Prn = Server.CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Call the PrintText method to print

RetVal = Prn.PrintText(TextString, FontSettings)

 

'Clean up

Set Prn = Nothing

 

 

 

 

red_bullet Example 2: Printing an order form from an HTML form (with several INPUT tags, text captions, etc.) after submitting it to an ASP page


'In the HTML form, we need to have a hidden field where, upon submitting the form, we store tha entire form code in it. This can be achieved by using the document.all.Form1.outerHTML Java Script or VB Script code

 

'Get the entire form contents (all of its HTML code) that were stored in the hidden field 'OrderFormContents'

FormContents = Request.Form("OrderFormContents")

                       

'Append the basic HTML tages (html, head, body, etc.) to the order form HTML code

FormContents = "<HTML><HEAD></HEAD><BODY>" & FormContents & "</BODY></HTML>"

                       

'Create the ASP Printer object (ASPPrinterCOM.ASPPrinter) on the web server

Set Prn = Server.CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Send the HTML code to the ASP Printer object

RetVal = Prn.PrintHTMLDocFromSource(FormContents)

 

'Clean up

Set Prn = Nothing

 

 

 

 

red_bullet Example 3: Printing a text file or RTF file from ASP page just after uploading the file to the server


'Get the file name from the 'File' field in the HTML form

FileName = Request.Form("File1")

 

'Get font settings from an HTML INPUT text field (named 'FontSettings')

'This is only required for text files. For RTF files, the file will be printed as is with all the layout (colors, margins, images, etc.)

FontSettings = Request.Form("FontSettings")

 

'Create the ASP Printer object (ASPPrinterCOM.ASPPrinter) on the web server

Set Prn = Server.CreateObject("ASPPrinterCOM.ASPPrinter")

 

'This is for text files

'Send the text file to the ASP Printer object

'Optionally, we also specify the print margins (left, top, right, bottom respectively)

RetVal = Prn.PrintTextFile(FileName, FontSettings, 2000, 1440, 2000, 1440)

 

'This is for RTF files

'Send the RTF file to the ASP Printer object

'Optionally, we also specify the paper orientation, the paper size and the paper bin

RetVal = Prn.PrintRTFFile(FileName,,,,, spOrientationLandscape, spPaperA4, spPaperBinLower)

 

'Clean up

Set Prn = Nothing

 

 

 

 

red_bullet Example 4: Printing an HTML file from ASP page just after uploading the file to the server. The file will be printed with all the layout, colors, images, etc. (WYSIWYG)


'Get the file name from the 'File' field in the HTML form

FileName = Request.Form("File1")

 

'Create the ASP Printer object (ASPPrinterCOM.ASPPrinter) on the web server

Set Prn = Server.CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Send the HTML file to the ASP Printer object

RetVal = Prn.PrintHTMLDocument(FileName)

 

'Clean up

Set Prn = Nothing

 

 

 

 

red_bullet Example 5: Printing the output of a live URL (web address), exactly as it is displayed in a web browser, including all page layout, font formatting, images, colors, etc. (WYSIWYG). The URL is specified on the client in an HTML INPUT form field, and is submitted to an ASP page on the server.


'Get the URL address from the HTML form field named 'URL'

URL = Request.Form("URL")

 

'Create the ASP Printer object (ASPPrinterCOM.ASPPrinter) on the web server

Set Prn = Server.CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Send the URL to the ASP Printer object

RetVal = Prn.PrintHTMLDocument(URL)

 

'Clean up

Set Prn = Nothing

 

 

 

 

red_bullet Example 6: Printing an HTML document (page) viewed in a web browser on the client machine. The printing is done on the client (using VB Script) from within the web browser, using the source code of the HTML document


'Create the ASP Printer object

Set Prn = CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Get the HTML page entire source code

HTMLSource = document.documentElement.outerHTML

 

'Send the HTML source code to the ASP Printer object

'The object will build the HTML document form its source code and will print it

RetVal = Prn.PrintHTMLDocFromSource(HTMLSource)

 

'Clean up

Set Prn = Nothing

 

'Notify the user for print completion

If RetVal = "" Then        

       MsgBox "Print Completed Successfully!",,"ASP Printer COM"

Else

       'RetVal returns a string containing the error number and error description separated by the pipe (|) character

       ErrorData = Split(RetVal,"|")

       MsgBox "Print Error!" & vbCrLf & "Error Number=" & ErrorData(0) & vbCrLf & "Error Description=" & ErrorData(1),,"ASP Printer COM"

End If

 

 

 

 

red_bullet Example 7: Printing an HTML file on the client machine from within the user's web browser, using VB Script


'Create the ASP Printer object

Set Prn = CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Get the file name from the HTML field 'File1' (file-type field). This file is located on the client machine

FileName = Form1.File1.value

 

'Call the PrintHTMLDocument method to print

RetVal = Prn.PrintHTMLDocument(FileName)

 

'Clean up

Set Prn = Nothing

 

'Notify the user for print completion

If RetVal = "" Then        

       MsgBox "Print Completed Successfully!",,"ASP Printer COM"

Else

       'RetVal returns a string containing the error number and error description separated by the pipe (|) character

       ErrorData = Split(RetVal,"|")

       MsgBox "Print Error!" & vbCrLf & "Error Number=" & ErrorData(0) & vbCrLf & "Error Description=" & ErrorData(1),,"ASP Printer COM"

End If

 

 

 

 

red_bullet Example 8: Printing a text document with title, body, header and footer from an HTML page viewed on the user's web browser, using VB Script


'Create the ASP Printer object

Set Prn = CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Get document parts from the appropriate HTML fields and assign them to the appropriate object properties

Prn.DocTitle = document.Form1.Title.value

Prn.DocHeader = document.Form1.Header.value

Prn.DocFooter = document.Form1.Footer.value

Prn.DocText = document.Form1.DocBody.value

                       

'Get font name and size for each part of the document from the appropriate HTML fields

Prn.DocTitleFont = document.Form1.TitleFont.value

Prn.DocHeaderFont = document.Form1.HeaderFont.value

Prn.DocFooterFont = document.Form1.FooterFont.value

Prn.DocTextFont = document.Form1.BodyFont.value

                       

'Set paper orientation (from the value of the appropriate HTML field)

Prn.PaperOrientation = document.Form1.PaperOrientation.value 'spOrientationLandscape = 2, spOrientationPortrait = 1

                       

'Set line spacing  (from the value of the appropriate HTML field)

Prn.LineSpacing = document.Form1.BodyLineSpacing.value        'spLineSpacingSingle = 1 (default), spLineSpacingOneHalf = 2, spLineSpacingDouble = 3

                       

'Send the document to the ASP Printer object.

'You can also specify the printer name (as argument), to which the document will be sent

RetVal = Prn.PrintDoc

 

'Clean up

Set Prn = Nothing

       

'Notify the user for print completion

If RetVal = "" Then        

       MsgBox "Print Completed Successfully!",,"ASP Printer COM"

Else

       'RetVal returns a string containing the error number and error description separated by the pipe (|) character

       ErrorData = Split(RetVal,"|")

       MsgBox "Print Error!" & vbCrLf & "Error Number=" & ErrorData(0) & vbCrLf & "Error Description=" & ErrorData(1),,"ASP Printer COM"

End If

 

 

 

 

red_bullet Example 9: Retrieving all the physical and logical printers connected to the system on the client machine from within the user's web browser, using VB Script


'Create the ASP Printer object

Set Prn = CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Query all physical printer devices attached to the system

'This will also retrieve any logical printers (such as FAX printer driver, PDF printer driver, etc.)

'The method returns a variant array (string) containing the names of the printers found

Printers = Prn.GetPrinters(PrintersCount)

 

P = PrintersCount & " printers were detected on your system:" & vbcrlf & vbcrlf

       

For i=0 To UBound(Printers)

       P = P & Printers(i) & vbcrlf

Next

 

'Clean up

Set Prn = Nothing

       

'Show the names of detected printers

MsgBox P,,"ASP Printer COM"

 

 

 

 

red_bullet Example 10: Printing a PDF (Adobe Portable Document Format) file on the client machine from within the user's web browser, using VB Script


'Create the ASP Printer object

Set Prn = CreateObject("ASPPrinterCOM.ASPPrinter")

 

'Get the file name from the HTML field 'File1' (file-type field). This file is located on the client machine

FileName = Form1.File1.value

 

'Call the PrintPDFFile method to print

RetVal = Prn.PrintPDFFile(FileName)

 

'Clean up

Set Prn = Nothing

 

'Notify the user for print completion

If RetVal = "" Then        

       MsgBox "Print Completed Successfully!",,"ASP Printer COM"

Else

       'RetVal returns a string containing the error number and error description separated by the pipe (|) character

       ErrorData = Split(RetVal,"|")

       MsgBox "Print Error!" & vbCrLf & "Error Number=" & ErrorData(0) & vbCrLf & "Error Description=" & ErrorData(1),,"ASP Printer COM"

End If