如何打印TextBox的内容

如何在metro应用程序中打印TextBox的内容? 我已经阅读了MSDN上的这个快速入门指南和许多在线教程,但它们非常复杂, 不能与TextBox控件一起使用, 只能使用RichTextBox控件

我们如何从Metro应用程序中的TextBox控件进行打印? 它甚至可能吗? 怎么样?

更新1

我创建了一个帮助类,简化了打印文本框内容。 您可以通过NuGet添加帮助程序类。 如果你想增强我现有的助手类,请在GitHub上进行分叉


在这里,我将从MSDN中为您提供修改后的打印样本 。 我已经把文字框你可以写任何东西,并将打印。 请注意我没有完成打印文本框文本的样本,即格式化(粗体,斜体,下划线,颜色)。 我已经设置了硬编码的打印格式。 你可以制作自己的格式。

Stack Overflow在回答中有字符限制,我的代码太长,所以发布CodePaste.net链接。

XAML: http : //codepaste.net/9nf261

CS: http : //codepaste.net/q3hsm3

请注意我已经使用了一些图像,因此将图像放在“图像”文件夹中

我刚刚用文本框(textBox1)和一个按钮(button1)创建了一个小的winforms-application。 代码隐藏看起来像:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawString(this.textBox1.Text, this.textBox1.Font, Brushes.Black, 10, 25); } private void button1_Click(object sender, EventArgs e) { PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += PrintDocumentOnPrintPage; printDocument.Print(); } } 

点击按钮即可完成打印。

我建议你在这里查看我的问题 ,其中我将介绍一个最简单的例子,你可以在页面上打印一些东西(你可以将这些代码添加到你现有的任何页面上 – 只需替换我用过的示例文本框的文本)无论你的心愿如何)。 他们使用富文本框的原因是因为它可以检测文本何时从页面溢出,因此他们使用该信息创建具有另一个富文本框的新页面,直到不再发生溢出。 无论如何,您可以使用自己的字符串解析器将文本拆分到不同的页面上。 基本上,在Windows 8应用程序中打印将打印您想要的任何UIElement,因此您几乎可以通过XAML以编程方式对齐页面,并按照您为任何其他Windows应用程序设置样式的方式设置样式。 说真的,检查一下这个问题,这将是一个巨大的帮助。 我花了几个小时将PrintSample攻击到最简单的情况,直到我弄清楚它是如何工作的。 没有必要重新发明轮子,利用我的挣扎来获得优势,这就是Stack的全部意义所在。 干杯!

编辑:为了您的方便,我会在这里提出代码,伙计们。

第1步:使用您的文本框将此代码添加到页面。

  protected PrintDocument printDocument = null; protected IPrintDocumentSource printDocumentSource = null; internal List printPreviewElements = new List(); 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(); } 

第2步:将block.Text替换为您想要的文本。

第3步:使用打印按钮显示打印UI:

  private async void PrintDocument(object sender, RoutedEventArgs e) { await Windows.Graphics.Printing.PrintManager.ShowPrintUIAsync(); } 

第4步:在App.xaml中放置RequestedTheme =“Light”,你就完成了。 注意:可以在此XAML类中以您希望的方式设置文本框的样式,而不必设置整个应用程序的主题。

步骤5(稍后开启):您可能需要考虑添加自己的新页面检测逻辑,该逻辑一直调用该方法以创建新页面。

第6步(现在):与M $负责让我们挣扎的人发生争执。