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.
精彩评论