7

For a simple reciept system I need to somehow define a template document with simple formatting, fill it with data and print it on a standard windows printer. It has to work on a windows service. What technology should I best use?

EDIT:

I tried using PDF-Forms. I defined a couple of text boxes and filled them in with iTextSharp. It worked until the point where I had to print them, which is really hard, as you have to essentially use the reader executable directly.

An alternative which seems to be better integrated into .NET seems to be to use XPS. Does XPS provide a similar functionality?

Philippe
  • 1,715
  • 4
  • 25
  • 49
  • 3
    Printing from a service is a Really Bad Idea. Printer drivers were made for desktop apps. They'll tell you that the toner cartridge is half-empty and where to order another one. If that happens in a service then it just won't print and you have no idea why. A template can be anything, a bitmap will do. Lots and lots of report generator programs with fill-in-the-blank features out there, you'll need to go shopping for them by yourself. – Hans Passant Jul 29 '13 at 13:41
  • I write printer drivers for a living, and I have to agree with Hans Passant. Printing from a windows service is almost never a good idea. – Jon Jul 29 '13 at 16:00

6 Answers6

5

Create your own receipt template using html or plain text.

Example using html:

HTML

<html>
  <head>
    <title>Receipt</title>
  </head>
  <body>
    <div>The price: <%Price%></div>
    <div>The time: <%Time%></div>
    <div>Payment Method: <%PaymentMethod%></div>
  </body>
</html>

C#

    static void Main(string[] args)
    {
        CreateReceipt("€1.50", "09.30", "Cash");
    }

    private static void CreateReceipt(string price, string time, string paymentMethod)
    {
        string bodyFile;
        string template = System.IO.Directory.GetCurrentDirectory() + "\\template.html";
        using (StreamReader reader = new StreamReader(template))
        {
            bodyFile = reader.ReadToEnd();
            bodyFile = bodyFile.Replace("<%Price%>", price);
            bodyFile = bodyFile.Replace("<%Time%>", time);
            bodyFile = bodyFile.Replace("<%PaymentMethod%>", paymentMethod);
        }
        FileStream fs = File.OpenWrite(System.IO.Directory.GetCurrentDirectory() + "\\receipt.html");
        StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
        writer.Write(bodyFile);
        writer.Close();
    }
}
Naomi Owens
  • 195
  • 2
  • 11
  • Well I don't really want to write that stuff myself. Also how would I print this afterwards? Is there a way to use something like Razor and then print it out? – Philippe Jul 23 '13 at 05:38
  • See edit. The method will now use the template.html to create a new file called receipt.html with whatever parameters are passed to it. I'm not familiar with Razor. – Naomi Owens Jul 23 '13 at 08:39
4

Use Mustache to create HTML templates and populate them.

The .Net interfaces are very easy to use.

Once you've got the HTML, you can use a WebBrowser control - offscreen - to print.

Community
  • 1
  • 1
Matt Cruikshank
  • 2,932
  • 21
  • 24
1

There are various ways;

Create a text file and search and replace keywords with appropriate values. Save/Print this.

Create a html file and search and replace keywords with appropriate values. Save/Print this.

Create a PDF file with built-in fields and replace these with appropriate values. Save/Print this.

And more...

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
1

Just for an Idea if you haven't seen this, You can print HTML DIV

How do I print part of a rendered HTML page in JavaScript?

Community
  • 1
  • 1
shiva kumar
  • 74
  • 1
  • 5
1

You could try ActiveReports

Link to ComponentOne

It's not for free.. but it works for printing stuff as you wish. Just create Templates and embedd them in your code.

Munchies
  • 444
  • 6
  • 14
-1

We endet up using the format of the EPSON TM-Intelligent series.

Philippe
  • 1,715
  • 4
  • 25
  • 49