Like Tree1Likes
  • 1 Post By nvoigt

printing coding for c#

This is a discussion on printing coding for c# within the C# Programming forums, part of the General Programming Boards category; how to print invoice in c sharp plz help .................

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    printing coding for c#

    how to print invoice in c sharp plz help ..............

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Well posting on the c# forum would be good - thread moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You're going to have to be a little more specific...
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Code:
    using System;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace PrintEvents_c
    {
        public partial class PrintEvents : Form
        {
            private Font PrintFont;
            private StreamReader PrintStream;
            public PrintEvents()
            {
                InitializeComponent();
            }
          
    
            public void Print_It()
            {
                try
                {
                    //Get the file to print
                    PrintStream = new StreamReader("html.docx");
                    try
                    {
                        PrintFont = new Font("Times New Roman", 14);
                        PrintDocument pd = new PrintDocument();
    
                        //Assign my overloaded version of the standard print controller
                        //Send it a reference to the label so it can tell us what is
                        //going on.
                        pd.PrintController = new MyPrintController(ref lblEvents);
    
                        //Install event handlers
                        pd.BeginPrint += new PrintEventHandler(this.pd_StartPrint);
                        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                        pd.EndPrint += new PrintEventHandler(this.pd_EndPrint);
    
                        // Print the document.
                        pd.Print();
                    }
                    finally
                    {
                        PrintStream.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    
            private void pd_StartPrint(object sender, PrintEventArgs ev)
            {
                lblEvents.Text += "PrintDocument: BeginPrint\n";
            }
    
            private void pd_EndPrint(object sender, PrintEventArgs ev)
            {
                lblEvents.Text += "PrintDocument: EndPrinting\n";
            }
    
            // The PrintPage event is raised for each page to be printed.
            private void pd_PrintPage(object sender, PrintPageEventArgs ev)
            {
                float linesPerPage = 0;
                float yPos = 0;
                int count = 0;
                float leftMargin = ev.MarginBounds.Left;
                float topMargin = ev.MarginBounds.Top;
                String line = null;
    
                lblEvents.Text += "PrintDocument:  PagePrint\n";
    
                // Calculate the number of lines per page.
                linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics);
    
                // Iterate over the file, printing each line. Use a basic StringFormat
                while (count++ < linesPerPage && ((line = PrintStream.ReadLine()) != null))
                {
                    //Calculate vertical position of the line.
                    yPos = topMargin + (count * PrintFont.GetHeight(ev.Graphics));
                    //This is the graphics object obtained by the PrintController
                    //OnStartPage method.  We are drawing to the printer!!
                    ev.Graphics.DrawString(line, PrintFont, Brushes.Black,
                                            leftMargin, yPos, new StringFormat());
                }
    
                // If more lines exist, print another page.
                if (line != null)
                    ev.HasMorePages = true;
                else
                    ev.HasMorePages = false;
            }
    
            private void cmdPrint_Click(object sender, System.EventArgs e)
            {
                Print_It();
               
            }
        }
    
        public class MyPrintController : StandardPrintController
        {
            private Label lblEvents;
            public MyPrintController(ref Label lbl)
                : base()
            {
                lblEvents = lbl;
            }
    
            public override void OnStartPrint(PrintDocument doc, PrintEventArgs e)
            {
                lblEvents.Text += "      PrintController: OnStartPrint\n";
                base.OnStartPrint(doc, e);
            }
            public override Graphics OnStartPage(PrintDocument doc, PrintPageEventArgs e)
            {
                lblEvents.Text += "      PrintController: OnStartPage\n";
                return (base.OnStartPage(doc, e));
            }
            public override void OnEndPage(PrintDocument doc, PrintPageEventArgs e)
            {
                lblEvents.Text += "      PrintController: OnEndPage\n";
                base.OnEndPage(doc, e);
            }
            public override void OnEndPrint(PrintDocument doc, PrintEventArgs e)
            {
                lblEvents.Text += "      PrintController: OnEndPrint\n";
                base.OnEndPrint(doc, e);
            }
    
        }
    }
    i try this coding but it not working propley

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    What do you mean "not working properly"?

    - does it crash?
    - does it print anything at all?
    - a bunch of blank pages?
    - a bunch of really small or really large characters
    - garbage characters?
    - etc etc

    For my money, I guess you need to pass the line you read onto this function, rather than just creating an empty string.
    Code:
                while (count++ < linesPerPage && ((line = PrintStream.ReadLine()) != null))
                {
                    //Calculate vertical position of the line.
                    yPos = topMargin + (count * PrintFont.GetHeight(ev.Graphics));
                    //This is the graphics object obtained by the PrintController
                    //OnStartPage method.  We are drawing to the printer!!
                    ev.Graphics.DrawString(line, PrintFont, Brushes.Black,
                                            leftMargin, yPos, new StringFormat());
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    i do this print coding for to print invoice in A4 size paper but it print many paper.
    but that paper has mean less letter. that is the problem.................

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Are you sure, that printing the contents of an MS-Word file line by line is what you want? It will look like a bunch of garbage, not like the view MS-Word presented you. Open your .docx in notepad, does it resemble your printings? If so, your print code is fine.
    inthu likes this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    thank you...... nvoigt............

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me coding
    By hakimstm1b in forum C Programming
    Replies: 4
    Last Post: 10-16-2010, 08:28 AM
  2. C++ coding help
    By abhay143 in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2009, 11:07 AM
  3. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-04-2008, 12:12 AM
  4. Coding a log in.....
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2008, 08:55 PM
  5. coding
    By rick1984 in forum C Programming
    Replies: 8
    Last Post: 07-29-2003, 05:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21