Convert PDF to PNG using WPF

Convert PDF to PNG using WPF

In this code sample we take a look how you can create PNG images from a PDF using the Windows Presentation Foundation.

Code sample to convert PDF to PNG

        static void Main(string[] args)
        {
            //read input from file
            Document input;
            using (FileStream fs = new FileStream(@"..\..\input.pdf", FileMode.Open, FileAccess.Read))
            {
                input = new Document(fs);
            }

            //the font names in the pdf have a different name than in the windows system.
            //the resolveFont event is raised whenever a font needs to be resolved
            RenderSettings renderSettings = new RenderSettings();
            renderSettings.TextSettings.ResolveFont += new ResolveFontEventHandler(TextSettings_ResolveFont);

            int count = input.Pages.Count;
            double dpi = 300;
            for (int i = 0; i < count; i++)
            {
                Page page = input.Pages[i];
                System.Windows.Documents.FixedPage fixedPage = page.ConvertToWpf(renderSettings, new ConvertToWpfOptions(), new Summary());

                // Create a new bitmap render target.
                double width = page.Width * dpi / 72.0;
                double height = page.Height * dpi / 72.0;
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)(width), (int)(height), dpi, dpi, System.Windows.Media.PixelFormats.Pbgra32);

                // Render the fixed page into the bitmap target.
                bmp.Render(fixedPage);

                // Write the bitmap to file.
                using (FileStream outStream = new FileStream(string.Format(@"..\..\output" + i + ".png"), FileMode.Create))
                {
                    PngBitmapEncoder enc = new PngBitmapEncoder();
                    enc.Frames.Add(BitmapFrame.Create(bmp));
                    enc.Save(outStream);
                }

                // For some PDF documents this samples has a memory usage pattern that appears to be affected by a garbage collection issue in the
                // Large Object Heap. See https://www.simple-talk.com/dotnet/.net-framework/the-dangers-of-the-large-object-heap/
                // Strangely but true: calling GC.GetTotalMemory appears to be able to avoid that issue, so we do that here.
                GC.GetTotalMemory(true);
            }
        }

        static void TextSettings_ResolveFont(TextRenderSettings sender, ResolveFontEventArgs args)
        {
            System.Diagnostics.Trace.WriteLine(String.Format("font ({0}, {1}, {2})", args.PdfFontName, args.SystemFontName, args.FontPath));

            if (args.FontLocation != FontLocation.System)
            {
                switch (args.PdfFontName)
                {
                    case "Times-Roman":
                        args.SystemFontName = "Times New Roman";
                        break;
                    case "Times-Bold":
                        args.SystemFontName = "Times New Roman";
                        args.Bold = true;
                        break;
                    case "Times-Italic":
                        args.SystemFontName = "Times New Roman";
                        args.Italic = true;
                        break;
                    case "Helvetica":
                        args.SystemFontName = "Arial Unicode MS";
                        break;
                    case "Helvetica-Bold":
                        args.SystemFontName = "Arial Unicode MS";
                        args.Bold = true;
                        break;
                    case "Helvetica-Italic":
                        args.SystemFontName = "Arial Unicode MS";
                        args.Italic = true;
                        break;
                }
            }
        }