Create PDF including charts without UI

I would like to create SAAS or console application that generates a pdf file including charts . I have tried to use the ExcelEngine but we are facing a problem when we try to create a chart. I used the ExcelChartToImageConverted but an exception occured with the following information. I have found the above method only for WPF mvc and asp.net libraries. Any suggestions ?

The console application (.NET 6.0) code looks like the following example,

Code Sample:

-------------------------

using Syncfusion.ExcelChartToImageConverter;

using Syncfusion.XlsIO;

using System.Drawing;

using System.Security.Permissions;

using(ExcelEngine excelEngine = new ExcelEngine())

{

//Initialize application

IApplication application = excelEngine.Excel;

//Set default application version

application.DefaultVersion = ExcelVersion.Excel2016;

//Instantiate chart to image converter for Excel imaging

application.ChartToImageConverter = new ChartToImageConverter();

//Set the image quality

application.ChartToImageConverter.ScalingMode = ScalingMode.Normal;

//Open the existing Excel workbook containing chart

string inputFileName = Path.GetFullPath("Sample.xlsx");

IWorkbook workbook = application.Workbooks.Open(inputFileName, ExcelOpenType.Automatic);

//Access the first worksheet from the worksheets collection

IWorksheet worksheet = workbook.Worksheets[0];

//Access the first chart from the charts collection

IChart chart = worksheet.Charts[0];

//Set chart to image converter

application.ChartToImageConverter = new Syncfusion.ExcelChartToImageConverter.ChartToImageConverter();

application.ChartToImageConverter.ScalingMode = ScalingMode.Best;

//Save as image

MemoryStream stream = new MemoryStream();

chart.SaveAsImage(stream);

System.Drawing.Image image = System.Drawing.Image.FromStream(stream);

image.Save("Output.png");


//Create the memory stream for chart image

MemoryStream chartStream = new MemoryStream();

//Save Excel chart as image

chart.SaveAsImage(chartStream);

chartStream.Position = 0;

//Get the image file from stream

// System.dwa Image image = Image.FromStream(chartStream);

var img = Bitmap.FromStream(chartStream);

//Save the image in png format

img.Save("Output.png");

}


-------------------------

Exception that occurs:

------------------------------

System.IO.FileNotFoundException

HResult=0x80070002

Message=Could not load file or assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Source=Syncfusion.ExcelChartToImageConverter.WPF

StackTrace:

at Syncfusion.ExcelChartToImageConverter.ChartToImageConverter.SaveAsImage(IChart excelChart, Stream imageAsStream)

at Syncfusion.XlsIO.Implementation.Shapes.ChartShapeImpl.SaveAsImage(Stream imageAsStream)

at Program.

--------------------


1 Reply

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team December 23, 2021 06:18 AM UTC

Hi Amanda, 

Greetings from Syncfusion. 

We suggest you to use Syncfusion.XlsIORenderer.Net.Core nuget package in your application and follow the below link for code snippet to convert Excel with Chart to PDF. 


Kindly try and let us know if this helps. 

Regards, 
Keerthi. 


Loader.
Up arrow icon