How do I print the contents of a TextBox in metro apps? I have read this quickstart guide on MSDN and many online tutorials, but they are very complicated and do not work with TextBox controls, only RichTextBox controls.
How do we print from a TextBox control in a metro app? Is it even possible? How?
Print a Selected Portion of TextHighlight the text you want to print. Select File > Print. Select the Page drop-down arrow and choose Print Selection. Select the Printer drop-down arrow, choose your printer, then select Print.
On the View menu, select Properties Window. Find TextBox1 in the Properties window drop-down box and change the Name property of the text box to displayText. Drag a Button control to the document and change the following properties. Now you can write the code that will run when the button is clicked.
When printing a document, you are able to see color changes from where a text box overlaps on another area that might have a background color or shape with a color. All text boxes are set to have no fill, shape outline or shape effects.
UPDATE 1
I have created a helper class which simplifies printing text box content. You can add helper class via NuGet. If you want to enhance my existing helper class, fork on GitHub
Here I am giving you the modified print sample from MSDN. I have put textbox you can write anything and that will be printed. Please note I have not done sample which prints textbox text exactly same as it is i.e formatting (bold, italic, underline, colors). I have set hard-coded print format. You can make your own format.
Stack Overflow has character limit in answer and my code is too long so posting CodePaste.net links.
XAML : http://codepaste.net/9nf261
CS : http://codepaste.net/q3hsm3
Please note that I have used some images so put images in "Images" folder
I just created a small winforms-application with a textbox (textBox1) and a button (button1). The code-behind looks like:
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();
}
}
On a click on the button the printing will be done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With