Printing Selected Page(s) = Blank Pages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jody Gelowitz

    Printing Selected Page(s) = Blank Pages

    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


  • Fergus Cooney

    #2
    Re: Printing Selected Page(s) = Blank Pages

    Hi Jody,

    Could you post some code ?

    Regards,
    Fergus


    Comment

    • Larry Serflaten

      #3
      Re: Printing Selected Page(s) = Blank Pages

      "Jody Gelowitz" <jody@mail.visu alstatement.com > wrote[color=blue]
      > I am having a problem with printing selected pages.[/color]

      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


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Printing Selected Page(s) = Blank Pages

        Hello,

        "Jody Gelowitz" <jody@mail.visu alstatement.com > schrieb:[color=blue]
        > 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[/color]

        Please post some code.

        Do _not_ crosspost VB .NET related questions to the VB Classic groups
        (microsoft.publ ic.vb.general.d iscussion). Thanks!

        HTH,
        Herfried K. Wagner
        --
        MVP ยท VB Classic, VB .NET
        Die Website von H. Wagner zu .NET, Visual Basic .NET, Classic Visual Basic, Webentwicklung und mehr.



        Comment

        • Jody Gelowitz

          #5
          Re: Printing Selected Page(s) = Blank Pages


          "Fergus Cooney" <filter-1@tesco.net> wrote in message
          news:eUoKUyobDH A.2960@tk2msftn gp13.phx.gbl...[color=blue]
          > Hi Jody,
          >
          > Could you post some code ?
          >
          > Regards,
          > Fergus
          >[/color]

          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


          Comment

          Working...