5

My requirement is that I need to print invoice, it may contain 10 line or may contain 20 line. Every thing should be in the one invoice.

For e.g. if you go to any supermarket, if you buy 3 items, you may get small sized bill. If you buy 30 items, you might get big sized bill. I want to implement the same in my vb.NET application.

Exactly I need that how to increase printer page length through program according to the nature of bill.

I am using dot matrix printer and graphic mode printing.

What I have tried:

As of now, I have created text file and print it through Command-line print using below command

Type Printfile.txt > prn

But, the problem is I am not able to format my text file with different font, weight, or size since I am writing it as textfile (notepad).

I am using streamwriter to write the files from VB.NET and as of now I am trying to format it in text files.

I want to format some words to be bold or italic and font size variation but I am not able to do so since I am formatting with text files.

Below are the format:


Store Name
Store Address
----------------------------------------      
Gift Receipt

Transaction #:          105
Date: 11/10/2009     Time: 6:10:10
Cashier:  2          Register: 5
----------------------------------------      
Item           Description       Quantity
----------------------------------------   
567577         xyz                2
687687         abc                4
–  –           – –                –
----------------------------------------  
                     Net Amount : 6

Thank You for shopping
XYZ StoreName
We hope you’ll come back soon!

djv
  • 15,168
  • 7
  • 48
  • 72
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • 1
    You have a plain text file, and you want to get to a formatted invoice on a piece of paper. What is the format of the text (columns, rows, etc.)? What technology are you using to format it (Excel, pdf, etc.)? How are you sending it to the printer? From the begging to end, there is *way* too much work to do, and as it stands, this question is too broad. I would vote to close if I could. – djv May 09 '17 at 15:46
  • 1
    We need to see your code attempts. As said above this seems to be _too broad_. There is a lot to do but we are going to struggle to know where to start. – Bugs May 10 '17 at 10:22
  • let em update code too – Vignesh Kumar A May 10 '17 at 12:34
  • usually to do this you should look if the printer has some type of [printer control language](https://en.wikipedia.org/wiki/Printer_control_language) (like epson ESC/P2 or something else) then you can send those commands to the printer just like you did with the `type` command. ESC/P2 is not that hard to master... –  May 12 '17 at 15:34

2 Answers2

4

You can use a WebBrowser control to print an html formatted invoice instead. You will still need to figure out how to populate the invoice from the text file, according to your needs. This can be automated. For example, make a loop to add each table row. You can even use css.

Add a WebBrowser control to your form, and run this code

Dim html =
    "<html>" &
        "<head>" &
            "<style>" &
                "table, th" &
                "{" &
                    "border: 1px solid black;" &
                    "table-layout: fixed;" &
                    "width: 100px;" &
                    "border-collapse: collapse;" &
                "}" &
                ".title" &
                "{" &
                    "color: blue;" &
                "}" &
            "</style>" &
        "</head>" &
        "<body>" &
            "<p><b><div class=""title"">Store Name</div></b></p>" &
            "<p>Store Address</p>" &
            "<p><hr/></p>" &
            "<p><b>Gift Receipt</b></p>" &
            "<p>Transaction #:          105</p>" &
            "<p>Date: 11/10/2009     Time: 6:10:10</p>" &
            "<p>Cashier:  2          Register: 5</p>" &
            "<p><hr/></p>" &
            "<table>" &
                "<tr>" &
                    "<th>Item</th>" &
                    "<th>Description</th>" &
                    "<th>Quantity</th>" &
                "</tr>" &
                "<tr>" &
                    "<th>567577</th>" &
                    "<th>xyz</th>" &
                    "<th>2</th>" &
                "</tr>" &
                "<tr>" &
                    "<th>687687</th>" &
                    "<th>abc</th>" &
                    "<th>4</th>" &
                "</tr>" &
                "<tr>" &
                    "<th>- -</th>" &
                    "<th>- -</th>" &
                    "<th>-</th>" &
                "</tr>" &
                "<tr>" &
                    "<th colspan=""2"">Net Amount</th>" &
                    "<th>6</th>" &
                "</tr>" &
            "</table>" &
            "<p><hr/></p>" &
            "<p>Thank You for shopping</p>" &
            "<p>XYZ StoreName</p>" &
            "<p>We hope you’ll come back soon!</p>" &
        "</body>" &
    "</html>"

Me.WebBrowser1.DocumentText = html

You will need a handler for document complete (or a separate print button, but the point is ShowPrintDialog() can't be called before the document is complete).

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Me.WebBrowser1.ShowPrintDialog()
End Sub

The above code produces this rudimentary, albeit formatted, receipt.

enter image description here

djv
  • 15,168
  • 7
  • 48
  • 72
  • Thanks for your answer. Let me give a try and let u know. Meanwhile will it support custom paper length? it may contain 10 line or may contain 20 line. Every thing should be in the one invoice. – Vignesh Kumar A May 12 '17 at 07:40
  • Of course, within reason. You could reduce the size of the table and the font when there are many lines. It would take a couple of trials to find the right size. If you had 100 items, of course you may not want to fit it in one page. So some logic should be written into the program. – djv May 12 '17 at 14:52
  • I don't think this will go well, the problem lies in how windows handle the page. to do this you should carefully set up the driver to not advance to the next page when finished printing. –  May 12 '17 at 15:42
  • It's in how the printer driver handles it. Some printers can fit to one page, some cannot. – djv May 12 '17 at 15:56
0

In dot printers, who can print all the lines (no mater how many) and at the end, send the ascii code to cut the paper using raw printing.

You can send FF (Form Feed, to eject paper from the rear of the printer), but not ESC O (to eject paper from the front of the printer), ESC 0 / ESC 1 (to initialize the printer / reset errors) or even an bell/buzzer: escape codes is BEL

The codes change from brand to brand and some times in the printer model. Also you can have several modes or Page description languages (PDL). HP also call it Printer Command Language (PCL). The most common is Epson PDL.

An example in C# , documentation for win32 api

Community
  • 1
  • 1
Pedro Polonia
  • 2,604
  • 3
  • 23
  • 31