ComponentOne VSView 8.0
Step 3: Previewing Files


The RenderFile routine is where most of the work is done. Here is what it looks like:

Example Title
Copy Code
Sub RenderFile()

 

  ' no file? quit

  If Len(FileName) = 0 Then Exit Sub

 

  ' set up document

  vp.DocName = "VSPrint8 QuickPrinter"

  vp.Header = "QuickPrinter||Page %d"

  vp.Footer = Format(Now, "dd-mmm-yy") & "||" & FileName

  vp.PageBorder = pbAll

 

  ' read the file into a big string

  Dim sText$

  On Error Resume Next

  Open FileName For Binary As #1

  sText = Input$(LOF(1), 1)

  Close #1

  On Error GoTo 0

  ' start document

  vp.StartDoc

  If LCase(Left(sText, 6)) = "<html>" Then

    VPRenderHTML vp, sText ' render html

  Else

    vp = sText ' render regular text and RTF

  End If

  vp.EndDoc

 

  ' update the form caption

  ShowCaption

 

End Sub

Here is what the RenderFile routine does, piece by piece:

Example Title
Copy Code
' no file? quit

If FileName = "" Then Exit Sub

This makes sure we have a document to render. It is good defensive programming.

Example Title
Copy Code
' set up document

vp.DocName = "VSView7 QuickPrinter"

This line sets the VSPrinter control's document name. This name will appear in the Windows print manager window and in the abort window that pops up while the document is printing.

Example Title
Copy Code
vp.Header = "QuickPrinter||Page %d"

vp.Footer = Format(Now, "dd-mmm-yy") & "||" & FileName

Here we build the header and footer for the document. The header has a left aligned part that says "QuickPrinter", a center aligned part that is empty, and a right-aligned part that has a page counter (the %d works as a placeholder for the current page being created). The footer contains the current date and the name of the file being printed.

Example Title
Copy Code
vp.PageBorder = pbAll

This line sets the page borders for the entire document. The color, style, and width for the borders are determined by the PenWidth, PenColor, and PenStyle properties, but we'll just use the defaults here.

Example Title
Copy Code
' read the file into a big string

Dim sText$

On Error Resume Next

Open FileName For Input As #1

sText = Input$(LOF(1), 1)

Close #1

On Error GoTo 0

This block of code reads the entire file into a string that will be printed later. Alternatively, we could render plain text one line at a time, but that would not work for RTF or HTML text. To keep the code simple, we treat all files types the same way.

Example Title
Copy Code
' start document

vp.StartDoc

This statement tells the VSPrinter control we are about to start creating a document. Because the Preview property is Trueby default, this will be a preview document and will not be sent to the printer immediately.

Example Title
Copy Code
If LCase(Left(sText, 6)) <> "<html>" Then

  vp.Paragraph = sText ' render regular text and RTF

Else

  VPRenderHTML vp, sText ' render html

End If

 This block of code is responsible for rendering the file into the VSPrinter control. VSPrinter has built-in support for plain text and RTF. Because the AutoRTF property is set to True, simply assigning the text to the Paragraph property will render plain text and RTF.

Handling HTML requires an extra step. VSPrinter does not have built-in support for rendering HTML text, but the VPUtil.BAS module provides a VPRenderHTML function that renders HTML (there is also an equivalent routine in C++). To make the VPRenderHTML function available to the code, add the VPUtil.BAS module to the project. (To add a module to the project, use the Project | Add File menu option and choose the VPUtil.BAS file).

Example Title
Copy Code
vp.EndDoc

This statement tells the VSPrinter control we are done creating the document. At this point, the document is available for previewing, saving to disk, or sending to the printer.

The NavBar property is set to vbnpBottom by default, so the control will display a navigation bar so the user can flip preview pages and zoom with the mouse. The Navigation property is set to vpnvMouseWheel by default, so the user can scroll the preview using the mouse and the mouse wheel.

Example Title
Copy Code
' update the form caption

ShowCaption

This last statement updates the form caption so it shows the name of the file being previewed. The ShowCaption routine is short and simple:

Example Title
Copy Code
Sub ShowCaption()

  Dim s$

  ' refresh form caption

  s = "ComponentOne VSPrinter8"

  If Len(FileName) > 0 Then s = s & " - " & FileName

  Caption = s

End Sub

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback