How to center text using print Document c#?

Asked

Viewed 916 times

3

I’m making an impression and would like to center the text using this code on printpage

 e.Graphics.DrawString("bla bla bla bla",
                      FontTitulo,
                      brush,
                      e.MarginBounds);

How do I center?

1 answer

0


Douglas, There are several ways to center the text, one of them is by getting the rectangle of the page boundaries.

string text1 = "Este texto será centralizado no TOPO";
            using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
            {

                //O string format serve para formatar strings
                //quanto a seu alinhamento e o proprio alinhamento da linha
                StringFormat sf = new StringFormat();

                sf.Alignment = StringAlignment.Center;

                SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);

                //Captura todo o retângulo dos limites da página
                System.Drawing.RectangleF rect = e.PageBounds;

                //Aumenta a coordenada de localização Y
                rect.Y += 50;

                //Desenhar texto centralizado
                e.Graphics.DrawString(text1, font1, drawBrush, rect, sf);

            }

How is it a Printdocument the property Pagebounds allows capturing the rectangle associated with the page limit. I increased the value of the coordinate Y to "look like" a Title.

  • gave right vlw

Browser other questions tagged

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