How to use Hasmorepages on this occasion?

Asked

Viewed 186 times

2

Well, I have a printing system here that is working perfectly. The problem I am facing is regarding the use of bool HasMorePages. I’m not sure how to implement it to print multiple pages if the content doesn’t fit in one. Take a look at the code:

    string[] legenda = { "Produto", "Preço", "Data da compra" };
    float[] retLarguras = { 400.0F, 100.0F, 400.0F };
    float retEspacamento = 15.0F;
    float distancia = 25.0F;

    float espacamentoVertical = 250F;
    float espacamentoHorizontal = 20.0F;

    bool imprimiuLogo = true;

    private void printDocumento_PrintPage(object sender, PrintPageEventArgs e)
    {
            var Fonte = new Font("Arial", 9);
            var FonteB = new Font("Arial", 12, FontStyle.Bold);

            if (imprimiuLogo)
            {
                e.Graphics.DrawImage(Properties.Resources.logoI, 20, 20);
                imprimiuLogo = false;
            }

            StringFormat alinhar = new StringFormat(StringFormatFlags.NoClip);

            alinhar.Alignment = StringAlignment.Near;

            Cliente cliente = Clientes[listaClientes.SelectedIndex];

            RectangleF retInicial = new RectangleF(espacamentoHorizontal, espacamentoVertical, retLarguras[0], distancia);
            RectangleF retAtual = retInicial;

            int coluna = -1;

            RectangleF retORC = new RectangleF(20F, 210F, 0, 0);
            e.Graphics.DrawString(string.Format("Orçamentos do(a) cliente {0} listados abaixo.", cliente.Nome), Fonte, Brushes.Black, retORC, alinhar);

            for (int col = 0; col < legenda.Length; col++)
            {
                e.Graphics.DrawString(legenda[col], FonteB, Brushes.Black, retAtual, alinhar);
                retAtual.Offset(retEspacamento + retLarguras[col], 0F);
            }

            foreach (var cli in cliente.Produtos)
            {
                coluna++;

                retAtual = retInicial;
                retAtual.Offset(0F, (float)(coluna + 1) * distancia * 1.5F);
                e.Graphics.DrawString(cli.NomeProduto, Fonte, Brushes.Black, retAtual, alinhar);

                retAtual.Offset(retEspacamento + retLarguras[0], 0F);
                retAtual.Width = retLarguras[1];
                e.Graphics.DrawString(string.Format("R${0}", cli.ValorProduto), Fonte, Brushes.Black, retAtual, alinhar);
                retAtual.Offset(retEspacamento + retLarguras[1], 0F);
                retAtual.Width = retLarguras[2];
                e.Graphics.DrawString(cli.DataCompra, Fonte, Brushes.Black, retAtual, alinhar);
            }

            RectangleF retVAL = new RectangleF(20F, 50F, 500F, 0);
            retVAL = retInicial;
            retVAL.Height = 0;
            retVAL.Width = 780F;
            retVAL.Offset(0F, (float)(coluna + 1) * distancia * 1.5F + 35F);
            string stringF = "\n\nAgradecemos a preferência.";
            double total = cliente.Produtos.Sum(x => x.ValorProduto);
            double porcentagem = total * 5 / 100;
            double totalD = total - porcentagem;
            if (cliente.Observacoes != null)
            {
                e.Graphics.DrawString(string.Format("Valor total: R${0}\nValor com desconto para pagamento à vista (-5%): R${1}\n\n''{2}''", total, totalD, cliente.Observacoes) + stringF, Fonte, Brushes.Black, retVAL, alinhar);
            }
            else
            {
                e.Graphics.DrawString(string.Format("Valor total: R${0}\nValor com desconto para pagamento à vista (-5%): R${1}", total, totalD) + stringF, Fonte, Brushes.Black, retVAL, alinhar);
            }
    }

To call the event, I have:

internal void Imprimir()
    {
            Margins margem = new Margins(20, 20, 20, 20);
            printDocumento.DefaultPageSettings.Margins = margem;
            printPrevisao.ShowDialog();
    }

I know to do this I have to use e.Graphics.MeasureString and check if the number of characters of strings that are written has not exceeded the page limit, but with this system I am not sure how to do this. In this case, the drawn columns that do not fit on the page should be passed to the other page along with the string that is written below them.

  • when you find an answer that you like in the reward period, mark it and comment on it with my name. I will give the reward :D

  • Hasmorepages is used to indicate that the event will be invoked again, in your case there is no way to use, the whole code is embedded inside the delegate, refactoring is necessary. Measurestring will not help either, because its code uses rectangles to format spacing, the calculations will not match, but it is possible to calculate how much was outside the page mathematically (tamPag - Tamretsomados). It will be very difficult to help without a functional example that can be reproduced.

No answers

Browser other questions tagged

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