C# PrintDocument 文字旋轉列印 範例
C# PrintDocument 文字旋轉列印 範例
資料來源: chatgpt
code
using System; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; namespace PrintRotationExample { public partial class Form1 : Form { private PrintDocument printDocument; public Form1() { InitializeComponent(); // 建立 PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += PrintPageHandler; // 建立按鈕觸發預覽 Button printButton = new Button { Text = "列印預覽", Location = new Point(10, 10), AutoSize = true }; printButton.Click += (s, e) => { PrintPreviewDialog preview = new PrintPreviewDialog { Document = printDocument, Width = 800, Height = 600 }; preview.ShowDialog(); }; Controls.Add(printButton); } private void PrintPageHandler(object sender, PrintPageEventArgs e) { Graphics g = e.Graphics; Font font = new Font("Arial", 16); // === 第一行:正常列印 === string text1 = "這是正常方向的文字(第1行)"; g.DrawString(text1, font, Brushes.Black, 100, 100); // === 第二行:旋轉90度列印 === string text2 = "這是旋轉90度的文字(第2行)"; // 將原點移到 (300, 200) g.TranslateTransform(300, 200); // 旋轉90度 g.RotateTransform(90); // 在旋轉後的座標系列印 g.DrawString(text2, font, Brushes.Blue, 0, 0); // 還原變換 g.ResetTransform(); // === 第三行:正常方向列印 === string text3 = "這是恢復正常方向的文字(第3行)"; g.DrawString(text3, font, Brushes.DarkGreen, 100, 300); } } }