Print matrices with line break

Asked

Viewed 112 times

1

I want to print the various variables of a List. I’m trying to use foreach but it hasn’t worked. Variables are written over each other instead of a line break. Look at my code in the event printPage:

        int charactersOnPage = 0;
        int linesPerPage = 0;

        var Fonte = new Font("Arial", 18);

        Cliente cliente = Clientes[listaClientes.SelectedIndex];

        foreach (var cli in cliente.Produtos)
        {
            string impressao = string.Format("Produto: {0}", cli.NomeProduto);

            e.Graphics.MeasureString(impressao, Fonte, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);

            e.Graphics.DrawString(impressao, Fonte, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);

            impressao = impressao.Substring(charactersOnPage);

            e.HasMorePages = false;
        }

I need to have as output in print preview something like:

Produto: Carro
Produto: Bicicleta

With this code of mine, the products are all shuffled on top of each other.

It’s the first time I’m studying impression.

  • I don’t know this print engine, but I think it’s missing a line break or something

  • Have you tried using " n" or System.Environment.Newline to break the line where desired? If you have not tried, do a test. Example, if you wanted to break the line after the product name: printed string = string.Format("Product: {0}", cli.Product name) + "n";

  • @Renan yes, I tried using n but it didn’t work. System.Environment.Newline I didn’t know, I’ll take a look at it.

  • @Caputo Yes, after trying with n unsuccessfully I came here to try to discover other methods to accomplish this line break.

  • @Renan Environment.Newline also does not cause any effect.

  • @Omni tried in every way you exposed, but none worked. Strings insist on getting shuffled on top of each other.

Show 1 more comment

1 answer

2


Your problem occurs due to this piece of code:

e.Graphics.DrawString(impressao, Fonte, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);

How it’s always passing the same value of e.MarginBounds for DrawString, the various strings are all drawn within the same rectangle.

One way to solve your problem would be to create the whole text before you have it drawn:

StringBuilder sb = new StringBuilder();
foreach (var cli in cliente.Produtos)
{
    sb.AppendFormat("Produto: {0}\n", cli.NomeProduto);
}

string stringFinal = sb.ToString();

e.Graphics.MeasureString(stringFinal, Fonte, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);

e.Graphics.DrawString(stringFinal, Fonte, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);

e.HasMorePages = false;
  • 1

    I understood your logic! That was exactly the root of the problem! Thank you, big hug! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.