Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. printing in C#.net

printing in C#.net

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    varun_mca_ju 0
    wrote on last edited by
    #1

    i have a C# windows application in Visual Studio. I have a form which is opened in a panel.i want to print the data in various controls of the form upon clicking a PRINT button.but i don't want to print the jpg image of the entire form or any of its controls.i want that the text in the controls should be printed.for e.g. i have a label NAME:- and a textbox txtName in front of the label and suppose the user enters the name john in the textbox . iwant that they should be printed as follows: NAME:- john the enitre label and text box should not be printed but only the text in them should be printed.....is there any way to do it....is there any alternative way like adding the data in textbox and label to an MS Word document and then printing the document...plz. help...thanx in advance. :confused:

    T 1 Reply Last reply
    0
    • V varun_mca_ju 0

      i have a C# windows application in Visual Studio. I have a form which is opened in a panel.i want to print the data in various controls of the form upon clicking a PRINT button.but i don't want to print the jpg image of the entire form or any of its controls.i want that the text in the controls should be printed.for e.g. i have a label NAME:- and a textbox txtName in front of the label and suppose the user enters the name john in the textbox . iwant that they should be printed as follows: NAME:- john the enitre label and text box should not be printed but only the text in them should be printed.....is there any way to do it....is there any alternative way like adding the data in textbox and label to an MS Word document and then printing the document...plz. help...thanx in advance. :confused:

      T Offline
      T Offline
      teejayem
      wrote on last edited by
      #2

      Have a look at the PrintDocument[^] class.

      Don't be overcome by evil, but overcome evil with good

      V 1 Reply Last reply
      0
      • T teejayem

        Have a look at the PrintDocument[^] class.

        Don't be overcome by evil, but overcome evil with good

        V Offline
        V Offline
        varun_mca_ju 0
        wrote on last edited by
        #3

        thanks for the reply teejayem...the link to MSDN print document class was good but in the coding we have to give the path of the file to be printed .... streamToPrint = new StreamReader ("C:\\Documents and Settings\\Administrator\\My Documents\\doc1.txt");..... but what i wanted was that the text entered by the user in the text box etc. of the running form should be printed...how can i do that?:~

        T 1 Reply Last reply
        0
        • V varun_mca_ju 0

          thanks for the reply teejayem...the link to MSDN print document class was good but in the coding we have to give the path of the file to be printed .... streamToPrint = new StreamReader ("C:\\Documents and Settings\\Administrator\\My Documents\\doc1.txt");..... but what i wanted was that the text entered by the user in the text box etc. of the running form should be printed...how can i do that?:~

          T Offline
          T Offline
          teejayem
          wrote on last edited by
          #4

          The example just shows the use of a StreamReader; However, it can be what ever type you want it to be (as long as you can get it as a string come time to print). You need a member variable for your data and in the "private void pd_PrintPage(object sender, PrintPageEventArgs ev)" (in the example) you loop through your data and print it accordingly. EDIT: Here is a quick example of what you want to do (i think). Here[^] is the image of the form. Here[^] is the image of the output. Below is the code. Please let me know if this is what you need. NOTE: there still needs more work done in the print event handler (like counting the lines for page breaks).

          public Form1()
          {
          InitializeComponent();

                  printDocument1.PrintPage += printDocument1\_PrintPage;
              }
          
              private void btnPrint\_Click(object sender, EventArgs e)
              {
                  using (PrintDialog pd = new PrintDialog())
                  {
                      if (pd.ShowDialog() == DialogResult.OK)
                      {
                          printDocument1.PrinterSettings = pd.PrinterSettings;
          
                          printDocument1.Print();
                      }
                  }
              }
          
              void printDocument1\_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
              {
                  Font objFont = new Font("Tahoma", 8.5F);
                  float fTopMargin = e.MarginBounds.Top;
                  float fLeftMargin = 50;
                  float fRightMargin = e.MarginBounds.Right - 150;
          
          
                  string Name = String.Concat("Name: ", txtName.Text);
                  string PhoneNumber = String.Concat("Phone Number: ", txtPhoneNum.Text);
                  string Email = String.Concat("Email Address: ", txtEmail.Text);
                  string DateOfBirth = String.Concat("Date Of Birth: ", txtDateOfBirth.Text);
          
                  e.Graphics.DrawString(Name, objFont, Brushes.Black, fLeftMargin, fTopMargin);
                  e.Graphics.DrawString(PhoneNumber, objFont, Brushes.Black, fRightMargin - PhoneNumber.Length, fTopMargin);
          
                  fTopMargin += objFont.GetHeight() \*
          
          V 1 Reply Last reply
          0
          • T teejayem

            The example just shows the use of a StreamReader; However, it can be what ever type you want it to be (as long as you can get it as a string come time to print). You need a member variable for your data and in the "private void pd_PrintPage(object sender, PrintPageEventArgs ev)" (in the example) you loop through your data and print it accordingly. EDIT: Here is a quick example of what you want to do (i think). Here[^] is the image of the form. Here[^] is the image of the output. Below is the code. Please let me know if this is what you need. NOTE: there still needs more work done in the print event handler (like counting the lines for page breaks).

            public Form1()
            {
            InitializeComponent();

                    printDocument1.PrintPage += printDocument1\_PrintPage;
                }
            
                private void btnPrint\_Click(object sender, EventArgs e)
                {
                    using (PrintDialog pd = new PrintDialog())
                    {
                        if (pd.ShowDialog() == DialogResult.OK)
                        {
                            printDocument1.PrinterSettings = pd.PrinterSettings;
            
                            printDocument1.Print();
                        }
                    }
                }
            
                void printDocument1\_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
                {
                    Font objFont = new Font("Tahoma", 8.5F);
                    float fTopMargin = e.MarginBounds.Top;
                    float fLeftMargin = 50;
                    float fRightMargin = e.MarginBounds.Right - 150;
            
            
                    string Name = String.Concat("Name: ", txtName.Text);
                    string PhoneNumber = String.Concat("Phone Number: ", txtPhoneNum.Text);
                    string Email = String.Concat("Email Address: ", txtEmail.Text);
                    string DateOfBirth = String.Concat("Date Of Birth: ", txtDateOfBirth.Text);
            
                    e.Graphics.DrawString(Name, objFont, Brushes.Black, fLeftMargin, fTopMargin);
                    e.Graphics.DrawString(PhoneNumber, objFont, Brushes.Black, fRightMargin - PhoneNumber.Length, fTopMargin);
            
                    fTopMargin += objFont.GetHeight() \*
            
            V Offline
            V Offline
            varun_mca_ju 0
            wrote on last edited by
            #5

            thanks a lot teejayem :rose:...your code works like a charm ...only one correction... PrintDocument printDocument1 = new PrintDocument(); had to be declared....thanks a lot...u saved my day(infact u saved me many hours of searching on google) :-D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups