給大家共享個CsharpWinform列印模組換行換頁的解決方案!大家可以參考。
阿新 • • 發佈:2019-01-27
原先用的是字串內的迴圈遍歷,後來改模式後效果真心不錯。
public partial class 換頁列印 : Form
{
public 換頁列印()
{
InitializeComponent();
}
string s = @"N多的列印內容";
StringFormat stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces, 0);
int count, rows;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
SizeF sf = e.Graphics.MeasureString(s, this.Font, e.MarginBounds.Size, stringFormat, out count, out rows);
MessageBox.Show("總長度:" + s.Length.ToString() + "#當頁長度:" + count.ToString() + "#行數:" + rows.ToString());
e.Graphics.DrawString(s.Substring(0, count), this.Font, new SolidBrush(Color.Black), e.MarginBounds, stringFormat);
s = s.Remove(0, count < s.Length ? count : s.Length);
if (s.Length > 0)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
private void Form1_Load(object sender, EventArgs e)
{
printPreviewDialog1.Show();
}
private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e)
{
}
}
public partial class 換頁列印 : Form
{
public 換頁列印()
{
InitializeComponent();
}
string s = @"N多的列印內容";
StringFormat stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces, 0);
int count, rows;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
SizeF sf = e.Graphics.MeasureString(s, this.Font, e.MarginBounds.Size, stringFormat, out count, out rows);
MessageBox.Show("總長度:" + s.Length.ToString() + "#當頁長度:" + count.ToString() + "#行數:" + rows.ToString());
e.Graphics.DrawString(s.Substring(0, count), this.Font, new SolidBrush(Color.Black), e.MarginBounds, stringFormat);
s = s.Remove(0, count < s.Length ? count : s.Length);
if (s.Length > 0)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
private void Form1_Load(object sender, EventArgs e)
{
printPreviewDialog1.Show();
}
private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e)
{
}
}