Question

I have an application in C# that would print invoices and payslips. The client have sent me a template which would be used for the day to day operations. I don't know how to print to it, though I already know how to print a programmatically made text file which contains the information from an access database.

How do I print the information on this kind of template? (This is only something I [found on Google][1] and is a good candidate for a simple invoice printing) The document template I have also have a LOGO..

Was it helpful?

Solution

Do it by mail merge in Word. Using this technique you create Word document. Inside document you create placeholders for text. And from code you fill placeholders with whatever you want.

For example:

  1. In word document type ctrl + F9
  2. Right click on field and Edit field
  3. Choose MergeField
  4. On field name type FirstName
  5. Add code:

.

var document = new Document("document.docx");
var sqlCommand = "SELECT TOP 1 userName FirstName FROM Users";
var table = GetTable(sqlCommand, String.Empty);
document.MailMerge.Execute(table);

OTHER TIPS

I've been using the PrintDocument and PrintPreview objects. That use the the Graphics class. When print is called you get an "PrintEventArgs e" object. Then you can use e.Graphics to have access to things like e.Graphics.DrawString, .DrawImage etc.

I built a whole print objects class that overrides print. So I have a detail box that has different fonts, or a logo, a header, leagal jargon etc. Each one of these has it's own class. I put them all in a big list and I call printThis(List); and it will take each print function and the coordinates and make me a form.

Inherited object

class formHdr : printObject
{
    private string headerText;

    public formHdr(string hText)
        : base()
    {
        headerText = hText;

    }

    public override void printThis(System.Drawing.Printing.PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawString(headerText, FRHEADER, Brushes.Black, BaseX, BaseY);

    }
}

Base class

abstract class printObject
{
    protected Font FTHEADER;
    protected Font NRML;
    protected Font DETAIL;
    protected Font FRHEADER;
    protected Font DETHEADER;
    protected Font LEGAL;
    protected Font LEGAL2;

    public int baseX, baseY;
    public int boxSX, boxSY;

    public printObject()
    {
        baseX = 0;
        baseY = 0;
        boxSX = 0;
        boxSY = 0;
        FTHEADER = new Font("Arial", 12, FontStyle.Bold);
        NRML = new Font("Calibri", 10);
        DETAIL = new Font("Consolas", 8);
        FRHEADER = new Font("Arial", 16, FontStyle.Bold);
        DETHEADER = new Font("Calibri", 10, FontStyle.Bold);
        LEGAL = new Font("Arial", 8);
        LEGAL2 = new Font("Arial", 10);
    }

    public virtual void printThis(PrintPageEventArgs e) { }

Object creation

                mainHead = new formHdr("Bill of Lading/Weigh slip Original");
                mainHead.BaseX = 225;
                mainHead.BaseY = 20;
                bol.Add(mainHead);

Maybe this can get you started? I'm still tweaking it and will be interested in other responses.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top