问题描述
我正在通过 FixedDocument 创建一个报告,我正在后面的代码中执行它以动态地将数据添加到报告中。
我今天开始使用 FixedDocument 并在对齐文本方面陷入困境。它似乎没有居中对齐。这是我的代码:
using System.Windows.Documents;
....
public void GenerateReport()
{
FixedDocument document = new FixedDocument();
PageContent content = new PageContent();
FixedPage page = new FixedPage();
page.Width = document.DocumentPaginator.PageSize.Width;
page.Height = document.DocumentPaginator.PageSize.Height;
page.Background = System.Windows.Media.Brushes.AliceBlue;
//header first line
System.Windows.Controls.Canvas canvas = new System.Windows.Controls.Canvas();
canvas.Width = document.DocumentPaginator.PageSize.Width;
FixedPage.SetTop(canvas,15);
FixedPage.SetLeft(canvas,15);
System.Windows.Controls.TextBlock block = new System.Windows.Controls.TextBlock();
block.FontSize = 11;
block.FontWeight = FontWeights.Bold;
block.FontFamily = new System.Windows.Media.FontFamily("Tahoma");
block.Text = "This is the first line";
block.HorizontalAlignment = HorizontalAlignment.Center;
canvas.Children.Add(block);
page.Children.Add(canvas);
//header second line
System.Windows.Controls.TextBlock block2 = new System.Windows.Controls.TextBlock(); block.FontSize = 11;
block2.FontWeight = FontWeights.Bold;
block2.FontFamily = new System.Windows.Media.FontFamily("Tahoma");
block2.Text = "Daily Report";
var canvas2 = new System.Windows.Controls.Canvas();
canvas2.Children.Add(block2);
FixedPage.SetTop(canvas2,30);
FixedPage.SetTop(canvas2,30);
FixedPage.SetLeft(canvas2,15);
FixedPage.SetRight(canvas2,15);
canvas2.Width = document.DocumentPaginator.PageSize.Width;
page.Children.Add(canvas2);
((IAddChild)content).AddChild(page);
document.Pages.Add(content);
}
我将如何正确对齐?
解决方法
https://docs.microsoft.com/en-us/dotnet/api/system.windows.documents.flowdocument?view=net-5.0#properties 我不确定您是否可以使用 FixedDocument 执行此操作。这在 Flowdocument 中可用,但具有 TextAlignmentProperty。