在C#中實現雙面打印可以通過使用PrintDocument類來實現。下面是一個簡單的示例代碼:
using System;
using System.Drawing;
using System.Drawing.Printing;
class Program
{
static void Main()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintPage);
pd.Print();
}
private static void PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Font font = new Font("Arial", 12);
string text = "This is a sample text for double sided printing.";
// Print on front side
g.DrawString(text, font, Brushes.Black, new PointF(100, 100));
// Print on back side
e.HasMorePages = true;
g.DrawString(text, font, Brushes.Black, new PointF(100, 100));
}
}
在上面的代碼中,我們創建了一個PrintDocument對象并為其添加了一個PrintPage事件處理程序。在PrintPage事件處理程序中,我們使用Graphics對象來繪制文本,并通過設置e.HasMorePages屬性為true來指示打印機需要打印另一面。通過這種方式,可以實現雙面打印。