0

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

John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71

2 Answers2

1

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.

Bmo
  • 1,212
  • 11
  • 34
  • what is the "bol.Add(mainHead)"? Is that a class you have made yourself too? – John Ernest Guadalupe Oct 31 '12 at 12:35
  • Sorry. bol is the name of the list that holds all the print objects. So when the print event fires. It loops through all the printObjects in that list. Prints them to the page and then sends it to the printer. – Bmo Oct 31 '12 at 12:37
  • Okay.. I kind of am understanding this a little. What are the BaseX and BaseYs for? – John Ernest Guadalupe Oct 31 '12 at 12:46
  • The XY referrers to the upper left point of the drawing object. If you think of a bounding box around a sentence or a picture it's on an XY coordinate in pixels on the paper. If I want to move it down I increase Y or move right I increase X. Thats where the text will show up. – Bmo Oct 31 '12 at 14:50
  • Any ideas How I can insert a logo here? and horizontal lines? – John Ernest Guadalupe Oct 31 '12 at 15:39
  • Your best bet will be to Google the documentation on methods for the Graphics class. There is .DrawLine(args), .DrawImage(args) ect. Also when you are in the e.Graphics namespace just hit the dot and browse the various methods there. – Bmo Oct 31 '12 at 16:14
1

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);
MaciejLisCK
  • 3,706
  • 5
  • 32
  • 39
  • So what will actually happen here? Will the text I plan to print be printed on the doc? I have included the actual text that will be included in the invoice above – John Ernest Guadalupe Oct 31 '12 at 15:37
  • Basically you create Word document. Inside document you create placeholders for text. And from code you fill placeholders with whatever you want. – MaciejLisCK Oct 31 '12 at 15:49
  • Oh! Okay I get it thank you very much! So I would just address the placeholders in the Word document in the code so that the information would be placed wherever the place holder is right? Is there a set place holder? Or do I just scan the whole doc for a placeholder I create then .Replace it? – John Ernest Guadalupe Oct 31 '12 at 15:51
  • What I mean by "set" is a "pre-defined" code to address placeholders – John Ernest Guadalupe Oct 31 '12 at 15:52
  • The whole code for filling placeholder is on answer. In example code I show how to create placeholder called `FirstName`. And then I make sql query where one column will be called `FirstName`. That's how it recognize where to place text. Of course you can create multiple place holders and place text where you want. – MaciejLisCK Oct 31 '12 at 15:57
  • `So I would just address the placeholders in the Word document in the code so that the information would be placed wherever the place holder is right?` Yes – MaciejLisCK Oct 31 '12 at 15:58