Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing using Template

Tags:

c#

.net

printing

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?

like image 617
Philippe Avatar asked Jul 22 '13 13:07

Philippe


People also ask

What is a template in printing?

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.


4 Answers

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();
    }
}
like image 156
Naomi Owens Avatar answered Oct 16 '22 05:10

Naomi Owens


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.

like image 33
Matt Cruikshank Avatar answered Oct 16 '22 03:10

Matt Cruikshank


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...

like image 45
ChrisBint Avatar answered Oct 16 '22 05:10

ChrisBint


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?

like image 42
shiva kumar Avatar answered Oct 16 '22 04:10

shiva kumar