Question:
I'm making a print and I'd like to center the text I'm using this code in the printpage
e.Graphics.DrawString("bla bla bla bla",
FontTitulo,
brush,
e.MarginBounds);
How do I centralize?
Answer:
Douglas, There are several ways to center the text, one of them is to obtain 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);
}
As this is a PrintDocument, the PageBounds property allows capturing the rectangle associated with the page boundary. I increased the Y coordinate value to "look" like a Title.