Articles in this section
Category / Section

How to export chart to PDF in Xamarin.iOS

1 min read

You can export a chart as a PDF by converting the chart into image stream. From the chart image stream, you can get the PDF of the chart using QLPreviewItem, QLPreviewControllerDataSource, and Syncfusion.Pdf.PdfDocument. The following code snippet demonstrates how to export a chart to PDF.

 

C#:

ExportToPDF.TouchDown += (object sender, EventArgs e) =>
   {
       MemoryStream stream = new MemoryStream();
       ConvertToPDF("Sfchart.pdf", stream, chart);
   };

 

 

public void ConvertToPDF(string filename, MemoryStream stream, SFChart nativechart)
 {
            PdfDocument doc = new PdfDocument();
            doc.PageSettings.Margins.All = 0;
            PdfPage page = doc.Pages.Add();
            PdfGraphics g = page.Graphics;
 
            UIGraphics.BeginImageContext(nativechart.Frame.Size);
            nativechart.Layer.RenderInContext(UIGraphics.GetCurrentContext());
 
            UIImage image = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
 
            Stream imgStream = image.AsJPEG().AsStream();
            g.DrawImage(PdfImage.FromStream(imgStream), 30, 30, 500, 700);
 
            doc.Save(stream);
            doc.Close(true);
 
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, filename);
 
            try
            {
                FileStream fileStream = File.Open(filePath, FileMode.Create);
                stream.Position = 0;
                stream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }
            catch (Exception e)
            {
                e.Message.ToString();
            }
 
            UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
 
            while (currentController.PresentedViewController != null)
                currentController = currentController.PresentedViewController;
            
            QLPreviewController qlPreview = new QLPreviewController();
            QLPreviewItem item = new QLPreviewItemBundle(filename, filePath);
            qlPreview.DataSource = new PreviewControllerDS(item);
 
            currentController.PresentViewController((UIViewController)qlPreview, true, (Action)null);
        }
}

 

 

public class QLPreviewItemFileSystem : QLPreviewItem
 {
        string _fileName, _filePath;
 
        public QLPreviewItemFileSystem(string fileName, string filePath)
        {
            _fileName = fileName;
            _filePath = filePath;
        }
 
        public override string ItemTitle
        {
            get
            {
                return _fileName;
            }
        }
 
        public override NSUrl ItemUrl
        {
            get
            {
                return NSUrl.FromFilename(_filePath);
            }
        }
    }
 
    public class QLPreviewItemBundle : QLPreviewItem
    {
        string _fileName, _filePath;
        public QLPreviewItemBundle(string fileName, string filePath)
        {
            _fileName = fileName;
            _filePath = filePath;
        }
 
        public override string ItemTitle
        {
            get
            {
                return _fileName;
            }
        }
        public override NSUrl ItemUrl
        {
            get
            {
                var documents = NSBundle.MainBundle.BundlePath;
                var lib = Path.Combine(documents, _filePath);
                var url = NSUrl.FromFilename(lib);
                return url;
            }
        }
    }
 
    public class PreviewControllerDS : QLPreviewControllerDataSource
    {
        private QLPreviewItem _item;
 
        public PreviewControllerDS(QLPreviewItem item)
        {
            _item = item;
        }
 
        public override nint PreviewItemCount(QLPreviewController controller)
        {
            return (nint)1;
        }
 
        public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
        {
            return _item as IQLPreviewItem;
        }
    }

 

 

Sample output

 

 

 

Exported PDF

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied