文字列を printdocument の中央に配置しようとしています。画像で次のことを行いましたが、機能しますが、文字列では同じようには機能しないようです。
画像を中央に配置するために使用したコードは次のとおりです
e.Graphics.DrawImage(logo, (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
中央に配置しようとしているテキストは、TabControl のタブから供給されています
using (var sf = new StringFormat())
{
height = logo.Height + 15;
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10),
new SolidBrush(tabData.ForeColor),
(e.MarginBounds.Width / 2) - (txtData.Width / 2), height, sf);
}
以下も試して、txtData.Widthの代わりにstring_size.Width /2を使用しました
SizeF string_size = e.Graphics.MeasureString(tabData.Text, tabData.Font);
編集
現在の完全なコード
float height = 0;
tabData.Text = "Date Range: 02/02/2010 - 08/09/2013"; //set just for testing
using (var logo = Properties.Resources.title)
{
e.Graphics.DrawImage(logo, e.PageBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
height = logo.Height + 15;
}
using (var sf = new StringFormat())
{
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10), new SolidBrush(tabData.ForeColor), e.PageBounds.Left + (e.PageBounds.Width / 2), height, sf);
}
PageBounds と MarginBounds の混合物を使用して Image を中央に配置する必要がある理由を理解していない