开发者

Print array of papers with minors changes in each in 1 click

I am supposed to print an array of invoices to our clients. There is a template invoice. I have to change the name, address and amount on it for each client and print them.

I've searched for lot of time on the net but there's no solution that talks about this kind of thing.

The template is pretty simple: To,

$customer_name,

$cutomer_address

You have been billed for $bill_amount for this month.

Thank you, CAT Team.

Below is the code that prints an array files in 1 c开发者_开发知识库lick

private void btnPrintInvoiceLetters_Click(object sender, EventArgs e)
        {
            //Create temp working directory and in it the files to be printed
            string tempDir = SetupFiles();

            string[] filePaths = Directory.GetFiles(tempDir, "*.txt", SearchOption.TopDirectoryOnly);

            foreach (string path in filePaths)
            {
                reader = new StreamReader(path);

                //Create a Verdana font with size 10
                verdana10Font = new Font("Verdana", 10);
                //Create a PrintDocument object
                PrintDocument pd = new PrintDocument();
                //Add PrintPage event handler
                pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
                //Call Print Method
                pd.DefaultPageSettings.PaperSize = new PaperSize("PaperA5", 582, 826);
                pd.PrinterSettings.DefaultPageSettings.Color = false;
                pd.Print();
                //Close the reader
                if (reader != null)
                {
                    reader.Close();
                    File.Delete(path);
                }
            }

            Directory.Delete(tempDir);
        }

But the catch here is I am creating temp files before they are printed. Is there a better approach?

Any help is greatly appreciated.


If the Invoice Template is a Word Document - You can use String Replacement and loop through to print the exact invoices.

For example : In the template you can set ##NAME##, ##ADDRESS##, ##AMOUNT## etc... and then in your loop replace them with actual values. Once the document is ready you can call the Print command on it using the PrintDocument class.


I guess the code I posted is a feasible way to do my task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜