Printing Selected Page(s) = Blank Pages

  • Thread starter Thread starter Jody Gelowitz
  • Start date Start date
J

Jody Gelowitz

I am having a problem with printing selected pages. Actually, the problem
isn't with printing selected pages as it is more to do with having blank
pages print for those pages that have not been selected.

For example, if I were to have 5 pages with every second page printing, I
would get the following results:
Page 1 = Print OK
Page 2 = Blank
Page 3 = Print OK
Page 4 = Blank
Page 5 = Print OK

I am doing the validation as to whether or not anything gets printed within
the PrintPage event before anything else is done. If it has been determined
that a page is to be skipped, then I skip over the printing code (sending
information to the e.Graphics object) and go directly to the clean-up code
which Disposes of objects used within the print routine and determines if
there are more pages to be printed as well as determine the page orientation
(Portrait/Landscape) of the next page.

Does anybody have any ideas as to why this might be happening as well as any
clues on how to fix it?

Thanks,
Jody
 
Jody Gelowitz said:
I am having a problem with printing selected pages.

Not seeing any code, I would suggest you put a breakpoint on
all your Printer.NewPage lines and see if they are being called
when they should not be.

Please do not mix classic VB groups with VB.Net groups.
Groups like ..public.dotnet.* are for the .Net languages, and
groups like ..public.vb.* are for the prior versions of VB.

As you may know, there is no Printer object in .Net, nor
does the PrintDocument support a NewPage method. In
other words, the languages may be similar, but they are not
compatable and for that reason it is best to keep them separated
when asking your questions. Post .Net questions to the dotnet.*
groups and classic VB questions to the VB.* groups, please....

LFS
 
Hello,

Jody Gelowitz said:
I am having a problem with printing selected pages. Actually, the problem
isn't with printing selected pages as it is more to do with having blank
pages print for those pages that have not been selected.

For example, if I were to have 5 pages with every second page printing, I
would get the following results:
Page 1 = Print OK
Page 2 = Blank
Page 3 = Print OK
Page 4 = Blank
Page 5 = Print OK

Please post some code.

Do _not_ crosspost VB .NET related questions to the VB Classic groups
(microsoft.public.vb.general.discussion). Thanks!

HTH,
Herfried K. Wagner
 
Fergus Cooney said:
Hi Jody,

Could you post some code ?

Regards,
Fergus

The code is at the end of this message.

I have a collection of forms that have been selected to be printed. Within
each form, there can be either 1 or 2 pages. The user also has the option
to select which pages of each form are to be printed through a treeview
control. The "PrintToGDIP" procedure is called which in turn sets up the
print job. The procedure that does the printing is under
"PrintPage_Event_PDF" which is setup using the "AddHandler" call. The line
of code within "PrintPage_Event_PDF" that determines if a page should be
printed is:
If strPages.IndexOf(m_intPrintPage + 1) > -1 Then
"strPages" contains a comma-separated list of the pages to be printed (ie.
"1,2" or "1" or "2") based on user selection.

After thinking about this over the weekend, it would be easier for me to
create an array of pages from each form that are to be printed before
"PrintPageEvent_PDF" is called. This way, all pages are determined before
printing and all I will have to do afterwards is cycle through the array
instead of doing validation on each form within the print code. Regardless,
it would still be nice to know if it is my code causing the blank pages to
be printed, or if it is some type of bug within the .NET Framework
(v1.0.3705).

Thanks,
Jody

Private Sub PrintToGDIP(ByVal p_colForms As Collection)

Try

Me.Cursor = Cursors.WaitCursor

If gdiprintDoc Is Nothing Then

gdiprintDoc = New Printing.PrintDocument()

gdiprintDoc.PrintController = New Printing.StandardPrintController()

If Not m_bolPDF Then

AddHandler gdiprintDoc.PrintPage, AddressOf PrintPage_Event_XML

Else

AddHandler gdiprintDoc.PrintPage, AddressOf PrintPage_Event_PDF

End If

End If

'set up printer defaults

With gdiprintDoc

..DocumentName = "MyDocument"

If Not datatier.SharedData.objPrintSetting Is Nothing Then

If Not datatier.SharedData.objPrintSetting.strPrinter Is Nothing AndAlso Not
datatier.SharedData.objPrintSetting.strPrinter = "" Then

..DefaultPageSettings.PrinterSettings.PrinterName =
datatier.SharedData.objPrintSetting.strPrinter

End If

Try

If Not .DefaultPageSettings.PaperSize.PaperName =
datatier.SharedData.objPrintSetting.strPaperName Then

Dim psTmp As New
Printing.PaperSize(datatier.SharedData.objPrintSetting.strPaperName, _

datatier.SharedData.objPrintSetting.intPaperWidth,
datatier.SharedData.objPrintSetting.intPaperHeight)

..DefaultPageSettings.PaperSize = psTmp

End If

Catch ex As Exception

End Try

If Not PresentationTier.XMLForm.bolDataOnly Then

If Not datatier.SharedData.objPrintSetting.objMargin Is Nothing Then

..DefaultPageSettings.Margins = datatier.SharedData.objPrintSetting.objMargin

End If

Else

..DefaultPageSettings.Margins.Left = 0

..DefaultPageSettings.Margins.Top = 0

..DefaultPageSettings.Margins.Right = 0

..DefaultPageSettings.Margins.Bottom = 0

End If

End If

'check if first page is landscape

If Not m_bolPDF Then

Dim objXMLForm As PresentationTier.XMLForm

objXMLForm = CType(p_colForms.Item(1), PresentationTier.XMLForm)

..DefaultPageSettings.Landscape = objXMLForm.bolLandscape

Else

Dim objPDFFOrm As PresentationTier.PDFForm

objPDFFOrm = CType(p_colForms.Item(1), PresentationTier.PDFForm)

..DefaultPageSettings.Landscape = objPDFFOrm.objPages(0).bolLandScape

End If

End With



Dim dlgPrint As New frmPrintDialog()

dlgPrint.Document = gdiprintDoc

If Not dlgPrint.ShowDialog = DialogResult.Cancel Then

gdiprintDoc.Print()

End If

Try

datatier.SharedData.objPrintSetting.strPrinter =
gdiprintDoc.PrinterSettings.PrinterName

Catch ex As Exception

End Try

Catch ex As Exception

MsgBox(ex.ToString)

Finally

If Not gdiprintDoc Is Nothing Then

RemoveHandler gdiprintDoc.PrintPage, AddressOf PrintPage_Event_PDF

RemoveHandler gdiprintDoc.PrintPage, AddressOf PrintPage_Event_XML

gdiprintDoc.Dispose()

gdiprintDoc = Nothing

Me.m_intForm = 0

Me.m_intPrintPage = 0

End If

Me.Cursor = Cursors.Default

End Try

End Sub



Private Sub PrintPage_Event_PDF(ByVal sender As Object, ByVal e As
Printing.PrintPageEventArgs)

Dim graCanvas As Graphics

Dim rectClip As RectangleF

Dim priDoc As Printing.PrintDocument

Dim intPrintCopy As Integer

Dim intDocW, intDocH As Integer

Dim sngScaleW, sngScaleH As Single

Dim sngScale As Single = 1

Dim sngDX, sngDY As Single

Dim objForm As PresentationTier.PDFForm

Dim mtxXForm As Drawing2D.Matrix

Dim imgBackground As Image

Dim ioMS As IO.MemoryStream

Dim abyteImage() As Byte

Dim sngEMFScale As Single

Try

objForm = CType(m_colForms.Item(m_intForm + 1), PresentationTier.PDFForm)

'Determine if this page has been selected to print

Dim nodeTmp As TreeNode

Dim objPrintForm As PrintForm

Dim strPages As String

For Each nodeTmp In tvwPrint.Nodes

Try

objPrintForm = Nothing

objPrintForm = CType(m_colPrintForms(nodeTmp.Tag), PrintForm)

If objPrintForm.strFormDisplayName = objForm.strFormDisplayName Then

strPages = objPrintForm.strPagesToPrint

Exit For

Else

If Not objForm.strPrintCopiesDisplayName Is Nothing Then

Dim intPrintCopyCount As Integer

For intPrintCopyCount = 0 To objForm.strPrintCopiesDisplayName.Length - 1

If objForm.strPrintCopiesDisplayName(intPrintCopyCount) =
objPrintForm.strFormDisplayName Then

strPages = objPrintForm.strPagesToPrint

Exit For

End If

Next

End If

End If

Catch ex As Exception

End Try

Next

If strPages.IndexOf(m_intPrintPage + 1) > -1 Then

priDoc = CType(sender, Printing.PrintDocument)

graCanvas = e.Graphics

graCanvas.PageUnit = GraphicsUnit.Point

graCanvas.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality

'Retrieve the image from the page object

If Not DataTier.SharedData.objPrintSetting.bolDataOnly Then

If objForm.objPages(m_intPrintPage).abyteImageData Is Nothing Then

'EMF:Load emf from file

Dim strEMFPath As String = Application.StartupPath & "\crashreports\" &
DataTier.SharedData.objCurrentPackage.GetConfig.strVersion & "\"

Dim strEMFFile As String = objForm.strFormName & ".page" &
m_intPrintPage.ToString

Dim strFile As String = strEMFPath & strEMFFile & "\" & strEMFFile & ".emf"

If IO.File.Exists(strFile) Then

'load into background image

imgBackground = Image.FromFile(strFile)

sngEMFScale = 1 'graCanvas.DpiX / imgBackground.HorizontalResolution

Else

'set background image= false

imgBackground = Nothing

Exit Sub

End If

'*EMF*

Else

abyteImage = objForm.objPages(m_intPrintPage).abyteImageData

ioMS = New IO.MemoryStream(abyteImage)

Dim sngImageW As Single, sngImageH As Single

imgBackground = Image.FromStream(ioMS)

ioMS.Close()

End If

End If

'NOTE***doc width/height are in 100ths of an inch 850=8.5 inches

If m_bolScale AndAlso Not DataTier.SharedData.objPrintSetting.bolDataOnly
Then

If Not e.PageSettings.Landscape Then

'set scale

intDocW = priDoc.DefaultPageSettings.Bounds.Width -
priDoc.DefaultPageSettings.Margins.Left -
priDoc.DefaultPageSettings.Margins.Right

intDocH = priDoc.DefaultPageSettings.Bounds.Height -
priDoc.DefaultPageSettings.Margins.Top -
priDoc.DefaultPageSettings.Margins.Bottom

Else

'set scale

intDocH = priDoc.DefaultPageSettings.Bounds.Width -
priDoc.DefaultPageSettings.Margins.Left -
priDoc.DefaultPageSettings.Margins.Right

intDocW = priDoc.DefaultPageSettings.Bounds.Height -
priDoc.DefaultPageSettings.Margins.Top -
priDoc.DefaultPageSettings.Margins.Bottom

End If



Dim sngImgW As Single, sngImgH As Single

'Paper Width/Height will be in 100th inch

sngImgW = objForm.objPages(m_intPrintPage).sngPaperWidth

sngImgH = objForm.objPages(m_intPrintPage).sngPaperHeight

If sngImgW = 0 Then

If Not objForm.objPages(m_intPrintPage).bolLandScape Then

sngImgW = 850 'default to 8.5 inch

Else

sngImgW = 1100

End If

End If

If sngImgH = 0 Then

If Not objForm.objPages(m_intPrintPage).bolLandScape Then

sngImgH = 1100 'default to 11 inch

Else

sngImgH = 850

End If

End If

sngScaleW = intDocW / sngImgW 'objForm.objPages(m_intPrintPage).sngPageWidth

sngScaleH = intDocH / sngImgH
'objForm.objPages(m_intPrintPage).sngPageHeight

If Single.IsInfinity(sngScaleW) Or Single.IsInfinity(sngScaleH) Then

sngScale = 1

Else

sngScale = Math.Min(sngScaleW, sngScaleH)

End If

Else

With priDoc.DefaultPageSettings.Margins

..Left = 0

..Right = 0

..Top = 0

..Bottom = 0

End With

sngScale = 1

If tsPrintPDF.TraceInfo Then

Trace.WriteLine("m_bolScale: " & m_bolScale.ToString)

Trace.WriteLine("Scale: " & sngScale)

End If

End If

'set translation for margins

'margins are in 1/100th an inch,for some reason this doesn't need to be
converted to page units

Try

m_sngPrintOffsetX = DataTier.SharedData.objPrintSetting.sngOffsetX

m_sngPrintOffsetY = DataTier.SharedData.objPrintSetting.sngOffsetY

Catch ex As Exception

End Try

sngDX = priDoc.DefaultPageSettings.Margins.Left + m_sngPrintOffsetX

sngDY = priDoc.DefaultPageSettings.Margins.Top + m_sngPrintOffsetY



mtxXForm = New Drawing2D.Matrix()

mtxXForm.Scale(sngScale, sngScale)

mtxXForm.Translate(sngDX, sngDY, Drawing.Drawing2D.MatrixOrder.Append)

graCanvas.Transform = mtxXForm

rectClip = graCanvas.ClipBounds

Dim rectfSource As New RectangleF(0, 0, imgBackground.Width,
imgBackground.Height)

Dim rectfDest As New RectangleF(0, 0, imgBackground.Width * sngEMFScale,
imgBackground.Height * sngEMFScale)

e.Graphics.DrawImage(imgBackground, rectfDest, rectfSource,
GraphicsUnit.Pixel)



Dim objField As PresentationTier.PDFField

If Not objForm.objPages(m_intPrintPage).objFields Is Nothing Then

For Each objField In objForm.objPages(m_intPrintPage).objFields

Try

objField.bolPrinting = True

If (DataTier.SharedData.objPrintSetting.bolDataOnly) Then

If objField.bolPrintDataOnly Then

objField.Render(graCanvas)

End If

Else

objField.Render(graCanvas)

End If

objField.bolPrinting = False



Catch exField As Exception

End Try

Next

End If

End If

'setup page,copy,form for next print call

'increment current page

m_intPrintPage += 1

'check if anymore pages on current form or more forms to print

If m_intPrintPage < objForm.objPages.Length Then

e.HasMorePages = True

Else

m_intPrintPage = 0

'increment current form

m_intForm += 1

'check if more forms to print

If m_intForm < m_colForms.Count Then

e.HasMorePages = True

End If

End If

'check if more forms

If e.HasMorePages Then

'check next form's orienation

objForm = CType(m_colForms.Item(m_intForm + 1), PresentationTier.PDFForm)

If objForm.objPages(m_intPrintPage).bolLandScape Then

e.PageSettings.Landscape = True

Else

e.PageSettings.Landscape = False

End If

End If

Catch exPrint As Exception

Debug.WriteLine(exPrint.ToString)

Finally

'reset status

If Not mtxXForm Is Nothing Then

mtxXForm.Dispose()

End If

If Not imgBackground Is Nothing Then

imgBackground.Dispose()

imgBackground = Nothing

End If

If e.HasMorePages = False Then

'reset print counters

m_intForm = 0

m_intPrintPage = 0

End If

End Try

End Sub
 
Back
Top