Render PDF with ResolveFontEventHandlers

Fonts
8/24/2015

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.

1 static void Main(string[] args) 2 { 3 using (FileStream fs = new FileStream(@"..\..\input.pdf",FileMode.Open,FileAccess.Read)) 4 { 5 Document d = new Document(fs); 6 Page page = d.Pages[0]; 7 8 RenderSettings renderSettings = new RenderSettings(); 9 renderSettings.TextSettings.ResolveFont += new ResolveFontEventHandler(Resolve); 10 11 Bitmap bmp = new Bitmap((int)page.Width * 4, (int)page.Height * 4); 12 using (Graphics graphics = Graphics.FromImage(bmp)) 13 { 14 graphics.ScaleTransform(4.0f, 4.0f); 15 page.Draw(graphics, renderSettings); 16 } 17 } 18 } 19 20 //map all non-system fonts to arial 21 static void Resolve(TextRenderSettings sender, ResolveFontEventArgs args) 22 { 23 // Map all non-system fonts to the system font arial 24 25 if (args.FontLocation != FontLocation.System) 26 { 27 args.FontRenderMode = FontRenderMode.RenderAsFont; 28 29 if (args.PdfFontName.EndsWith("Bold")) 30 { 31 args.Bold = true; 32 } 33 34 args.SystemFontName = "Arial"; 35 } 36 }