I am currently using the OxyPlot and QuestPDF libraries to be able to generate a PDF with charts. I have designed a pie chart and a bar chart. I am currently saving the charts, exporting them to SVG, inside a folder, and then adding them to the PDF. I would like to change this and save them in memory and then yes, add them to the PDF. According to the documentation I read from the OxyPlot library, it has an option to save the plot to a PNG in memory, but then I don't know how to insert it into the PDF:
var stream = new MemoryStream();
var pngExporter = new PngExporter { Width = 600, Height = 400, Background = OxyColors.White };
pngExporter.Export(plotModel, stream);
I leave below the code that I am using to export the graph, in SVG format, and saving it in a folder. And then, I add it to the PDF. This is how I generate the SVG and export it:
using (var stremPieChart = File.Create("pieChart.svg"))
{
var exporter = new OxyPlot.SvgExporter { Width = 400, Height = 300, UseVerticalTextAlignmentWorkaround = true };
exporter.Export(newPieModel, stremPieChart);
}
And so I add it to the PDF:
.Element(element =>
{
element
.PaddingTop(-13)
.PaddingLeft(4)
.AlignCenter()
.Canvas((canvas, size) =>
{
var pieChartSvg = new SKSvg();
pieChartSvg.Load("pieChart.svg");
canvas.Scale(0.65f);
canvas.DrawPicture(pieChartSvg.Picture);
});
});
I hope you can help me! Thank you!