VB6 中列印動態資料極簡單,Printer 物件支援自動分頁:
rs.MoveFirst
Printer.PaperSize = vbPRPSA4
Printer.Orientation = vbPRORPortrait
Printer.FontName = "Courier New"
Printer.FontBold = True
Printer.FontSize = 24
Printer.Print " FQA Report"
' ... 迴圈列印記錄 ...
' 超過一頁自動分頁,無需額外設定
MSDN 指出:VB6 Printer 物件在 .NET 被 PrintDocument 取代,行為不同但可複製大部分功能。然而查閱大量資料、教科書範例(如《C# 高級程式設計》第 25 章)皆需先載入檔案、計算總行數、設定屬性……極其複雜,無法直接套用,只能自行改寫。
public bool PrintDoc() {
try {
// 取得列印資料
string strSQL = "exec [usp_PalletPrintDocInfo] '" + Parameters.strStation + "', '" + Parameters.strPalletID + "'";
dsPrintInfo = doDB.GetDataSet(strSQL);
if (dsPrintInfo.Tables[0].Rows.Count < 1 || dsPrintInfo.Tables[1].Rows.Count < 1) {
Parameters.strMsg = "Get Print Information Error"; return false;
}
// 列印
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
printDocument.Print();
// 列印預覽(除錯用,省紙)
// PrintPreviewDialog ppd = new PrintPreviewDialog();
// PrintDocument pd = new PrintDocument();
// pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
// ppd.Document = pd; ppd.ShowDialog();
return true;
} catch (Exception ex) {
Parameters.strMsg = ex.Message.ToString(); return false;
}
}