title: Calculating an emSize value for a font so that text fills an area
body: <p>I have been pulling my hair out on this one for ages now, so thought I'd try to get some help...
  I'm working with PDFsharp (may or may not be significant) and trying to put a watermark into a pdf document when it gets downloaded via my asp.net web app.</p>
  
  <p>My problem comes with that the user needs to be able to define the text in the watermark, so I can't use a fixed text size, also, the pdf page size can change.</p>
  
  <p>Assuming that I'm using Arial and bold, and can find the page width in cm/mm/inch/pt, how do I calculate the font's emSize that is needed so that whatever text is entered grows/shrinks to fill the  width?</p>
  
  <p>The PDFsharp XFont constructor takes font name and emSize.</p>
  
  <p>Edit:
  Many thanks for the suggestions guys, this is what I implemented in the end:</p>
  
  <pre><code>    PdfDocument doc = PdfReader.Open(stream, PdfDocumentOpenMode.Modify);
  
      foreach (PdfPage page in doc.Pages)
      {
          double watermarkWidth = (Math.Sqrt(Math.Pow(page.Height.Point, 2) + Math.Pow(page.Width.Point, 2)));
          //reduce by 10% so that the wording doesn't go right into the corners
          watermarkWidth -= (watermarkWidth / 10);
  
          XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
  
          double emSize = 150;
          XFont font = new XFont("Arial", emSize, XFontStyle.Bold);
          XSize size = gfx.MeasureString(watermark, font);
  
          while (size.Width &gt; watermarkWidth &amp;&amp; emSize &gt; 10)
          {
              emSize -= 5;
              font = new XFont("Arial", emSize, XFontStyle.Bold);
              size = gfx.MeasureString(watermark, font);
          }
  
          gfx.TranslateTransform(page.Width / 2, page.Height / 2);
          gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
          gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
  
          XGraphicsPath path = new XGraphicsPath();
  
          path.AddString(watermark, font.FontFamily, XFontStyle.Bold, emSize,
              new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
              XStringFormats.Default);
  
          XPen pen = new XPen(XColor.FromArgb(75, 255, 0, 0), 2);
  
          gfx.DrawPath(pen, path);
      }
      doc.Save(stream, false);
  </code></pre>