"Fergus Cooney" <fi******@tesco .net> wrote in message
news:eU******** ******@tk2msftn gp13.phx.gbl...
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 "PrintToGDI P" procedure is called which in turn sets up the
print job. The procedure that does the printing is under
"PrintPage_Even t_PDF" which is setup using the "AddHandler " call. The line
of code within "PrintPage_Even t_PDF" that determines if a page should be
printed is:
If strPages.IndexO f(m_intPrintPag e + 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(ByV al p_colForms As Collection)
Try
Me.Cursor = Cursors.WaitCur sor
If gdiprintDoc Is Nothing Then
gdiprintDoc = New Printing.PrintD ocument()
gdiprintDoc.Pri ntController = New Printing.Standa rdPrintControll er()
If Not m_bolPDF Then
AddHandler gdiprintDoc.Pri ntPage, AddressOf PrintPage_Event _XML
Else
AddHandler gdiprintDoc.Pri ntPage, AddressOf PrintPage_Event _PDF
End If
End If
'set up printer defaults
With gdiprintDoc
..DocumentName = "MyDocument "
If Not datatier.Shared Data.objPrintSe tting Is Nothing Then
If Not datatier.Shared Data.objPrintSe tting.strPrinte r Is Nothing AndAlso Not
datatier.Shared Data.objPrintSe tting.strPrinte r = "" Then
..DefaultPageSe ttings.PrinterS ettings.Printer Name =
datatier.Shared Data.objPrintSe tting.strPrinte r
End If
Try
If Not .DefaultPageSet tings.PaperSize .PaperName =
datatier.Shared Data.objPrintSe tting.strPaperN ame Then
Dim psTmp As New
Printing.PaperS ize(datatier.Sh aredData.objPri ntSetting.strPa perName, _
datatier.Shared Data.objPrintSe tting.intPaperW idth,
datatier.Shared Data.objPrintSe tting.intPaperH eight)
..DefaultPageSe ttings.PaperSiz e = psTmp
End If
Catch ex As Exception
End Try
If Not PresentationTie r.XMLForm.bolDa taOnly Then
If Not datatier.Shared Data.objPrintSe tting.objMargin Is Nothing Then
..DefaultPageSe ttings.Margins = datatier.Shared Data.objPrintSe tting.objMargin
End If
Else
..DefaultPageSe ttings.Margins. Left = 0
..DefaultPageSe ttings.Margins. Top = 0
..DefaultPageSe ttings.Margins. Right = 0
..DefaultPageSe ttings.Margins. Bottom = 0
End If
End If
'check if first page is landscape
If Not m_bolPDF Then
Dim objXMLForm As PresentationTie r.XMLForm
objXMLForm = CType(p_colForm s.Item(1), PresentationTie r.XMLForm)
..DefaultPageSe ttings.Landscap e = objXMLForm.bolL andscape
Else
Dim objPDFFOrm As PresentationTie r.PDFForm
objPDFFOrm = CType(p_colForm s.Item(1), PresentationTie r.PDFForm)
..DefaultPageSe ttings.Landscap e = objPDFFOrm.objP ages(0).bolLand Scape
End If
End With
Dim dlgPrint As New frmPrintDialog( )
dlgPrint.Docume nt = gdiprintDoc
If Not dlgPrint.ShowDi alog = DialogResult.Ca ncel Then
gdiprintDoc.Pri nt()
End If
Try
datatier.Shared Data.objPrintSe tting.strPrinte r =
gdiprintDoc.Pri nterSettings.Pr interName
Catch ex As Exception
End Try
Catch ex As Exception
MsgBox(ex.ToStr ing)
Finally
If Not gdiprintDoc Is Nothing Then
RemoveHandler gdiprintDoc.Pri ntPage, AddressOf PrintPage_Event _PDF
RemoveHandler gdiprintDoc.Pri ntPage, AddressOf PrintPage_Event _XML
gdiprintDoc.Dis pose()
gdiprintDoc = Nothing
Me.m_intForm = 0
Me.m_intPrintPa ge = 0
End If
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub PrintPage_Event _PDF(ByVal sender As Object, ByVal e As
Printing.PrintP ageEventArgs)
Dim graCanvas As Graphics
Dim rectClip As RectangleF
Dim priDoc As Printing.PrintD ocument
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 PresentationTie r.PDFForm
Dim mtxXForm As Drawing2D.Matri x
Dim imgBackground As Image
Dim ioMS As IO.MemoryStream
Dim abyteImage() As Byte
Dim sngEMFScale As Single
Try
objForm = CType(m_colForm s.Item(m_intFor m + 1), PresentationTie r.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_colPrin tForms(nodeTmp. Tag), PrintForm)
If objPrintForm.st rFormDisplayNam e = objForm.strForm DisplayName Then
strPages = objPrintForm.st rPagesToPrint
Exit For
Else
If Not objForm.strPrin tCopiesDisplayN ame Is Nothing Then
Dim intPrintCopyCou nt As Integer
For intPrintCopyCou nt = 0 To objForm.strPrin tCopiesDisplayN ame.Length - 1
If objForm.strPrin tCopiesDisplayN ame(intPrintCop yCount) =
objPrintForm.st rFormDisplayNam e Then
strPages = objPrintForm.st rPagesToPrint
Exit For
End If
Next
End If
End If
Catch ex As Exception
End Try
Next
If strPages.IndexO f(m_intPrintPag e + 1) > -1 Then
priDoc = CType(sender, Printing.PrintD ocument)
graCanvas = e.Graphics
graCanvas.PageU nit = GraphicsUnit.Po int
graCanvas.Smoot hingMode = Drawing.Drawing 2D.SmoothingMod e.HighQuality
'Retrieve the image from the page object
If Not DataTier.Shared Data.objPrintSe tting.bolDataOn ly Then
If objForm.objPage s(m_intPrintPag e).abyteImageDa ta Is Nothing Then
'EMF:Load emf from file
Dim strEMFPath As String = Application.Sta rtupPath & "\crashreports\ " &
DataTier.Shared Data.objCurrent Package.GetConf ig.strVersion & "\"
Dim strEMFFile As String = objForm.strForm Name & ".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.H orizontalResolu tion
Else
'set background image= false
imgBackground = Nothing
Exit Sub
End If
'*EMF*
Else
abyteImage = objForm.objPage s(m_intPrintPag e).abyteImageDa ta
ioMS = New IO.MemoryStream (abyteImage)
Dim sngImageW As Single, sngImageH As Single
imgBackground = Image.FromStrea m(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.Shared Data.objPrintSe tting.bolDataOn ly
Then
If Not e.PageSettings. Landscape Then
'set scale
intDocW = priDoc.DefaultP ageSettings.Bou nds.Width -
priDoc.DefaultP ageSettings.Mar gins.Left -
priDoc.DefaultP ageSettings.Mar gins.Right
intDocH = priDoc.DefaultP ageSettings.Bou nds.Height -
priDoc.DefaultP ageSettings.Mar gins.Top -
priDoc.DefaultP ageSettings.Mar gins.Bottom
Else
'set scale
intDocH = priDoc.DefaultP ageSettings.Bou nds.Width -
priDoc.DefaultP ageSettings.Mar gins.Left -
priDoc.DefaultP ageSettings.Mar gins.Right
intDocW = priDoc.DefaultP ageSettings.Bou nds.Height -
priDoc.DefaultP ageSettings.Mar gins.Top -
priDoc.DefaultP ageSettings.Mar gins.Bottom
End If
Dim sngImgW As Single, sngImgH As Single
'Paper Width/Height will be in 100th inch
sngImgW = objForm.objPage s(m_intPrintPag e).sngPaperWidt h
sngImgH = objForm.objPage s(m_intPrintPag e).sngPaperHeig ht
If sngImgW = 0 Then
If Not objForm.objPage s(m_intPrintPag e).bolLandScape Then
sngImgW = 850 'default to 8.5 inch
Else
sngImgW = 1100
End If
End If
If sngImgH = 0 Then
If Not objForm.objPage s(m_intPrintPag e).bolLandScape Then
sngImgH = 1100 'default to 11 inch
Else
sngImgH = 850
End If
End If
sngScaleW = intDocW / sngImgW 'objForm.objPag es(m_intPrintPa ge).sngPageWidt h
sngScaleH = intDocH / sngImgH
'objForm.objPag es(m_intPrintPa ge).sngPageHeig ht
If Single.IsInfini ty(sngScaleW) Or Single.IsInfini ty(sngScaleH) Then
sngScale = 1
Else
sngScale = Math.Min(sngSca leW, sngScaleH)
End If
Else
With priDoc.DefaultP ageSettings.Mar gins
..Left = 0
..Right = 0
..Top = 0
..Bottom = 0
End With
sngScale = 1
If tsPrintPDF.Trac eInfo Then
Trace.WriteLine ("m_bolScale : " & m_bolScale.ToSt ring)
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_sngPrintOffse tX = DataTier.Shared Data.objPrintSe tting.sngOffset X
m_sngPrintOffse tY = DataTier.Shared Data.objPrintSe tting.sngOffset Y
Catch ex As Exception
End Try
sngDX = priDoc.DefaultP ageSettings.Mar gins.Left + m_sngPrintOffse tX
sngDY = priDoc.DefaultP ageSettings.Mar gins.Top + m_sngPrintOffse tY
mtxXForm = New Drawing2D.Matri x()
mtxXForm.Scale( sngScale, sngScale)
mtxXForm.Transl ate(sngDX, sngDY, Drawing.Drawing 2D.MatrixOrder. Append)
graCanvas.Trans form = mtxXForm
rectClip = graCanvas.ClipB ounds
Dim rectfSource As New RectangleF(0, 0, imgBackground.W idth,
imgBackground.H eight)
Dim rectfDest As New RectangleF(0, 0, imgBackground.W idth * sngEMFScale,
imgBackground.H eight * sngEMFScale)
e.Graphics.Draw Image(imgBackgr ound, rectfDest, rectfSource,
GraphicsUnit.Pi xel)
Dim objField As PresentationTie r.PDFField
If Not objForm.objPage s(m_intPrintPag e).objFields Is Nothing Then
For Each objField In objForm.objPage s(m_intPrintPag e).objFields
Try
objField.bolPri nting = True
If (DataTier.Share dData.objPrintS etting.bolDataO nly) Then
If objField.bolPri ntDataOnly Then
objField.Render (graCanvas)
End If
Else
objField.Render (graCanvas)
End If
objField.bolPri nting = 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.objPage s.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.Coun t Then
e.HasMorePages = True
End If
End If
'check if more forms
If e.HasMorePages Then
'check next form's orienation
objForm = CType(m_colForm s.Item(m_intFor m + 1), PresentationTie r.PDFForm)
If objForm.objPage s(m_intPrintPag e).bolLandScape Then
e.PageSettings. Landscape = True
Else
e.PageSettings. Landscape = False
End If
End If
Catch exPrint As Exception
Debug.WriteLine (exPrint.ToStri ng)
Finally
'reset status
If Not mtxXForm Is Nothing Then
mtxXForm.Dispos e()
End If
If Not imgBackground Is Nothing Then
imgBackground.D ispose()
imgBackground = Nothing
End If
If e.HasMorePages = False Then
'reset print counters
m_intForm = 0
m_intPrintPage = 0
End If
End Try
End Sub