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?
A document used in electronic or paper media that has a pre-determined page layout and style, which can be edited to produce the required finished document. A template will indicate where specific page elements are to be inserted and may include standard text or images as required.
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();
}
}
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.
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...
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With