// PrintPage is the foundational printing event. This event gets fired for every
// page that will be printed. You could also handle the BeginPrint and EndPrint
// events for more control.
//
// The following is very
// fast and useful for plain text Measurestring calculates the text that
// can be fitted on an entire page. This is ! that useful, however, for
// formatted text. In that case you would want to have word-level (vs page-level)
// control, which is more complicated. // Declare a variable to hold the position of the last printed char. Declare
// static so that subsequent PrintPage events can reference it. Static variables
// are not supported inside a function in C#
static int intCurrentChar; private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Initialize the font to be used for printing. Font font = new Font("Microsoft Sans Serif", 24);
int intPrintAreaHeight;
int intPrintAreaWidth;
int marginLeft;
int marginTop; // Initialize local variables that contain the bounds of the printing
// area rectangle.
intPrintAreaHeight = pdoc.DefaultPageSettings.PaperSize.Height - pdoc.DefaultPageSettings.Margins.Top - pdoc.DefaultPageSettings.Margins.Bottom;
intPrintAreaWidth = pdoc.DefaultPageSettings.PaperSize.Width - pdoc.DefaultPageSettings.Margins.Left - pdoc.DefaultPageSettings.Margins.Right;
// Initialize local variables to hold margin values that will serve
// the X and Y coordinates for the upper left corner of the printing
// area rectangle.
marginLeft = pdoc.DefaultPageSettings.Margins.Left; // X coordinate
marginTop = pdoc.DefaultPageSettings.Margins.Top; // Y coordinate // if the user selected Landscape mode, swap the printing area height
// and width. if (pdoc.DefaultPageSettings.Landscape)
{
int intTemp = intPrintAreaHeight;
intPrintAreaHeight = intPrintAreaWidth;
intPrintAreaWidth = intTemp;
}
// Calculate the total number of lines in the document based on the height of
// the printing area and the height of the font. int intLineCount= (int)(intPrintAreaHeight / font.Height); // Initialize the rectangle structure that defines the printing area. RectangleF rectPrintingArea = new RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight); // Instantiate the stringFormat class, which encapsulates text layout
// information (such alignment and line spacing), display manipulations
// (such ellipsis insertion and national digit substitution) and OpenType
// features. Use of stringFormat causes Measurestring and Drawstring to use
// only an integer number of lines when printing each page, ignoring partial
// lines that would otherwise likely be printed if the number of lines per
// page do not divide up cleanly for each page (which is usually the case).
// See further discussion in the SDK documentation about stringFormatFlags. StringFormat fmt = new StringFormat(StringFormatFlags.LineLimit); // Call Measurestring to determine the number of characters that will fit in
// the printing area rectangle. The CharFitted Int32 is passed ref and used
// later when calculating intCurrentChar and thus HasMorePages. LinesFilled
// is ! needed for this sample but must be passed when passing CharsFitted.
// Mid is used to pass the segment of remaining text left off from the
// previous page of printing (recall that intCurrentChar was declared
// static. int intLinesFilled;
int intCharsFitted; e.Graphics.MeasureString(txtDocument.Text.Substring(intCurrentChar), font,new SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt,out intCharsFitted, out intLinesFilled); // Print the text to the page.
e.Graphics.DrawString(txtDocument.Text.Substring(intCurrentChar), font,Brushes.Black, rectPrintingArea, fmt); // Advance the current char to the last char printed on this page.
// intCurrentChar is a static variable, its value can be used for the next
// page to be printed. It is advanced by 1 and passed to Mid() to print the
// next page (see above in Measurestring()). intCurrentChar += intCharsFitted; // HasMorePages tells the printing module whether another PrintPage event
// should be fired. if (intCurrentChar < (txtDocument.Text.Length-1))
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
// You must explicitly reset intCurrentChar it is static.
intCurrentChar = 0;
}
}
// page that will be printed. You could also handle the BeginPrint and EndPrint
// events for more control.
//
// The following is very
// fast and useful for plain text Measurestring calculates the text that
// can be fitted on an entire page. This is ! that useful, however, for
// formatted text. In that case you would want to have word-level (vs page-level)
// control, which is more complicated. // Declare a variable to hold the position of the last printed char. Declare
// static so that subsequent PrintPage events can reference it. Static variables
// are not supported inside a function in C#
static int intCurrentChar; private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Initialize the font to be used for printing. Font font = new Font("Microsoft Sans Serif", 24);
int intPrintAreaHeight;
int intPrintAreaWidth;
int marginLeft;
int marginTop; // Initialize local variables that contain the bounds of the printing
// area rectangle.
intPrintAreaHeight = pdoc.DefaultPageSettings.PaperSize.Height - pdoc.DefaultPageSettings.Margins.Top - pdoc.DefaultPageSettings.Margins.Bottom;
intPrintAreaWidth = pdoc.DefaultPageSettings.PaperSize.Width - pdoc.DefaultPageSettings.Margins.Left - pdoc.DefaultPageSettings.Margins.Right;
// Initialize local variables to hold margin values that will serve
// the X and Y coordinates for the upper left corner of the printing
// area rectangle.
marginLeft = pdoc.DefaultPageSettings.Margins.Left; // X coordinate
marginTop = pdoc.DefaultPageSettings.Margins.Top; // Y coordinate // if the user selected Landscape mode, swap the printing area height
// and width. if (pdoc.DefaultPageSettings.Landscape)
{
int intTemp = intPrintAreaHeight;
intPrintAreaHeight = intPrintAreaWidth;
intPrintAreaWidth = intTemp;
}
// Calculate the total number of lines in the document based on the height of
// the printing area and the height of the font. int intLineCount= (int)(intPrintAreaHeight / font.Height); // Initialize the rectangle structure that defines the printing area. RectangleF rectPrintingArea = new RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight); // Instantiate the stringFormat class, which encapsulates text layout
// information (such alignment and line spacing), display manipulations
// (such ellipsis insertion and national digit substitution) and OpenType
// features. Use of stringFormat causes Measurestring and Drawstring to use
// only an integer number of lines when printing each page, ignoring partial
// lines that would otherwise likely be printed if the number of lines per
// page do not divide up cleanly for each page (which is usually the case).
// See further discussion in the SDK documentation about stringFormatFlags. StringFormat fmt = new StringFormat(StringFormatFlags.LineLimit); // Call Measurestring to determine the number of characters that will fit in
// the printing area rectangle. The CharFitted Int32 is passed ref and used
// later when calculating intCurrentChar and thus HasMorePages. LinesFilled
// is ! needed for this sample but must be passed when passing CharsFitted.
// Mid is used to pass the segment of remaining text left off from the
// previous page of printing (recall that intCurrentChar was declared
// static. int intLinesFilled;
int intCharsFitted; e.Graphics.MeasureString(txtDocument.Text.Substring(intCurrentChar), font,new SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt,out intCharsFitted, out intLinesFilled); // Print the text to the page.
e.Graphics.DrawString(txtDocument.Text.Substring(intCurrentChar), font,Brushes.Black, rectPrintingArea, fmt); // Advance the current char to the last char printed on this page.
// intCurrentChar is a static variable, its value can be used for the next
// page to be printed. It is advanced by 1 and passed to Mid() to print the
// next page (see above in Measurestring()). intCurrentChar += intCharsFitted; // HasMorePages tells the printing module whether another PrintPage event
// should be fired. if (intCurrentChar < (txtDocument.Text.Length-1))
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
// You must explicitly reset intCurrentChar it is static.
intCurrentChar = 0;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货