0

我正在通过 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);
}

我将如何正确对齐?

4

1 回答 1

0

https://docs.microsoft.com/en-us/dotnet/api/system.windows.documents.flowdocument?view=net-5.0#properties 我不确定您是否可以使用 FixedDocument 执行此操作。这在 Flowdocument 中可用,但使用 TextAlignmentProperty。

于 2021-03-22T09:23:49.507 回答