How to print the contents of a TextBox

29,361

Solution 1

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

Solution 2

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.

Share:
29,361

Related videos on Youtube

jay_t55
Author by

jay_t55

Updated on July 09, 2022

Comments

  • jay_t55
    jay_t55 almost 2 years

    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?

    • jay_t55
      jay_t55 about 11 years
      Why the downvote? This is NOT a duplicate. The other questions and answers on here failed to answer my question, and the code provided is for RichTextBox controls, and does NOT work.
  • John Koerner
    John Koerner about 11 years
    Adding a link to a code download is not a good answer. If you take that file off of skydrive then this answer becomes useless. Include the code as part of the answer.
  • Farhan Ghumra
    Farhan Ghumra about 11 years
    I never remove my public samples. Then also I am posting code here
  • John Koerner
    John Koerner about 11 years
    The question is about windows store apps, which is a completely different beast.
  • Farhan Ghumra
    Farhan Ghumra about 11 years
    The question is regarding Windows Store apps, that's quite different from Windows Forms apps.
  • jay_t55
    jay_t55 about 11 years
    @Xyroid thank you for doing this. I ran into a NullReferenceException on line: ` PrintingRoot.Children.Add(page);` but I don't see why anything is null?
  • Farhan Ghumra
    Farhan Ghumra about 11 years
    I didn't face any problem, I recommend you to create new project and use my code from Skydrive. My Skydrive project is tested by me.
  • jay_t55
    jay_t55 almost 11 years
    4 lines of code in WinForms, compared to probably hundreds in a Metro app - and what for????? Just to print the contents of a TextBox which may or may not have just a single character inside of it? Microsoft really stuffed this one up.
  • jay_t55
    jay_t55 almost 11 years
    I think the fact that the code is too long to fit on SO is a pretty good indication that printing plain text from a TextBox in Metro apps is overly and unnecessarily complicated.
  • Cody Gray
    Cody Gray almost 11 years
    Good god. I have posted some exceptionally long answers, and I've never hit the length limit for answers on SO. Surely there's a bunch of unnecessary boilerplate in your code, right? Because otherwise, that's totally nuts. Wow. Someone badly needs to wrap this all in a library.
  • jay_t55
    jay_t55 almost 11 years
    @CodyGray - my whole purpose now is to do just what you suggested. To wrap it all lin a library freely available to everyone. with straight-to-the-point samples. But I need to learn how first lol.