title: Graphics.DrawString center in printdocument width
body: <p>I'm attempting to center a string on a printdocument.
  I've done the following with an image and it works but doesn't seem to work the same with a string.</p>
  
  <p>Here is the code that I used to center the image</p>
  
  <pre><code>e.Graphics.DrawImage(logo, (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
  </code></pre>
  
  <p>The text i'm trying to center is being supply from a Tab in a TabControl</p>
  
  <pre><code> using (var sf = new StringFormat())
   {
         height = logo.Height + 15;
         sf.LineAlignment = StringAlignment.Center;
         sf.Alignment = StringAlignment.Center;
         e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10),
              new SolidBrush(tabData.ForeColor),
              (e.MarginBounds.Width / 2) - (txtData.Width / 2), height, sf);
    }
  </code></pre>
  
  <p>I have also tried below and used string_size.Width /2 in place of txtData.Width</p>
  
  <pre><code>SizeF string_size = e.Graphics.MeasureString(tabData.Text, tabData.Font);
  </code></pre>
  
  <p><em><strong>EDIT</em></strong></p>
  
  <p>Current full code</p>
  
  <pre><code>        float height = 0;
          tabData.Text = "Date Range: 02/02/2010 - 08/09/2013"; //set just for testing
          using (var logo = Properties.Resources.title)
          {
              e.Graphics.DrawImage(logo, e.PageBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
              height = logo.Height + 15;
          }
  
          using (var sf = new StringFormat())
          {
  
              sf.LineAlignment = StringAlignment.Center;
              sf.Alignment = StringAlignment.Center;
              e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10), new SolidBrush(tabData.ForeColor), e.PageBounds.Left + (e.PageBounds.Width / 2), height, sf);
          }
  </code></pre>
  
  <p>Don't understand why I have to use a mixture of PageBounds and MarginBounds to center the Image then with the text it will center with either both MarginBounds or both PageBounds</p>