I urge you to check my question here in which I illustrate one of the simplest cases in which you can print something off a page (and you can add that code onto any page you currently have - just replace the example text box's text I use to whatever your heart desires). The reason why they use the rich text boxes is because it can detect when text overflows off the page, thus they use that information to create a new page with another rich text box until no more overflowing occurs. No matter, you can use your own string parser to split your text up on different pages. Essentially, printing in Windows 8 apps will print any UIElement you want, so you can pretty much XAML align your page programmatically and style it just the way you'd style any other Windows app. Seriously, check the question, it'll be a huge help. I spent hours hacking the PrintSample down to the simplest case until I figured out how it all worked. No point in reinventing the wheel, use my struggles to your advantage, that's what Stack is all about. Cheers!
Edit: I'll pose the code here for your convenience, guys.
Step 1: Add this code to the page with your text box.
protected PrintDocument printDocument = null;
protected IPrintDocumentSource printDocumentSource = null;
internal List<UIElement> printPreviewElements = new List<UIElement>();
protected event EventHandler pagesCreated;
protected void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
PrintTask printTask = null;
printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
{
printTask.Completed += async (s, args) =>
{
if (args.Completion == PrintTaskCompletion.Failed)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
MessageDialog dialog = new MessageDialog("Something went wrong while trying to print. Please try again.");
await dialog.ShowAsync();
});
}
};
sourceRequested.SetSource(printDocumentSource);
});
}
protected void RegisterForPrinting()
{
printDocument = new PrintDocument();
printDocumentSource = printDocument.DocumentSource;
printDocument.Paginate += CreatePrintPreviewPages;
printDocument.GetPreviewPage += GetPrintPreviewPage;
printDocument.AddPages += AddPrintPages;
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequested;
}
protected void UnregisterForPrinting()
{
if (printDocument != null)
{
printDocument.Paginate -= CreatePrintPreviewPages;
printDocument.GetPreviewPage -= GetPrintPreviewPage;
printDocument.AddPages -= AddPrintPages;
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested -= PrintTaskRequested;
}
}
protected void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
{
printPreviewElements.Clear();
PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
PrintPageDescription pageDescription = printingOptions.GetPageDescription(0);
AddOnePrintPreviewPage(pageDescription);
if (pagesCreated != null)
{
pagesCreated.Invoke(printPreviewElements, null);
}
((PrintDocument)sender).SetPreviewPageCount(printPreviewElements.Count, PreviewPageCountType.Intermediate);
}
protected void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
{
((PrintDocument)sender).SetPreviewPage(e.PageNumber, printPreviewElements[e.PageNumber - 1]);
}
protected void AddPrintPages(object sender, AddPagesEventArgs e)
{
foreach (UIElement element in printPreviewElements)
{
printDocument.AddPage(element);
}
((PrintDocument)sender).AddPagesComplete();
}
protected void AddOnePrintPreviewPage(PrintPageDescription printPageDescription)
{
TextBlock block = new TextBlock();
block.Text = "This is an example.";
block.Width = printPageDescription.PageSize.Width;
block.Height = printPageDescription.PageSize.Height;
printPreviewElements.Add(block);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
RegisterForPrinting();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
UnregisterForPrinting();
}
Step 2: Replace block.Text with your desired text.
Step 3: Use a print button to show the print UI:
private async void PrintDocument(object sender, RoutedEventArgs e)
{
await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync();
}
Step 4: Put RequestedTheme="Light" in your App.xaml and you're done. Note: Might be able to alternatively style the textbox the way you want in this XAML class and not have to set the theme of the entire app.
Step 5 (Later On): You might want to consider adding in your own new page detection logic that keeps calling that method up top to create a new page.
Step 6 (Right Now): Get into a fight with the guy at M$ who's responsible for making us struggle.