I've generated a single long string of invoices formatted as required. I now need to print them out on a printer with one invoice displayed per page.
I've looked at these tutorials/help as well as some code I was:
https://msdn.microsoft.com/en-us/library/cwbe712d%28v=vs.110%29.aspx
I've primarily followed the second one.
What I've ended up with is (Working VS C# project with a single form with a single button):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestPrint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string stringToPrint = "Hello\r\nWorld\r\n\r\n<<< Page >>>World\r\nHello";
private void button1_Click(object sender, EventArgs e)
{
// Convert string to strings
string[] seperatingChars = { "<<< Page >>>" };
string[] printString = stringToPrint.Split(seperatingChars, System.StringSplitOptions.RemoveEmptyEntries);
// Connect to the printer
PrintDocument printDocument1 = new PrintDocument(); // Stream to the printer
// Send to printer (reference: https://social.msdn.microsoft.com/Forums/en-US/93e54c4f-fd07-4b60-9922-102439292f52/c-printing-a-string-to-printer?forum=csharplanguage)
foreach (string s in printString)
{
printDocument1.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black),
new RectangleF(0, 0, printDocument1.DefaultPageSettings.PrintableArea.Width,
printDocument1.DefaultPageSettings.PrintableArea.Height));
};
try
{
printDocument1.Print();
printDocument1.Dispose();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
}
}
}
}
I break the long string into it's parts that I want printed to each individual page and then sent that to the printer. This works fine for the first invoice/page but after that it just adds each page on to image of the first (I added the printDocument1.Dispose();
to try and sort that but that didn't work).
What I want to know is how can I print the string as a single string while keeping one invoice per page.
EDIT: How do I generate the string as multi-page image for the printer?