Nieuw lid |
|
Ik wil graag een embedded font gebruiken in mijn applicatie. Nu heb ik het voorbeeld gebruikt van www.bobpowell.net/embedfonts.htm.
Helaas krijg ik in mij printdocument1 gewoon een standaard lettertype te zien ipv. mijn Alphd___.ttf.
Iemand een idee waar dit aan zou kunnen liggen?
De code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
PrivateFontCollection pfc = new PrivateFontCollection();
private void Form1_Load(object sender, EventArgs e)
{
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("WindowsApplication5.Alphd___.ttf");
byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata, 0, (int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed (byte* pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.Show();
printPreviewDialog1.Activate();
printPreviewDialog1.BringToFront();
}
private void printPreviewDialog1_Load(object sender, EventArgs e)
{
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
bool bold = false;
bool regular = false;
bool italic = false;
e.Graphics.PageUnit = GraphicsUnit.Point;
SolidBrush b = new SolidBrush(Color.Black);
float y = 5;
System.Drawing.Font fn;
foreach (FontFamily ff in pfc.Families)
{
if (ff.IsStyleAvailable(FontStyle.Regular))
{
regular = true;
fn = new Font(ff, 18, FontStyle.Regular);
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
fn.Dispose();
y += 20;
}
if (ff.IsStyleAvailable(FontStyle.Bold))
{
bold = true;
fn = new Font(ff, 18, FontStyle.Bold);
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
fn.Dispose();
y += 20;
}
if (ff.IsStyleAvailable(FontStyle.Italic))
{
italic = true;
fn = new Font(ff, 18, FontStyle.Italic);
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
fn.Dispose();
y += 20;
}
if (bold && italic)
{
fn = new Font(ff, 18, FontStyle.Bold | FontStyle.Italic);
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
fn.Dispose();
y += 20;
}
fn = new Font(ff, 18, FontStyle.Underline);
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
fn.Dispose();
y += 20;
fn = new Font(ff, 18, FontStyle.Strikeout);
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic);
fn.Dispose();
}
b.Dispose();
}
}
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Drawing.Text; using System.Windows.Forms; using System.IO; namespace WindowsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } PrivateFontCollection pfc = new PrivateFontCollection (); private void Form1_Load(object sender, EventArgs e) { Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("WindowsApplication5.Alphd___.ttf"); byte[] fontdata = new byte[fontStream. Length]; fontStream.Read(fontdata, 0, (int)fontStream.Length); fontStream.Close(); unsafe { fixed (byte* pFontData = fontdata) { pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length); } } } private void button1_Click(object sender, EventArgs e) { printPreviewDialog1 = new PrintPreviewDialog (); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.Show(); printPreviewDialog1.Activate(); printPreviewDialog1.BringToFront(); } private void printPreviewDialog1_Load(object sender, EventArgs e) { } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { bool bold = false; bool regular = false; bool italic = false; e.Graphics.PageUnit = GraphicsUnit.Point; SolidBrush b = new SolidBrush (Color. Black); float y = 5; System.Drawing.Font fn; foreach (FontFamily ff in pfc.Families) { if (ff.IsStyleAvailable(FontStyle.Regular)) { regular = true; fn = new Font (ff, 18, FontStyle. Regular); e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic); fn.Dispose(); y += 20; } if (ff.IsStyleAvailable(FontStyle.Bold)) { bold = true; fn = new Font (ff, 18, FontStyle. Bold); e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic); fn.Dispose(); y += 20; } if (ff.IsStyleAvailable(FontStyle.Italic)) { italic = true; fn = new Font (ff, 18, FontStyle. Italic); e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic); fn.Dispose(); y += 20; } if (bold && italic) { fn = new Font (ff, 18, FontStyle. Bold | FontStyle. Italic); e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic); fn.Dispose(); y += 20; } fn = new Font (ff, 18, FontStyle. Underline); e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic); fn.Dispose(); y += 20; fn = new Font (ff, 18, FontStyle. Strikeout); e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic); fn.Dispose(); } b.Dispose(); } } }
|