locked
how do I simply print string[] to a printer each string to a line RRS feed

  • Question

  • I have never used printer in my programs and have no clue how to it


    I need such a function:

    public static void PrintString(string[] lines, PrinterSettings settings, Font f)
    {

    }


    can somebody help me please?


    Shimmy
    • Moved by Peter Ritchie Wednesday, June 4, 2008 9:25 PM off-topic
    Wednesday, June 4, 2008 3:58 PM

Answers

  • follow this : http://msdn.microsoft.com/en-us/library/aa287530(VS.71).aspx
    with some changes

    private void printDocument1_PrintPage(string[] lines)
    {
    float yPos = 0f;
    int count = 0;
    float leftMargin = e.MarginBounds.Left;
    float topMargin = e.MarginBounds.Top;
    string line = null;
    float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
    while (count < linesPerPage)
    {
    line = lines[count];
    if (line == null)
    {
    break;
    }
    yPos = topMargin + count * printFont.GetHeight(e.Graphics);
    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
    count++;
    }
    if (line != null)
    {
    e.HasMorePages = true;
    }
    }


    Please click 'Mark as Answer' on the post that helped you.
    Wednesday, June 4, 2008 4:21 PM
  • This is the code suggested by  H. (冬) Tony modified for your function:

        private string[] StringsToPrint;

        private int LinePrinting;

        private System.Drawing.Font printFont;

        public  void PrintString(string[] lines, System.Drawing.Printing.PrinterSettings settings, Font f)

        {

          this.StringsToPrint = lines;

          this.printFont = f;

          this.printDocument1.PrinterSettings = settings;

          this.printDocument1.OriginAtMargins = true;

          this.LinePrinting = 0;

          this.printDocument1.Print();

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

        {

          int LastLineToPrint = LinePrinting + e.MarginBounds.Height / printFont.Height;

          if (LastLineToPrint > StringsToPrint.Length)

            LastLineToPrint = StringsToPrint.Length;

          else

            e.HasMorePages = true;

          for (; LinePrinting < LastLineToPrint; LinePrinting++)

            e.Graphics.DrawString(StringsToPrint[LinePrinting], printFont, Brushes.Black, 0, LinePrinting * printFont.Height);

        }

    • Edited by JohnWein Wednesday, June 4, 2008 7:55 PM code correction
    • Marked as answer by Shimmy Weitzhandler Wednesday, June 4, 2008 8:10 PM
    Wednesday, June 4, 2008 7:39 PM
  •  For questions and discussions regarding client application development using Windows Forms controls and features, please see http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=8&SiteID=1
    http://www.peterRitchie.com/blog
    • Marked as answer by Peter Ritchie Wednesday, June 4, 2008 9:24 PM
    Wednesday, June 4, 2008 9:24 PM

All replies

  • follow this : http://msdn.microsoft.com/en-us/library/aa287530(VS.71).aspx
    with some changes

    private void printDocument1_PrintPage(string[] lines)
    {
    float yPos = 0f;
    int count = 0;
    float leftMargin = e.MarginBounds.Left;
    float topMargin = e.MarginBounds.Top;
    string line = null;
    float linesPerPage = e.MarginBounds.Height/printFont.GetHeight(e.Graphics);
    while (count < linesPerPage)
    {
    line = lines[count];
    if (line == null)
    {
    break;
    }
    yPos = topMargin + count * printFont.GetHeight(e.Graphics);
    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
    count++;
    }
    if (line != null)
    {
    e.HasMorePages = true;
    }
    }


    Please click 'Mark as Answer' on the post that helped you.
    Wednesday, June 4, 2008 4:21 PM
  • somehow during the print I get an error message that represents an internal exception

    an exception is not thrown but I see a message box

    do yuo have a clue?
    Shimmy
    Wednesday, June 4, 2008 4:37 PM
  • This is the code suggested by  H. (冬) Tony modified for your function:

        private string[] StringsToPrint;

        private int LinePrinting;

        private System.Drawing.Font printFont;

        public  void PrintString(string[] lines, System.Drawing.Printing.PrinterSettings settings, Font f)

        {

          this.StringsToPrint = lines;

          this.printFont = f;

          this.printDocument1.PrinterSettings = settings;

          this.printDocument1.OriginAtMargins = true;

          this.LinePrinting = 0;

          this.printDocument1.Print();

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

        {

          int LastLineToPrint = LinePrinting + e.MarginBounds.Height / printFont.Height;

          if (LastLineToPrint > StringsToPrint.Length)

            LastLineToPrint = StringsToPrint.Length;

          else

            e.HasMorePages = true;

          for (; LinePrinting < LastLineToPrint; LinePrinting++)

            e.Graphics.DrawString(StringsToPrint[LinePrinting], printFont, Brushes.Black, 0, LinePrinting * printFont.Height);

        }

    • Edited by JohnWein Wednesday, June 4, 2008 7:55 PM code correction
    • Marked as answer by Shimmy Weitzhandler Wednesday, June 4, 2008 8:10 PM
    Wednesday, June 4, 2008 7:39 PM
  • thank you guys
    Shimmy
    Wednesday, June 4, 2008 8:10 PM
  •  For questions and discussions regarding client application development using Windows Forms controls and features, please see http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=8&SiteID=1
    http://www.peterRitchie.com/blog
    • Marked as answer by Peter Ritchie Wednesday, June 4, 2008 9:24 PM
    Wednesday, June 4, 2008 9:24 PM
  •  forgive me for posting here
    my unintentionally mistake
    Shimmy
    Wednesday, June 4, 2008 9:28 PM