// results of using the Split function on the text
String[] data;
// a line of text from our file
string text = "";
// print the header once per page
centerText(ev.Graphics, headerText, _headerFont, _defaultBrush, ReportheaderR);
// how many lines can we fit on a page?
float bodyFontHeight = footerHeight;
int linesPerPage = Convert.ToInt32(bodyR.Height / bodyFontHeight) - 1;
// the header = 2 lines
int currentLine = 2;
// currentY is what we'll use for the Top (Y) coordinate that
// DrawString needs.
float currentY = 0;
// Print each line of the file. This could be easily modified to
// deal with data from a DataTable
while(currentLine + 1 < linesPerPage && ((text=_Data.ReadLine()) != null))
{
data = text.Split(',');
// retrieve the current top position
currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
// DrawString will take a Rectangle argument, but NOT a RectangleF arg,
// so we need to provide the X and Y arguments.
ev.Graphics.DrawString(data[0], _bodyFont, _defaultBrush, bodyR.Left, currentY);
currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
ev.Graphics.DrawString(data[1], _bodyFont, _defaultBrush, bodyR.Left + 25, currentY);
currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
ev.Graphics.DrawString("Phone: " + data[2], _bodyFont, _defaultBrush, bodyR.Left + 25, currentY);
currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
ev.Graphics.DrawString("Fax: " + data[3], _bodyFont,_defaultBrush, bodyR.Left + 25, currentY);
//currentY = getCurrentY(currentLine++, topMargin, bodyFontHeight);
//ev.Graphics.DrawLine(_defaultPen, leftMargin, currentY, pageWidth, currentY);
currentLine++;
}
// the StringFormat class has a lot of cool features
private void centerText(Graphics g, string t, Font f, Brush b, RectangleF rect)
{
StringFormat sf = new StringFormat();
// center horizontally
sf.Alignment = StringAlignment.Center;
// center vertically
sf.LineAlignment = StringAlignment.Center;
g.DrawString(t, f, b, rect, sf);
}
// used for debugging
private void dumpRectF(RectangleF rect)
{
System.Diagnostics.Debug.WriteLine(rect.ToString());
}
private void _PDoc_BeginPrint(object sender, PrintEventArgs pv)
{
// make sure we reset this before printing
_currentPage = 0;
// the file should be in the same folder as the EXE
_Data = new StreamReader("contacts.csv");
_headerFont = new Font("Arial", 24, FontStyle.Underline, GraphicsUnit.World);
_bodyFont = new Font("Arial", 12, GraphicsUnit.World);
}
private void PrintForm_Load(object sender, System.EventArgs e)
{
_PDoc = new PrintDocument();
// this is what will show up in the print manager
_PDoc.DocumentName = "printTest -- Northwind Contact List";
// make sure at least one printer is installed
if (_PDoc.PrinterSettings.PrinterName == "<no default printer>")
{
StatusLabel.Text = "At least one (1) printer must be installed to continue.";
PrintButton.Enabled = false;
PreviewButton.Enabled = false;
}
else
{
StatusLabel.Text = "You have " + PrinterSettings.InstalledPrinters.Count + " printer(s) installed.";
// bind the events
_PDoc.PrintPage += new PrintPageEventHandler(_PDoc_PrintPage);
_PDoc.BeginPrint += new PrintEventHandler(_PDoc_BeginPrint);
_PDoc.EndPrint += new PrintEventHandler(_PDoc_EndPrint);
_PDoc.QueryPageSettings += new QueryPageSettingsEventHandler(_PDoc_QueryPageSettings);
}
}
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.ComponentModel;
using System.Windows.Forms;
namespace printTest
{
public class PrintForm : System.Windows.Forms.Form
{
// private variables
private PrintDocument _PDoc;
private StreamReader _Data;
private int _currentPage = 0;
// these will be created in BeginPrint and
// destroyed in EndPrint.
private Font _headerFont;
private Font _bodyFont;
private Brush _defaultBrush = Brushes.Black;
private Pen _defaultPen = new Pen(Brushes.Black, .25f);
// used to define the sections of the document
float headerHeight = _headerFont.GetHeight(ev.Graphics);
float footerHeight = _bodyFont.GetHeight(ev.Graphics);
// report header
RectangleF ReportheaderR = new RectangleF(leftMargin, topMargin, pageWidth, headerHeight);
// report body
RectangleF bodyR = new RectangleF(leftMargin, ReportheaderR.Bottom, pageWidth, pageHeight - ReportheaderR.Height - footerHeight);
// report footer
RectangleF ReportfooterR = new RectangleF(leftMargin, bodyR.Bottom, pageWidth, footerHeight * 2);
using System;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace printTest {
// this is how you would create your own PrintDocument class that
// inherits from PrintDocument. You need to provide implementation
// code for the events you plan to use (at least OnPrintPage).
public class CustomPrintDocument : PrintDocument {
private StreamReader dataToPrint;
public CustomPrintDocument(StreamReader data) : base() {
dataToPrint = data;
}
const short HORZSIZE = 4; /* Horizontal size in millimeters */
const short VERTSIZE = 6; /* Vertical size in millimeters */
const short HORZRES = 8; /* Horizontal width in pixels */
const short VERTRES = 10; /* Vertical height in pixels*/
const short PHYSICALOFFSETX = 112; /* Physical Printable Area x margin */
const short PHYSICALOFFSETY = 113; /* Physical Printable Area y margin */
// Modified from code originally written by Ron Allen (Ron@us-src.com).
public marginInfo(int deviceHandle) {
// Non printable margin in pixels
float offsetX = Convert.ToSingle(GetDeviceCaps(deviceHandle, PHYSICALOFFSETX));
float offsetY = Convert.ToSingle(GetDeviceCaps(deviceHandle, PHYSICALOFFSETY));
// printable page size in pixels
float resolutionX = Convert.ToSingle(GetDeviceCaps(deviceHandle, HORZRES));
float resolutionY = Convert.ToSingle(GetDeviceCaps(deviceHandle, VERTRES));
// printable page size in current unit (in this case, converted to inches)
float horizontalSize = Convert.ToSingle(GetDeviceCaps(deviceHandle, HORZSIZE))/25.4f;
float verticalSize = Convert.ToSingle(GetDeviceCaps(deviceHandle, VERTSIZE))/25.4f;