У меня есть система печати, которая работает в моем проекте UWP.
Для подготовки содержимого для печати я использую это:
(Источник: https://blogs.u2u.be/diederik/post/Printing-from-MVVM-XAML-Windows-8-Store-apps)
public void RegisterForPrinting(Page sourcePage, Type printPageType, object viewModel)
{
this.callingPage = sourcePage;
if (PrintingRoot == null)
{
this.OnStatusChanged(new PrintServiceEventArgs("The calling page has no PrintingRoot Canvas."));
return;
}
this.printPageType = printPageType;
this.DataContext = viewModel;
// Prep the content
this.PreparePrintContent();
// Create the PrintDocument.
printDocument = new PrintDocument();
// Save the DocumentSource.
printDocumentSource = printDocument.DocumentSource;
// Add an event handler which creates preview pages.
printDocument.Paginate += PrintDocument_Paginate;
// Add an event handler which provides a specified preview page.
printDocument.GetPreviewPage += PrintDocument_GetPrintPreviewPage;
// Add an event handler which provides all final print pages.
printDocument.AddPages += PrintDocument_AddPages;
// Create a PrintManager and add a handler for printing initialization.
PrintManager printMan = PrintManager.GetForCurrentView();
try
{
printMan.PrintTaskRequested += PrintManager_PrintTaskRequested;
this.OnStatusChanged(new PrintServiceEventArgs("Registered successfully."));
}
catch (InvalidOperationException)
{
// Probably already registered.
this.OnStatusChanged(new PrintServiceEventArgs("You were already registered."));
}
}
private void PreparePrintContent()
{
// Create and populate print page.
var printPage = Activator.CreateInstance(this.printPageType) as Page;
printPage.DataContext = this.DataContext;
// Create print template page and fill invisible textblock with empty paragraph.
// This pushes all real content into the overflow.
firstPage = new PrintPage();
firstPage.AddContent(new Paragraph());
// Move content from print page to print template - paragraph by paragraph.
var printPageRtb = printPage.Content as RichTextBlock;
while (printPageRtb.Blocks.Count > 0)
{
var paragraph = printPageRtb.Blocks.First() as Paragraph;
printPageRtb.Blocks.Remove(paragraph);
var container = paragraph.Inlines[0] as InlineUIContainer;
if (container != null)
{
// Place the paragraph in a new textblock, and measure it.
var measureRtb = new RichTextBlock();
measureRtb.Blocks.Add(paragraph);
PrintingRoot.Children.Clear();
PrintingRoot.Children.Add(measureRtb);
PrintingRoot.InvalidateMeasure();
PrintingRoot.UpdateLayout();
measureRtb.Blocks.Remove(paragraph);
// Apply line height to trigger overflow.
paragraph.LineHeight = measureRtb.ActualHeight;
}
firstPage.AddContent(paragraph);
};
// Send it to the printing root.
PrintingRoot.Children.Clear();
PrintingRoot.Children.Add(firstPage);
}
PrintPage
public sealed partial class PrintPage : Page
{
public PrintPage()
{
this.InitializeComponent();
}
public PrintPage(RichTextBlockOverflow textLinkContainer)
: this()
{
textLinkContainer.OverflowContentTarget = continuationPageLinkedContainer;
}
internal void AddContent(Paragraph block)
{
this.textContent.Blocks.Add(block);
}
}
<RichTextBlock x:Name="textContent"
Grid.Row="1"
Grid.ColumnSpan="2"
FontSize="18"
OverflowContentTarget="{Binding ElementName=continuationPageLinkedContainer}"
IsTextSelectionEnabled="True"
TextAlignment="Left"
FontFamily="Segoe UI"
VerticalAlignment="Top"
HorizontalAlignment="Left">
</RichTextBlock>
<RichTextBlockOverflow x:Name="continuationPageLinkedContainer" Grid.Row="2" />
sourcePage
<RichTextBlock x:Name="PrintContent">
<!-- Content -->
</RichTextBlock>
При настройке программы мне нужно иметь возможность печатать каждый абзац RichTextBox на отдельных страницах.
Возможно ли это?