简体   繁体   English

UWP richeditbox打印多页

[英]UWP richeditbox print multiple pages

I haven't been able to do multipage printing with Richeditbox. 我无法使用Richeditbox进行多页打印。 I have Richeditbox in Xaml named Editor. 我在Xaml中有Richeditbox,名为Editor。 I use custom GetText() function to get all content inside of Editor. 我使用自定义GetText()函数来获取Editor内的所有内容。 I have been able to do printing with single page but don't have idea how i can make multiple pages. 我已经可以单页打印,但不知道如何制作多页。

I have tried to look Microsoft Documentation and this PrintHelper class . 我试图查看Microsoft文档和此PrintHelper类 Still I am not sure how I should implement this to my project. 仍然我不确定如何将其实施到我的项目中。

So main question is how should I do printing with multiple pages with richeditbox? 所以主要的问题是我应该如何使用richeditbox打印多个页面?

Below is my project printing code and yes i know that there is hard coded: printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final); 下面是我的项目打印代码,是的,我知道有硬编码:printDoc.SetPreviewPageCount(1,PreviewPageCountType.Final); But don't know how i should count those pages 但不知道我该如何计算这些页面

 private PrintManager printMan;
 private PrintDocument printDoc;
 private IPrintDocumentSource printDocSource;

public MainPage()
{
    InitializeComponent();
    // Register for PrintTaskRequested event
    printMan = PrintManager.GetForCurrentView();
    printMan.PrintTaskRequested += PrintTaskRequested;

    // Build a PrintDocument and register for callbacks
    printDoc = new PrintDocument();
    printDocSource = printDoc.DocumentSource;
    printDoc.Paginate += Paginate;
    printDoc.GetPreviewPage += GetPreviewPage;
    printDoc.AddPages += AddPages;
}

private async void Print_Click(object sender, RoutedEventArgs e)
{
    if (PrintManager.IsSupported())
    {
        try
        {
            // Show print UI
            await PrintManager.ShowPrintUIAsync();
        }
        catch
        {
            // Printing cannot proceed at this time
            ContentDialog noPrintingDialog = new ContentDialog()
            {
                Title = "Printing error",
                Content = "\nSorry, printing can' t proceed at this time.",
                PrimaryButtonText = "OK"
            };
            await noPrintingDialog.ShowAsync();
        }
    }
    else
    {
        // Printing is not supported on this device
        ContentDialog noPrintingDialog = new ContentDialog()
        {
            Title = "Printing not supported",
            Content = "\nSorry, printing is not supported on this device.",
            PrimaryButtonText = "OK"
        };
        await noPrintingDialog.ShowAsync();
    }

}

private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
    // Create the PrintTask.
    // Defines the title and delegate for PrintTaskSourceRequested
    var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);

    // Handle PrintTask.Completed to catch failed print jobs
    printTask.Completed += PrintTaskCompleted;
}

private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
    // Set the document source.
    args.SetSource(printDocSource);
}

private void Paginate(object sender, PaginateEventArgs e)
{
    printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}

private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
    string text = GetText(); ;
    RichEditBox richTextBlock = new RichEditBox();
    richTextBlock.Document.SetText(TextSetOptions.FormatRtf, text);
    richTextBlock.Background = new SolidColorBrush(Windows.UI.Colors.White);
    printDoc.SetPreviewPage(e.PageNumber, richTextBlock);
}


private void AddPages(object sender, AddPagesEventArgs e)
{
    string text = GetText(); ;
    RichEditBox richTextBlock = new RichEditBox();
    richTextBlock.Document.SetText(TextSetOptions.FormatRtf, text);
    richTextBlock.Background = new SolidColorBrush(Windows.UI.Colors.White);
    richTextBlock.Padding = new Thickness(20,20,20,20);
    printDoc.AddPage(richTextBlock);

    // Indicate that all of the print pages have been provided
    printDoc.AddPagesComplete();
}

private async void PrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
    // Notify the user when the print operation fails.
    if (args.Completion == PrintTaskCompletion.Failed)
    {
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            ContentDialog noPrintingDialog = new ContentDialog()
            {
                Title = "Printing error",
                Content = "\nSorry, failed to print.",
                PrimaryButtonText = "OK"
            };
            await noPrintingDialog.ShowAsync();
        });
    }
}

RichTextBlock has OverflowContentTarget property. RichTextBlock具有OverflowContentTarget属性。 You should specify RichTextBlockOverflow control there. 您应该在那里指定RichTextBlockOverflow控件。 RichTextBlockOverflow control may also have OverflowContentTarget. RichTextBlockOverflow控件也可能具有OverflowContentTarget。 So you add additinal page and look if it has overflow content or not. 因此,您添加了其他页面,并查看它是否具有溢出内容。 Content which didn't suit the page flows to the next overflow control and so on. 不适合页面的内容将流入下一个溢出控件,依此类推。 So you render pages one by one until there is nothing in overflow and at that moment you know page count. 因此,您可以一页一页地渲染页面,直到没有任何溢出为止,此时您就知道页面数。

Just exactly what I said but in their official docs : 正是我所说的,但在他们的官方文档中

lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription);

   // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview
   // page has extra content
   while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible)
   {
         lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription);
   }

MS docs skip important points and hard to understand. 微软文档跳过重点,难以理解。 The best documentation on printing is this blog by Diederic Krols. 关于印刷的最佳文档是Diederic Krols撰写的此博客 There is also nice article written by him how to print from ItemsControl. 他还写了一篇不错的文章 ,介绍如何从ItemsControl打印。 (but it's more advanced) It's for windows 8, but API didn't change since that time. (但更高级)用于Windows 8,但自那时以来API一直没有改变。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM