Skip to main content

How to print multiple pages from an MDI application in a .NET WinForms app.

  • February 15, 2013
  • 0 replies
  • 0 views

Problem:

The .NET Framework comes with a Print Preview control  to be used to give the user a preview of a document.

How to use this control to present multiple pages of print?

Resolution:

When using the PrintPage event you get passed an instance of System.Drawing.Printing.PrintPageEventArgs.

This has a boolean property "HasMorePages" that you can use after printing a page to tell WinForms that another page needs to be printed. Some example code that uses this is:

       method-id.  "printDocument1_PrintPage" final private.

       01  ls-left-margin  pic s9(9) comp-5.

       01  ls-top-margin  pic s9(9) comp-5.

       01  ls-count        pic s9(9) comp-5.

       01  ls-y            pic s9(9) comp-5 value 0.

       linkage section.

       01 sender object.

       01 e S-D-Printing-PrintPageEventArgs.

       procedure division using by value  sender e.

       

           move e::"MarginBounds"::"Top"  to ls-top-margin

           move e::"MarginBounds"::"Left" to ls-top-margin

       

           if ws-first-page

               perform set-print-font

               move 1 to ws-page-number

               set ws-first-page to false      

           else

               add 1 to ws-page-number         

           end-if    

       

           perform varying ls-count from 1 by 1 until ls-count > 30

           

               compute ls-y = ls-top-margin (ls-count * ws-font::"GetHeight"(e::"Graphics"))

               

               invoke e::"Graphics"::"DrawString"("This is a string",ws-font,cBrushes::"Black",ls-left-margin,ls-y)

           

           end-perform       

           

         *> Limit pages to 3  

           if ws-page-number >= 3

               set e::"HasMorePages" to false

               set ws-first-page to true

               move 0 to ws-page-number

           else

               set e::"HasMorePages" to true

           end-if

           

           exit method

           .

       

       set-print-font section.

       

           set ws-font to cFont::"New"("Verdana",24,cFontStyle::"Regular", cGraphicsUnit::"Pixel")

           .

       

       end method "printDocument1_PrintPage".

Attached is a complete Visual Studio Solution that shows how to use the PrintPreview control from COBOL.

Attachments:

HasMorePages.zip

Old KB# 4404