I'm working on a few tools that require printed output for the user, and I have had some good results with the System.Drawing and System.Graphics classes to layout the document to be printed, but I'm needing more page layout features.
The code I've used and had good results with so far is here:
//create the Print Dialog
PrintDialog pd = new PrintDialog();
//Create Document to Print
PrintDocument doc = new PrintDocument();
//Add document to dialog to print
pd.Document = doc;
//Adds the contents of the page
doc.PrintPage += new PrintPageEventHandler(pd_printpage);
//Launch the Print Dialog
DialogResult res = pd.ShowDialog();
//if everything is okay with the dialog, Print it.
if(res == DialogResult.OK)
{
doc.DefaultPageSettings.Landscape = false;
doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
doc.Print();
}
with the actual printing here
//Generate a Graphics object
Graphics gfx = e.Graphics;
//Set the Font to print with
Font courier = new Font("Courier New", 12);
//check to see if data is not set to default.
if (check_text())
{
//if the manual radio is selected:
if (rd_ManRcpt.Checked == true)
{
//Place Amount on page.
gfx.DrawString(tx_amt.Text, courier, new SolidBrush(Color.Black), 55, 80);
//Place Date on page
gfx.DrawString(tx_date.Text, courier, new SolidBrush(Color.Black), 35, 105);
//Place Serial number to page
gfx.DrawString(tx_No.Text, courier, new SolidBrush(Color.Black), 250, 105);
//place contributer name to page.
gfx.DrawString(tx_CredTo.Text, courier, new SolidBrush(Color.Black), 75, 130);
//Place Address Information on Page
gfx.DrawString(tx_add1.Text + "/n" + tx_add2.Text + "/n" + tx_city.Text + ", " + tx_state.Text + " " + tx_zip.Text, courier, new SolidBrush(Color.Black), 75, 200);
}
else if (rd_BDRcpt.Checked == true)
{
gfx.DrawString("$40.75", courier, new SolidBrush(Color.Black), 515, 10);
gfx.DrawString("03/13/2017", courier, new SolidBrush(Color.Black), 625, 105);
gfx.DrawString("XXXXXXX", courier, new SolidBrush(Color.Black), 625, 165);
gfx.DrawString("XXXXXX", courier, new SolidBrush(Color.Black), 625, 215);
gfx.DrawString("Contributer Here", courier, new SolidBrush(Color.Black), 75, 175);
gfx.DrawString("Address Information Here.", courier, new SolidBrush(Color.Black), 75, 195);
}
The code here is just to generate one of two receipts and print them. I'm not sure how to define the page size and orientation where the printer will print on the size 10 envelope page.
The other part of this question is how to fully control the layout of the document being printed or if possible, use HTML to control the layout as I'm very familiar and I'm sure others would be interested in this technique as well.