WPF FlowDocument按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;不要将第一段移到文档的中心。它看起来像放在文档一半的中央。 我做错了什么? 预先感谢您的帮助

iCMS 回答:WPF FlowDocument按A4的一半打印对齐内容

页面似乎被设置为两列。您的标题将居于两列的第一列。

我能够通过添加该行来对其进行修复

            doc.ColumnWidth = doc.PageWidth;

之后

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

ColumnWidth属性的documentation指出默认值为NaN,这将导致仅显示一列,但似乎是一个NaN值不起作用,我不确定为什么。

本文链接:https://www.f2er.com/1540535.html

大家都在问