Copy a chart created by Aspose.Cells to a PDF as a PDF chart

Hi,

I am currently using Aspose.Cells to load an Excel file and populate data for a chart. This chart is then saved as SVG and added to a PDF document using an Aspose.PDF image, which is added to the Paragraphs collection of a Page. This renders the SVG in the PDF as an image.

I’ve seen that Aspose.Cells can also do a .ToPdf on a chart, which will save the chart as a beautiful, fully zoomable PDF ‘object’, where the axis text can be selected, etc. I would like to be able to save my chart to my PDF in the same way. Is this possible at all?

Drak

@Drak

Thanks for contacting support.

You may please use following code snippet in order to convert SVG into PDF and see if results are better as per your requirements.

var loadopt = new SvgLoadOptions();
loadopt.AdjustPageSize = true;
Aspose.Pdf.Document Doc = new Aspose.Pdf.Document(dataDir + "EquationSvg.svg", loadopt);
Doc.Save(dataDir + "SvgToPDF.pdf");

In case the results are not as per your desire, please share your sample SVG and expected output PDF for our reference. We will test the scenario in our environment and address it accordingly.

Hi,
I think you may have misunderstood the problem: I don’t want to make a PDF from an SVG, I need to include a chart from Aspose.Cells in a PDF (somewhere on a page, not a separate PDF), and would preferably have it generated using PDF shapes, instead of an image.

@Drak

Thanks for elaborating further.

Would you kindly share a sample input file (e.g. Excel File) where desired chart is present and you want to place it in PDF document. We will check feasibility of your requirement and share our feedback with you accordingly.

@asad.ali

Please find attached a zip file containing the chart, the output straight to PDF, the output when trying to insert it into a PDF page using SVG format, and a small example program of how I am doing these things.

AsposePDFChart.zip (118.5 KB)

It would be great if the ‘straight to pdf’ output from Aspose.Cells could somehow be used in a page in an Aspose.PDF pdf document.

@Drak

Thanks for sharing sample project and files.

For you specific requirements, we suggest you to use PageStamp where you can add stamp at any point (X,Y) over a PDF Page using Aspose.PDF. Please change/modify your methods as following in sample project:

public static string inputfile = "";
static long Generate(Aspose.Pdf.Document doc)
{
 Aspose.Pdf.Facades.PdfFileStamp fileStamp = new Aspose.Pdf.Facades.PdfFileStamp();
 // Open Document
 fileStamp.BindPdf(doc);
 // Create stamp
 Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
 stamp.BindPdf(inputfile, 1);
 stamp.SetOrigin(20, 500);
 // Set particular pages
 stamp.Pages = new int[] { 1 };
 // Add stamp to PDF file
 fileStamp.AddStamp(stamp);
 return stream.Length;
}

static void CreateImageFromChart()
{
 // load workbook and find chart
 Aspose.Cells.Worksheet sheet = _workbook.Worksheets[0];
 Aspose.Cells.Charts.Chart chart = sheet.Charts[0];
 // render
 Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();
 // set options
 options.SaveFormat = Aspose.Cells.SaveFormat.SVG;
 // save as pdf
 chart.ToPdf(System.IO.Path.Combine(_folder, "DirectToPDF.pdf"));
 inputfile = System.IO.Path.Combine(_folder, "DirectToPDF.pdf");
}

We hope above suggestion would help you achieving what you require. In case you need further assistance, please feel free to let us know.

@asad.ali

Hi,
sadly this does not help me, as I don’t know the position of the chart on the page. The chart needs to come after whatever else happens to be on the page, but this is content from a database, so the size is not pre-determined.

@Drak

Would you please share some sample content to add inside PDF which is actually coming from database. We will test the scenario with a sample content and share our feedback with you accordingly.

Hi,

below is a sample which emulates the sort of thing we wish to achieve.

SSCCEChartToPDF2.zip (14.6 KB)

@Drak

We have created a sample PDF document and are sharing it with you. Please check this and let us know about your feedback if this fulfills what your require. In attached PDF, the SVG Chart Image is drawn instead of placed as object.

UsingSVGInPDF_Demo.pdf (102.5 KB)

@asad.ali

Yes! This is exactly what we require!

@Drak

Thanks for the feedback.

The shared output was generated using Aspose.PDF for .NET 18.1 and it seems like newer versions of the API are not honoring the SVG image API used to do in the past. For the sake of investigation, we have logged a ticket as PDFNET-47138 in our issue tracking system so that this matter can be further analysed.

We will surely let you know as soon as the ticket is resolved and your requirements are feasible using latest version of the API. Please spare us little time.

We are sorry for the inconvenience.

@asad.ali

Thank you. Looking forward to the fix :slight_smile: