UWP richeditbox打印多页
UWP richeditbox print multiple pages
我无法使用 Richeditbox 进行多页打印。我在 Xaml 中有名为 Editor 的 Richeditbox。我使用自定义 GetText() 函数来获取编辑器中的所有内容。我已经能够打印单页,但不知道如何制作多页。
我已尝试查看 Microsoft 文档和此 PrintHelper class。我仍然不确定我应该如何在我的项目中实现它。
所以主要问题是我应该如何使用 richeditbox 打印多页?
下面是我的项目打印代码,是的,我知道有硬编码:printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
但不知道我应该如何计算这些页数
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 有 OverflowContentTarget 属性。您应该在那里指定 RichTextBlockOverflow 控件。 RichTextBlockOverflow 控件也可能有 OverflowContentTarget。因此,您添加附加页面并查看它是否有溢出内容。不适合页面的内容流向下一个溢出控制等等。因此,您一页一页地渲染页面,直到溢出时没有任何内容,那时您就知道页数了。
正是我所说的,但在他们的官方 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 文档跳过了要点并且难以理解。
最好的打印文档是他写的 blog by Diederic Krols. There is also nice article 如何从 ItemsControl 打印。 (但它更高级)它适用于 windows 8,但 API 从那时起就没有改变。
我无法使用 Richeditbox 进行多页打印。我在 Xaml 中有名为 Editor 的 Richeditbox。我使用自定义 GetText() 函数来获取编辑器中的所有内容。我已经能够打印单页,但不知道如何制作多页。
我已尝试查看 Microsoft 文档和此 PrintHelper class。我仍然不确定我应该如何在我的项目中实现它。
所以主要问题是我应该如何使用 richeditbox 打印多页?
下面是我的项目打印代码,是的,我知道有硬编码:printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final); 但不知道我应该如何计算这些页数
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 有 OverflowContentTarget 属性。您应该在那里指定 RichTextBlockOverflow 控件。 RichTextBlockOverflow 控件也可能有 OverflowContentTarget。因此,您添加附加页面并查看它是否有溢出内容。不适合页面的内容流向下一个溢出控制等等。因此,您一页一页地渲染页面,直到溢出时没有任何内容,那时您就知道页数了。
正是我所说的,但在他们的官方 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 文档跳过了要点并且难以理解。 最好的打印文档是他写的 blog by Diederic Krols. There is also nice article 如何从 ItemsControl 打印。 (但它更高级)它适用于 windows 8,但 API 从那时起就没有改变。