根據該篇文章 -
作法:使用預覽列印在 Windows Forms 中進行列印 的練習,其實 PrintPreviewDialog 的使用還蠻簡易,練習重點在於
- MeasureString(),之前閱讀時沒有很清楚 charactersOnPage 和 linesPerPage 參數,該範例算是較實務的使用情境
- 該段 Code,直接看沒有很清楚為何還要重置 stringToPrint 變數,因為實務上有類似應用,想了解看看
if (e.HasMorePages == false)
stringToPrint = documentContents;
完整 C# Code
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
namespace PrintPreviewSample
{
public partial class Form1 : Form
{
private PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
private PrintDocument printDocument = new PrintDocument();
public Form1()
{
InitializeComponent();
printDocument.PrintPage += printDocument_PrintPage;
}
// 存儲文字檔內容
private string documentContents { get; set; }
// 待列印內容
private string stringToPrint { get; set; }
private void ReadDocument()
{
string docPath = @"D:\";
string docName = "testPage.txt";
using (FileStream stream = new FileStream(Path.Combine(docPath, docName), FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
documentContents = reader.ReadToEnd();
stringToPrint = documentContents;
}
printDocument.DocumentName = docName;
}
private void btnPrintPreview_Click(object sender, EventArgs e)
{
ReadDocument();
// PrintPreviewDialog 標準使用方式
printPreviewDialog.Document = printDocument;
printPreviewDialog.ShowDialog();
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// 測量文字範圍,並回傳範圍內有多少文字和行數
e.Graphics.MeasureString(
stringToPrint,
this.Font,
e.MarginBounds.Size,
StringFormat.GenericTypographic,
out int charactersOnPage, out int linesPerPage);
// 在範圍內繪製文字
e.Graphics.DrawString(
stringToPrint,
this.Font,
Brushes.Black,
e.MarginBounds,
StringFormat.GenericTypographic);
// 移除已經列印的文字
stringToPrint = stringToPrint.Substring(charactersOnPage);
// 確定是否還有下一頁需要列印
e.HasMorePages = (stringToPrint.Length > 0);
// 沒有下一頁可以進行列印的話,就重置 stringToPrint
// 避免多次使用 Print 功能時,stringToPrint 沒有資料存在
if (e.HasMorePages == false)
stringToPrint = documentContents;
}
}
}
沒有留言:
張貼留言