1

テキストボックスのテキストを印刷したいのですが、行が大きすぎてページから外れると、境界内に行に含めることができる文字数を知ることができます。サイズとフォントが変わること。

私はすでにどこかでウェブから入手したこのコードを持っているので、あなたは私が欲しいものを知っています。

private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;

            Font printFont = txtMain.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            // Work out the number of lines per page, using the MarginBounds.
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            // Iterate over the string using the StringReader, printing each line.
            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                // calculate the next line position based on the height of the font according to the printing device
                yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                // draw the next line in the rich edit control
                e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }
            // If there are more lines, print another page.
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            myBrush.Dispose();
        }

前もって感謝します。

4

2 に答える 2

2

FontMetricsについて読む必要があります

フォントメトリックを取得する方法がわかったら、それを描画領域と組み合わせて使用​​して、配置できる文字数を決定できます。

編集:次のようにペイント領域のサイズを取得できます:

//This gives you a rectangle object with a length and width.
Rectangle bounds = e.MarginBounds;

ページの幅を取得したら、フォントからフォントメトリックを取得し、ページ幅をフォントの幅で割ります。その床を取り、それはあなたがページに水平に置くことができる文字数です。同じ単位を使用していることを確認してください(幅はデフォルトのピクセルです)。必要に応じて、垂直に対しても同じことができます。

于 2012-02-16T19:47:52.787 に答える