Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การพิมพ์ด้วย Font ตัวเองใน C# (Custom Font Printing)

ใน WinForms/WPF ใช้ System.Drawing (GDI+) หรือ System.Windows.Media สำหรับ Custom Font Printing

WinForms (System.Drawing)

สร้าง Font

using System.Drawing;
using System.Drawing.Printing;

// FontFamily: ชื่อ Font (ต้องติดตั้งในระบบ)
FontFamily fontFamily = new FontFamily("Arial");

// Font: ขนาด Style Unit
Font font = new Font(fontFamily, 20, FontStyle.Bold, GraphicsUnit.Pixel);
// หรือ
Font font = new Font("Arial", 20, FontStyle.Bold); // Default Unit = Point

พิมพ์ข้อความ (PrintPage Event)

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) {
    Font font = new Font("Arial", 14, FontStyle.Regular);
    Brush brush = Brushes.Black;
    PointF location = new PointF(100, 100);
    
    e.Graphics.DrawString("Hello Custom Font!", font, brush, location);
    
    // หลายบรรทัด
    string[] lines = { "Line 1", "Line 2", "Line 3" };
    float y = 100;
    foreach (var line in lines) {
        e.Graphics.DrawString(line, font, brush, 100, y);
        y += font.GetHeight(e.Graphics); // ระยะห่างบรรทัด
    }
}

WPF (System.Windows.Media)

using System.Windows.Media;
using System.Windows.Documents;
using System.Printing;

// FlowDocument สำหรับ Rich Text
FlowDocument doc = new FlowDocument();
Paragraph para = new Paragraph(new Run("Hello WPF Font!")) {
    FontFamily = new FontFamily("Segoe UI"),
    FontSize = 20,
    FontWeight = FontWeights.Bold,
    Foreground = Brushes.Black
};
doc.Blocks.Add(para);

// Print
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == true) {
    IDocumentPaginatorSource paginator = doc;
    dialog.PrintDocument(paginator.DocumentPaginator, "Custom Font Print");
}

ติดตั้ง Font แบบ Embedded (ไม่ต้อง Install บนเครื่อง)

using System.Drawing.Text;

PrivateFontCollection _fonts = new PrivateFontCollection();

// โหลดจากไฟล์ .ttf/.otf
_fonts.AddFontFile("CustomFont.ttf");

// ใช้ FontFamily จาก Collection
FontFamily customFamily = _fonts.Families[0];
Font customFont = new Font(customFamily, 16, FontStyle.Regular);

ตัวอย่างครบถ้วน (WinForms Print)

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public class CustomFontPrint : Form {
    private PrintDocument _printDoc = new PrintDocument();
    private Font _printFont;
    
    public CustomFontPrint() {
        _printDoc.PrintPage += PrintDoc_PrintPage;
        _printFont = new Font("Consolas", 12); // Monospace Font
        
        Button btnPrint = new Button { Text = "Print", Dock = DockStyle.Top };
        btnPrint.Click += (s, e) => _printDoc.Print();
        Controls.Add(btnPrint);
    }
    
    private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e) {
        string text = "Custom Font Printing Example\nLine 2\nLine 3";
        e.Graphics.DrawString(text, _printFont, Brushes.Black, 50, 50);
    }
    
    protected override void Dispose(bool disposing) {
        if (disposing) {
            _printFont?.Dispose();
            _printDoc?.Dispose();
        }
        base.Dispose(disposing);
    }
}

จุดสำคัญ

  • FontFamily: ชื่อ Typeface (Arial, Times New Roman, Consolas)
  • Font: ขนาด (Size), Style (Bold, Italic, Underline), Unit (Point, Pixel, Millimeter)
  • Graphics.DrawString: วาดข้อความบน Surface (Printer, Screen, Bitmap)
  • Dispose(): Font และ Graphics เป็น Unmanaged Resources — ต้อง Dispose เสมอ
  • Printer ต้องรองรับ Font นั้น (หรือ Embed Font ใน PDF แทน)