WPF Data Chart to PDF

Tom Puglisi / Monday, January 16, 2012

The Infragistics NetAdvantage WPF controls toolset currently does not have a PDF engine, however, for certain cases, one can use the PDF engine available in the NetAdvantage Windows Forms toolset. In this topic, I will give you a quick explanation on how to accomplish this by taking an image snapshot of a WPF FrameworkElement and adding it to the PDF. I also include a simple downloadable sample.

 

Getting Started

Create a basic WPF Application that contains a Window along with an instance of xamDataChart. The xamDataChart control is set up with some data along with any combination of series, axes, legends, or whatever you need. For directions on how to get to this point, you can refer to the following xamDataChart getting started help topic.

References

Add the following references to your Microsoft™ Visual Studio project:

  • Infragistics4.Documents.Core.v11.2
  • Infragistics4.Documents.Reports.v11.2

The “4” in the first assembly naming section represents “CLR4”, and the “11.2” in the assembly name represents “the 2011, volume two version of the NetAdvantage product”.

These assemblies are part of the NetAdvantage Windows Forms installation.

Execution Logic

This is straight forward – convert a FrameWork element into an image; in this case, the xamDataChart control instance. Create a PDF Document and then insert the image into the PDF document. That’s it.

Image from FrameworkElement

The following C# code accomplishes this:

        public static void SaveImage(string fileName, FrameworkElement element)
        {
            string imageExtension = 
                new FileInfo(fileName).Extension.ToLower(CultureInfo.InvariantCulture);

            BitmapEncoder imgEncoder = null;
            switch (imageExtension)
            {
                case ".bmp":
                    imgEncoder = new BmpBitmapEncoder();
                    break;

                case ".jpg":
                case ".jpeg":
                    imgEncoder = new JpegBitmapEncoder();
                    break;

                case ".png":
                    imgEncoder = new PngBitmapEncoder();
                    break;

                case ".gif":
                    imgEncoder = new GifBitmapEncoder();
                    break;

                case ".tif":
                case ".tiff":
                    imgEncoder = new TiffBitmapEncoder();
                    break;

                case ".wdp":
                    imgEncoder = new WmpBitmapEncoder();
                    break;

                default:
                    imgEncoder = new BmpBitmapEncoder();
                    break;
            }

            if (element != null)
            {
                RenderTargetBitmap bmpSource = 
                    new RenderTargetBitmap(
                        (int)element.ActualWidth, 
                        (int)element.ActualHeight, 96, 96, 
                        PixelFormats.Pbgra32);
                bmpSource.Render(element);

                imgEncoder.Frames.Add(BitmapFrame.Create(bmpSource));
                using (Stream stream = File.Create(fileName))
                {
                    imgEncoder.Save(stream);
                    stream.Close();
                }
            }
        }

Creating a PDF Document

The following code creates a PDF document and includes the image generated from the previous code block.

 

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Report theReport = new Report();

            ISection theSection = theReport.AddSection();

            IText theText = theSection.AddText();

            theText.AddContent("Chart Data");

            string theImgFile = 
                AppDomain.CurrentDomain.BaseDirectory + @"\theimg.png";


            SaveImage(theImgFile, this.xamDataChart1);

            theSection.AddImage(
                new Infragistics.Documents.Reports.Graphics.Image(theImgFile));


            string theFile = AppDomain.CurrentDomain.BaseDirectory + @"\thePDF.pdf";

            theReport.Publish(theFile, FileFormat.PDF);

            System.Diagnostics.Process.Start(theFile);

        }

And there you have it, a simple way to convert a FrameworkElement into an Image and then load it into a PDF Document. One thing to note – this is a WYSIWYG approach – meaning that Whatever state the FrameworkElement is in,(xamDataChart control in this case) the image will be captured in that exact same state.

Feel free to download the included sample application.

http://media.infragistics.com/community/Release/11.2/XamlDV/wpf_datachart_to_pdf/xamChartToPDF.zip