e.graphics.DrawString text cuts off from R.H.S of the page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmedhussain
    New Member
    • Dec 2008
    • 79

    e.graphics.DrawString text cuts off from R.H.S of the page

    Hi.

    I am having a problem in printing text to printer through printPage event using e.grphics.drawS tring method. On printdocument.p rintPage event When it draws the string, text from the right hand side of the page gets out.. And printer finishes printing...
    Code:
    void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Font myFont = new Font("m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point);
                
                siteHTML = webBrowser1.Document.Body.InnerHtml + webBrowser1.Document.Body.OuterHtml;
    
                float lineHeight = myFont.GetHeight(e.Graphics) + 4;
                float yLineTop = e.MarginBounds.Top;
                            
                    if (yLineTop + lineHeight > e.MarginBounds.Bottom)
                    {
                        e.HasMorePages = true;
                        return;
                    }
                    //e.Graphics.DrawString(siteHTML, myFont, Brushes.Black,
                      //  new PointF(e.MarginBounds.Left, yLineTop));
                    e.Graphics.DrawString(siteHTML, myFont, Brushes.Black, e.PageSettings.PrintableArea.X, e.PageSettings.PrintableArea.Y);
                    yLineTop += lineHeight;
                
                e.HasMorePages = false;
            }
    How will I deal with this problem?

    Regards,
    Syed Ahmed Hussain
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    What you need to take into consider is that DrawString does not automatically wrap your text, nor is there a function to do this automatically, however the Graphics class does offer the method MeasureString, though seeing as I love working with the graphics class admittedly, here is a method that will help you:

    Code:
    private void DrawWrappedString(Graphics g, Font font, string str, int maxWidth, Brush brush, PointF position, int rightMargin = 30, int lineSpacing = 0)
            {
                SizeF s = g.MeasureString(str, font);
                string lineBuffer = "";
                float lineHeight = s.Height;
                float y = position.Y;
                PointF linePosition = new PointF(0, 0);
                foreach (char c in str)
                {
                    s = g.MeasureString(lineBuffer, font);
                    if (s.Width >= (maxWidth - position.X - rightMargin) && c == ' ')
                    { 
                        linePosition = new PointF(position.X, y);
                        g.DrawString(lineBuffer.Trim(), font, brush, linePosition);
                        y += lineHeight + lineSpacing;
                        lineBuffer = "";
                    }
                    lineBuffer += c.ToString();
                }
                linePosition = new PointF(position.X, y);
                g.DrawString(lineBuffer.Trim(), font, brush, linePosition);
            }

    Comment

    • Ahmedhussain
      New Member
      • Dec 2008
      • 79

      #3
      Hi,

      I like the work, but need to know width parameter will take width of what?

      Thank you,

      Comment

      • Aimee Bailey
        Recognized Expert New Member
        • Apr 2010
        • 197

        #4
        luckily the method you are in actually has this information :) just use:

        Code:
        int width = e.PageBounds.Width;
        Aimee.

        Comment

        • Ahmedhussain
          New Member
          • Dec 2008
          • 79

          #5
          hey,
          Thanks for the code, but it still cuts the text from the right hand side. is there any way to stop this?

          Comment

          • Ahmedhussain
            New Member
            • Dec 2008
            • 79

            #6
            I used this, it was working fine except for the e.pagebounds.wi dth. So I divided the width by 2, And now it prints on almost half of my page.

            Regards,
            Ahmed

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              I was under the impression that if you used the overload of DrawString that took a rectangle, it would wrap the text to be contained inside the rectangle.
              It mentions this in the StringFormatFla gs enum where you can tell it NoWrap or DirectionRightT oLeft and other options

              Comment

              • Ahmedhussain
                New Member
                • Dec 2008
                • 79

                #8
                Hi,
                Platter, I would appreciate if you give me some code... Cause I am not getting a proper understanding of e.graphics, to print on a piece of a paper. :'(

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Take a look at the DrawString function:
                  g.DrawString(St ringToDraw, FontToUse, BrushToUse, RectangleFToUse , StringFormatToU se);

                  If you supply a bounding RectangleF, instead of just a starting point, the text will be wrapped.


                  Here's an example showing 2 different types. The first example word wraps, but could leave a partial line (the tops of the glyphs used in xenophobia)
                  while the 2nd type forces only whole lines to be drawn, it character wraps. I didn't play around enough to see if you could make both happen.

                  Code:
                  private static void DemoBitmap()
                  {
                    Bitmap bm = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
                    Graphics g= Graphics.FromImage(bm);
                  
                    string StringToDraw = "Don had a lot of rancid cats. The cats liked his xenophobia very much.";
                    Font FontToUse = new Font(FontFamily.GenericSansSerif, 10);
                    Brush BrushToUse = new SolidBrush(Color.Black);
                    int margin = 10;//10pixels
                    RectangleF RectangleFToUse = new RectangleF(margin, margin, bm.Width - (2 * margin), bm.Height - (2 * margin));
                    StringFormat StringFormatToUse = new StringFormat();//can add StringFormatFlags here 
                  
                    g.Clear(Color.White);
                    g.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width-1, bm.Height-1);
                    g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
                    bm.Save(@"c:\boundedbmp1.bmp", ImageFormat.Bmp );//will have a partial line shown
                  
                    g.Clear(Color.White);
                    g.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width-1, bm.Height-1);
                    StringFormatToUse.FormatFlags = StringFormatToUse.FormatFlags | StringFormatFlags.LineLimit;
                    g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
                    bm.Save(@"c:\boundedbmp2.bmp", ImageFormat.Bmp);//will not have a partial line shown, wraps on character
                  }

                  Comment

                  • Ahmedhussain
                    New Member
                    • Dec 2008
                    • 79

                    #10
                    Platter, It create bitmaps great.. But still tell me how to print.


                    Code:
                    private static void DemoBitmap(String str, Graphics g1)
                            {
                                  Bitmap bm = new Bitmap(750, 1000, PixelFormat.Format32bppArgb);
                                  Graphics g = Graphics.FromImage(bm);
                                  g1 = Graphics.FromImage(bm);
                                  string StringToDraw = str;//"Donhad a lot of rancid cats. The cats liked his xenophobia very much.";
                                  Font FontToUse = new Font(FontFamily.GenericSansSerif, 10);
                                  Brush BrushToUse = new SolidBrush(Color.DarkBlue);
                                  int margin = 10;//10pixels
                                  RectangleF RectangleFToUse = new RectangleF(margin, margin, bm.Width - (2 * margin), bm.Height - (2 * margin));
                                  StringFormat StringFormatToUse = new StringFormat();//can add StringFormatFlags here 
                                  
                                  g1.Clear(Color.White);
                                  g1.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width - 1, bm.Height - 1);
                                  g.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
                                  g1.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
                                  //bm.Save(@"d:\boundedbmp1.bmp", ImageFormat.Bmp);//will have a partialline shown
                    
                                  g1.Clear(Color.White);
                                  g1.DrawRectangle(new Pen(BrushToUse), 0, 0, bm.Width - 1, bm.Height - 1);
                                  StringFormatToUse.FormatFlags = StringFormatToUse.FormatFlags | StringFormatFlags.LineLimit;
                                  g1.DrawString(StringToDraw, FontToUse, BrushToUse, RectangleFToUse, StringFormatToUse);
                                 // bm.Save(@"d:\boundedbmp2.bmp", ImageFormat.Bmp);//will not have a partial line shown, wraps on character
                            }
                    but it doesnt print any thing .. I get only blank paper on my print event :(

                    Comment

                    • Ahmedhussain
                      New Member
                      • Dec 2008
                      • 79

                      #11
                      What I did to print on the page properly is this.

                      Code:
                      void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
                              {
                                  siteHTML = webBrowser1.Document.Body.OuterHtml;
                                  float linesPerPage = 0;
                                  float leftMargin = e.MarginBounds.Left;
                                  float topMargin = e.MarginBounds.Top;
                                  String line = null;
                      
                                  byte[] byteArray = Encoding.ASCII.GetBytes(siteHTML);
                                  MemoryStream stream = new MemoryStream(byteArray);
                                  StreamReader reader = new StreamReader(stream);
                      
                                  e.PageSettings.Color = true;
                                  rptFont = new Font("Arial", 10);
                                  linesPerPage = e.MarginBounds.Height / rptFont.GetHeight(e.Graphics);
                                  
                                  e.PageSettings.PaperSize = new PaperSize("A4", 8, 11);
                                  float lineHeight = rptFont.GetHeight(e.Graphics);
                                  
                                  int count = 0;
                                  while (((line = reader.ReadLine()) != null)&& count<linesPerPage)
                                  {
                                      Rectangle rect = new Rectangle(20, 20 + Convert.ToInt32(lineHeight * count), 800, Convert.ToInt32(lineHeight));
                                      e.Graphics.DrawString(line, rptFont, Brushes.BlueViolet, rect, new StringFormat());
                                      count++;
                                  }
                              }
                      But now I have to look for a next page to print it properly. If I do something like,

                      Code:
                      if (count > linesPerPage)
                                          e.HasMorePages = true;
                                      else
                                          e.HasMorePages = false;
                      The code will go boom. :@
                      How would it be possible to print another page???

                      Comment

                      • Ahmedhussain
                        New Member
                        • Dec 2008
                        • 79

                        #12
                        Well not the code, but the count for pages go unlumited :s

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          The bitmap was just an example. Printing works with the same Graphics object. It's just a big empty bitmap that is printed

                          Comment

                          Working...