How to center text using print document c #?

3

I'm making an impression and would like to center the text or use this code in printpage

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

How do I centralize?

    
asked by anonymous 08.07.2016 / 03:51

1 answer

0

Douglas, There are several ways to center the text, one of them is getting the rectangle from 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);

            }

As a PrintDocument property, the PageBounds property lets you capture the rectangle associated with the page border. I increased the value of the Y coordinate to be "like" a Title.

    
11.07.2016 / 15:37