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