Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. PrintDocument clipping / wrapping text problem

PrintDocument clipping / wrapping text problem

Scheduled Pinned Locked Moved C#
helpdatabasegraphicsquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark F
    wrote on last edited by
    #1

    I have the following code which prints three RichTextBox controls to a page. The format is Avery 5388 which is the perforated index card stock paper (8.5x11). The print preview shows the text in each box but the lines don't wrap. Any way to fix the wrapping problem?

        private void fcPrintDocument\_PrintPage( object sender, PrintPageEventArgs e )
        {
            cardDataTabPage.Focus();
            DrawPage( e.Graphics );
        }
    
        private void DrawPage( Graphics graphics )
        {
            Graphics g = graphics;
            Pen boxPen = new Pen( Brushes.Black );
    
            int pageWidth = fcPrintDocument.DefaultPageSettings.PaperSize.Width;
            int pageHeight = fcPrintDocument.DefaultPageSettings.PaperSize.Height;
            
            RectangleF srcRect = cardDataTabPage.ClientRectangle;
            RectangleF destRect = new Rectangle( 0, 0, pageWidth, pageHeight );
            
            float scaleX = destRect.Width / 850;
            float scaleY = destRect.Height / 1100;
    
            for ( int i = 0; i < cardDataTabPage.Controls.Count; i++ )
            {
                if ( cardDataTabPage.Controls\[i\].GetType() == richTextBox1.GetType() )
                {
                    RichTextBox rich = (RichTextBox)cardDataTabPage.Controls\[i\];
                    g.DrawRectangle( boxPen, rich.Bounds );  // draws 3x5 box where card should be
                    g.DrawString( rich.Text, rich.Font, Brushes.Black, 
                        rich.Bounds.Left \* scaleX, rich.Bounds.Top \* scaleY, new StringFormat() );
                }
            }
        }
    

    Thanks.

    L 1 Reply Last reply
    0
    • M Mark F

      I have the following code which prints three RichTextBox controls to a page. The format is Avery 5388 which is the perforated index card stock paper (8.5x11). The print preview shows the text in each box but the lines don't wrap. Any way to fix the wrapping problem?

          private void fcPrintDocument\_PrintPage( object sender, PrintPageEventArgs e )
          {
              cardDataTabPage.Focus();
              DrawPage( e.Graphics );
          }
      
          private void DrawPage( Graphics graphics )
          {
              Graphics g = graphics;
              Pen boxPen = new Pen( Brushes.Black );
      
              int pageWidth = fcPrintDocument.DefaultPageSettings.PaperSize.Width;
              int pageHeight = fcPrintDocument.DefaultPageSettings.PaperSize.Height;
              
              RectangleF srcRect = cardDataTabPage.ClientRectangle;
              RectangleF destRect = new Rectangle( 0, 0, pageWidth, pageHeight );
              
              float scaleX = destRect.Width / 850;
              float scaleY = destRect.Height / 1100;
      
              for ( int i = 0; i < cardDataTabPage.Controls.Count; i++ )
              {
                  if ( cardDataTabPage.Controls\[i\].GetType() == richTextBox1.GetType() )
                  {
                      RichTextBox rich = (RichTextBox)cardDataTabPage.Controls\[i\];
                      g.DrawRectangle( boxPen, rich.Bounds );  // draws 3x5 box where card should be
                      g.DrawString( rich.Text, rich.Font, Brushes.Black, 
                          rich.Bounds.Left \* scaleX, rich.Bounds.Top \* scaleY, new StringFormat() );
                  }
              }
          }
      

      Thanks.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you're not printing a RichTextBox at all, you are simply painting its textual content, in a single font, style, and color, at a specific point without taking care of width and height. There are DrawString overloads that take a Rectangle and perform a word wrap for you, keeping everything in the allotted width. If you really want to print an RTB, you need to call some Win32 functions, using P/Invoke. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Getting an article published on CodeProject should be easier and faster.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, you're not printing a RichTextBox at all, you are simply painting its textual content, in a single font, style, and color, at a specific point without taking care of width and height. There are DrawString overloads that take a Rectangle and perform a word wrap for you, keeping everything in the allotted width. If you really want to print an RTB, you need to call some Win32 functions, using P/Invoke. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Getting an article published on CodeProject should be easier and faster.


        M Offline
        M Offline
        Mark F
        wrote on last edited by
        #3

        I figured it out after I found a reference that states: when passing a RectangleF as one of the parameters, DrawString automatically wraps the text inside the destination's bounding rectangle. So I changed by method call to:

        RectangleF cardBox = new RectangleF( rich.Bounds.X + 5, rich.Bounds.Y + 5, rich.Bounds.Width - 5, rich.Bounds.Height - 5 );
        g.DrawString( rich.Text, rich.Font, Brushes.Black, cardBox, frmt );
        

        The 'cardBox' rectangle is deflated by 5 pixels for padding. I also included a StringFormat to center the text on the card.

        Mark

        L 1 Reply Last reply
        0
        • M Mark F

          I figured it out after I found a reference that states: when passing a RectangleF as one of the parameters, DrawString automatically wraps the text inside the destination's bounding rectangle. So I changed by method call to:

          RectangleF cardBox = new RectangleF( rich.Bounds.X + 5, rich.Bounds.Y + 5, rich.Bounds.Width - 5, rich.Bounds.Height - 5 );
          g.DrawString( rich.Text, rich.Font, Brushes.Black, cardBox, frmt );
          

          The 'cardBox' rectangle is deflated by 5 pixels for padding. I also included a StringFormat to center the text on the card.

          Mark

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          you want -10 to center, not -5, and even that is just an approximation as it will paint flush left. to really center, you need to use Graphics.MeasureString to find actual width, then adjust Left (and maybe Top) while keeping the original Width and Height. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Getting an article published on CodeProject should be easier and faster.


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups