Печать длинных строк в C# с Graphics.DrawString - PullRequest
0 голосов
/ 18 февраля 2020

Доброе утро. Я должен напечатать несколько длинных строк, которые падают с полей моей страницы. Я не могу автоматически разбивать как многострочную строку.

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
  ...
  string line ="Condimentum a a ac aenean parturient risus suscipit et orci scelerisque convallis porttitor enim venenatis viverra.Egestas nibh natoque mus etiam a parturient feugiat hendrerit a sagittis viverra dui ante varius lectus arcu."
  float leftMargin = ev.MarginBounds.Left;
  float topMargin = ev.MarginBounds.Top;    
  ev.Graphics.DrawString(line, printFont, Brushes.Black, new RectangleF(leftMargin, yPos, 400.0f, 200.0f));
  ...
}

Я пытался использовать перегрузку со спецификацией ограничивающего прямоугольника, как вы можете видеть, но в результате получается замятие символов на страницы, потому что строки из длинной строки напечатаны поверх самого себя.

Есть ли быстрый способ решить эту проблему?

Спасибо

1 Ответ

0 голосов
/ 18 февраля 2020

решено Я наконец решил свою проблему. Это не так легко объяснить, поэтому я публикую здесь фрагмент кода, который показывает, что я сделал:

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        float printAreaHeight = ev.MarginBounds.Height;
        float printAreaWidth = ev.MarginBounds.Width;
        string line = null;
        float yPos = topMargin;

        int charactersFitted = 0;
        int linesFilled = 0;
        SizeF theSize = new SizeF();
        Font printFont = new Font("Arial", 12, FontStyle.Regular);

        SizeF layoutSize = new SizeF(printAreaWidth, printAreaHeight);

        // Calculate the number of lines per page.
        linesPerPage = printAreaHeight / printFont.GetHeight(ev.Graphics);

        // Print each line of the array.
        while (count < linesPerPage && lineIdx < linesArray.Count())
        {
            line = linesArray[lineIdx++];
            theSize = ev.Graphics.MeasureString(line, printFont, layoutSize, new StringFormat(), out charactersFitted, out linesFilled);
            ev.Graphics.DrawString(line, printFont, Brushes.Black, new RectangleF(50.0F, yPos, theSize.Width, theSize.Height));
            yPos += (1 + linesFilled) * printFont.GetHeight(ev.Graphics);
            count += linesFilled + 1;
         }

         // If more lines exist, print another page.
         if (count > linesPerPage)
            ev.HasMorePages = true;
         else
            ev.HasMorePages = false;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...