WPF FlowDocument 按 A4 的一半打印对齐内容

WPF FlowDocument print aligning content by half of the A4

我有一个带有代码的打印按钮:

            PrintDialog printDlg = new PrintDialog();

            FlowDocumentPageViewer docpv = new FlowDocumentPageViewer();

            FlowDocument doc = new FlowDocument();
            doc.PagePadding = new Thickness(10);                
            doc.PageWidth = 793;// a4 width in px
           
         
            Section sec = new Section();
            



            Paragraph p = new Paragraph();
            

            p = new Paragraph(new Run(Setup.GUI_texts_collection[193] + " " + vInvoice.Id.ToString()));
            p.Margin = new Thickness(10);
            p.FontSize = 30;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            p.TextAlignment = TextAlignment.Center;               
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Type_lbl.Content + ":  " + this.TypeComboBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Code_lbl.Content + ":  " + vInvoice.Code));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Date_lbl.Content + ":  " + this.DateTextBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Sender_lbl.Content + ":  " + this.SenderComboBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Receiver_lbl.Content + ":  " + this.ReceiverComboBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.From_stock_lbl.Content + ":  " + this.From_stockComboBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.To_stock_lbl.Content + ":  " + this.To_stockComboBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Cash_lbl.Content + ":  " + this.Paid_by_cashTextBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Bank_lbl.Content + ":  " + this.Paid_by_bankTextBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Debt_lbl.Content + ":  " + this.DebtTextBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            p = new Paragraph(new Run(this.Total_amount_lbl.Content + ":  " + this.Total_amountTextBox.Text));
            p.Margin = new Thickness(5);
            p.FontSize = 20;
            p.FontFamily = new FontFamily("Arial");
            p.FontWeight = FontWeights.Bold;
            sec.Blocks.Add(p);

            
            //// Create first Paragraph  
            doc.Blocks.Add(sec);




            doc.Name = "FlowDoc";
            IDocumentPaginatorSource idpSource = doc;


        
            if (printDlg.ShowDialog() == true)
            {
                printDlg.PrintDocument(idpSource.DocumentPaginator, "Invoice " + vInvoice.Id.ToString());
            }

但结果我得到以下文件Printed result

所以,为什么 p.TextAlignment = TextAlignment.Center;不将第一段移动到文档的中心。看起来它把它放在文档的一半的中心。 我做错了什么? 在此先感谢您的帮助

页面似乎设置了两栏。您的标题位于两列中的第一列中央。

我可以通过添加行

来修复它
            doc.ColumnWidth = doc.PageWidth;

之后

            doc.PageWidth = 793;// a4 width in px

ColumnWidth 属性 的 documentation 声明默认值为 NaN 并且这导致仅显示一列,但似乎NaN 值不起作用,我不确定为什么。