print document or text

[Migrated content. Thread originally posted on 11 April 2012]

Hi ((:

I wonder, how can I do to print text using .net
something like this: www.c-sharpcorner.com/.../printfile.aspx

But of course, in Cobol.net ((:

Thanks ((:
  • Verified Answer

    I have attached file PrintTextVC.zip to this post.

    It is a Visual COBOL version of the demo program that you referenced.

    I am also including the source code here for quick reference:


          $set ilusing"System.Windows.Forms"
          $set ilusing"System.IO"
          $set ilusing"System.Drawing.Printing"
           class-id PrintTextVC.Form1 is partial
                     inherits type System.Windows.Forms.Form.
           
           working-storage section.
           01 verdana10Font type Font.
           01 reader        type StreamReader.

           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               goback.
           end method.

           method-id btnBrowse_Click final private.
           01 fdlg    type OpenFileDialog.
           procedure division using by value sender as object e as type System.EventArgs.
           
              set fdlg to new type OpenFileDialog
              set fdlg::Title to "Visual COBOL Open File Dialog"
              set fdlg::InitialDirectory to "C:\ "
              set fdlg::Filter to "Text files|*.txt|All files (*.*)|*.*"
              set fdlg::FilterIndex to 2
              set fdlg::RestoreDirectory to true

              if (fdlg::ShowDialog = type DialogResult::OK)
                 set textBox1::Text to fdlg::FileName
              end-if
             
           end method.

           method-id btnPrint_Click final private.
           01 filename   string.
           01 pd         type PrintDocument.
           procedure division using by value sender as object e as type System.EventArgs.
               set filename to textBox1::Text::ToString
               
               *> Create a StreamReader object
               set reader to new StreamReader(filename)
               *> Create a Verdana font with size 10
               set verdana10Font to new Font ("Verdana", 10)
               *> Create a PrintDocument object
               set pd to new PrintDocument
               *> Add PrintPage event handler
               invoke pd::add_PrintPage(new System.Drawing.Printing.PrintPageEventHandler(self::PrintTextHandler))
               *> Call Print Method
               invoke pd::Print
               *> Close the reader
               if (reader not = null)
                  invoke reader::Close
               end-if.
           
           end method.
           
           method-id PrintTextHandler final private.
           01 g            type Graphics.
           01 linesPerPage comp-1.
           01 yPos         comp-1.
           01 count1       binary-long.
           01 leftMargin   comp-1.
           01 topMargin    comp-1.
           01 line1        string.
           procedure division using by value sender as object, ppeArgs as type PrintPageEventArgs.
               *> Get the Graphics object
               set g to ppeArgs::Graphics
               set linesPerPage to 0
               set yPos to 0
               set count1 to 0
               *> Read margins from PrintPageEventArgs
               set leftMargin to ppeArgs::MarginBounds::Left
               set topMargin to ppeArgs::MarginBounds::Top
               set line1 to null
               *> Calculate the lines per page on the basis of the height of the page and the height of the font
               set linesPerPage to ppeArgs::MarginBounds::Height / verdana10Font::GetHeight (g)

               *> Now read lines one by one, using StreamReader

               perform until count1 > linesPerPage
                  set line1 to reader::ReadLine
                  if line1 = null
                     exit perform
                  end-if
                  *> Calculate the starting position
                  set yPos to topMargin (count1 * verdana10Font::GetHeight (g))
                  *> Draw text
                  invoke g::DrawString (line1, verdana10Font, type Brushes::Black, leftMargin, yPos, new type StringFormat)
                  *> Move to next line
                  add 1 to count1
               end-perform

               *> If PrintPageEventArgs has more pages to print
               if (line1 not = null)
                  set ppeArgs::HasMorePages to true
               else
                  set ppeArgs::HasMorePages to false
               end-if
           
           end method.
           
           end class.
    PrintTextVC.zip