Search code examples
c#printingdocuments

How to print information programmatically on a doc template?


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


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);