Render PDF with ResolveFont event handler
Render PDF with ResolveFont event handler
When you try to render a PDF which uses non-system fonts, you can add your own specified fonts to make sure it is rendered correctly.
static void Main(string[] args)
{
using (FileStream fs = new FileStream("input.pdf",FileMode.Open,FileAccess.Read))
{
Document d = new Document(fs);
Page page = d.Pages[0];
RenderSettings renderSettings = new RenderSettings();
renderSettings.TextSettings.ResolveFont += new ResolveFontEventHandler(Resolve);
Bitmap bmp = new Bitmap((int)page.Width * 4, (int)page.Height * 4);
using (Graphics graphics = Graphics.FromImage(bmp))
{
graphics.ScaleTransform(4.0f, 4.0f);
page.Draw(graphics, renderSettings);
}
}
}
// map all non-system fonts to arial
static void Resolve(TextRenderSettings sender, ResolveFontEventArgs args)
{
if (args.FontLocation != FontLocation.System)
{
args.FontRenderMode = FontRenderMode.RenderAsFont;
if (args.PdfFontName.EndsWith("Bold"))
{
args.Bold = true;
}
args.SystemFontName = "Arial";
}
}