C# WPF在流文档的每一页上打印页码

slwdgvem  于 2023-05-08  发布在  C#
关注(0)|答案(1)|浏览(322)

我正在尝试打印一个流程文档,其中每一页上都有相应的页码。
最好的办法是什么?直接在flowdocument上还是在PrintDialog上?
我一直在寻找这个,但没有成功。
先谢谢你了。

private void Btn_print_Click(object sender, RoutedEventArgs e)
        {
           

            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() == true)
            {
                IDocumentPaginatorSource idp = FlowDoc;
                pd.PrintDocument(idp.DocumentPaginator, "Flow Document");
            }
            

        }
brc7rcf0

brc7rcf01#

该代码包含注解,用于解释该过程的每个步骤

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Print_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog pd = new PrintDialog();
        if (pd.ShowDialog() == true)
        {
            // Create a new FlowDocument
            FlowDocument flowDoc = new FlowDocument();
            flowDoc.PagePadding = new Thickness(50);
            flowDoc.PageHeight = pd.PrintableAreaHeight;
            flowDoc.PageWidth = pd.PrintableAreaWidth;

            // Add content to the FlowDocument
            Paragraph para1 = new Paragraph(new Run("This is paragraph 1."));
            Paragraph para2 = new Paragraph(new Run("This is paragraph 2."));
            flowDoc.Blocks.Add(para1);
            flowDoc.Blocks.Add(para2);

            // Add a header to the FlowDocument that displays the current page number
            AddHeader(flowDoc);

            // Set the PageRangeSelection and UserPageRangeEnabled properties to allow the user to choose which pages to print
            pd.PageRangeSelection = PageRangeSelection.UserPages;
            pd.UserPageRangeEnabled = true;

            // Print the FlowDocument
            pd.PrintDocument(flowDoc.DocumentPaginator, "Flow Document");
        }
    }

    private void AddHeader(FlowDocument flowDoc)
    {
        // Create a Paragraph to hold the page number information
        Paragraph paragraph = new Paragraph();
        paragraph.TextAlignment = TextAlignment.Center;
        paragraph.FontSize = 12;
        paragraph.Inlines.Add(new Run("Page "));
        paragraph.Inlines.Add(new Run(new Bold(new Run("{0}"))));
        paragraph.Inlines.Add(new Run(" of "));
        paragraph.Inlines.Add(new Run(new Bold(new Run("{1}"))));

        // Format the string using the current page number and the total page count
        string headerText = string.Format("Page {0} of {1}", "{PageNumber}", "{PageCount}");

        // Substitute the header text into the Runs that represent the page number and page count
        ((Bold)paragraph.Inlines.ElementAt(1)).Inlines.Add(new Run(headerText));
        ((Bold)paragraph.Inlines.ElementAt(3)).Inlines.Add(new Run(headerText));

        // Create a FixedPage to hold the Paragraph
        FixedPage fixedPage = new FixedPage()
        {
            Width = flowDoc.PageWidth,
            Height = flowDoc.PageHeight,
        };
        BlockUIContainer container = new BlockUIContainer();
        container.Child = paragraph;
        fixedPage.Children.Add(container);

        // Add the FixedPage to the PageHeader property of the FlowDocument
        flowDoc.PageHeader = fixedPage;
    }
}

相关问题