Inserting chart image while converting excel to pdf in Java

Hi,

Since we cannot export chart directly from excel to pdf, I converted the chart in the excel to images and would like to add them in the corresponding location in the pdf file.

I did successfully use the ChartToimage and placed the image in pdf but not in the same place as in the excel.

For example, the chart may be between two tables and in the pdf also it should be in the same order.

How do you know the position where the chart has to be placed in the pdf. Please help.

Thanks

Hi,

Thanks for your interest in our products.

There are multiple ways to accomplish your requirement.

You can create a main table and add the one table in 1st cell of the main table, then add the image in 2nd table cell and then in the 3rd cell add the other table. For related information, please visit the following links

  • Table
  • Create Nested Table (Please only refer to the description over this link. The code might not work properly. For sample code, please visit Table)

You can use the FloatingBox to accomplish your requirement. A FloatingBox is a paragraph which can be placed anywhere over the page based over Absolute positioning rather than Relative positioning. You can put the image inside the FloatingBox and then place it in between the two tables. Please visit the following link for information on FloatingBox

In case I've not properly understood your requirement or you've any further query, please feel free to contact.

Thanks for your reply.

I already have the tables and chart in the excel template. I just need to save a pdf in the same format as the Excel template by inserting the chart as an image from excel to pdf.

I have attached the sample code and template.

Thanks

Hi,

Sorry for replying you late.

I've tested the scenario while adding a sample chart inside the source XLSX file that you've shared. Please find attached the updated XLSX file that I've generated. I've noticed that when the same file is converted into PDF format, the second table is being displayed a few rows beneath the image (some extra rows are being added after the image). Before we proceed towards its resolution, Can you please confirm, if its the actual problem that you've been facing ?

We apologize for your inconvenience.

Thank you for the reply.

I have attached a template xlsx file. We need to convert this to pdf. There are two tables and a chart between them. How can we successfully convert this to pdf in the same format?

Also do we need to know that there are so many chart images and where in the template they are, so that we can place them at the corresponding location in the pdf file. How can we determine this location?

Thanks

Hi,

Thanks for sharing the resource worksheet. We will look into the details of this problem and will keep you updated with the status of correction.

Thanks for your cooperation.

Thank you. I will wait for the fix.

Thanks

Hi,

Do you have an update on a fix for this issue?

Thanks

Hi,

Aspose.Cells team is planning to release a hotfix in coming days which will support the feature to convert charts to images automatically and then save and set those images for the generated intermediate XML file. Please be patient and we will get back to you shortly.

Thanks for your patience and understanding.

Thank you for the response.

Hi,

Please try the attached version of Aspose.Cells for Java. To export chart as image for Excel to Pdf feature has been supported now.

Thank you.

Thank you for the new jar file.

I tried it out and I'm getting the following exception:

Exception in thread "main" java.lang.NoSuchMethodError: com.aspose.a.c.a.a(Lcom/aspose/cells/Chart;)Z

at com.aspose.cells.Chart.toImage(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.c(Unknown Source)

at com.aspose.cells.cR.b(Unknown Source)

at com.aspose.cells.cR.c(Unknown Source)

at com.aspose.cells.cR.b(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.Workbook.save(Unknown Source)

at com.nielsen.claritas.poc.Aspose.exportToPdf(Aspose.java:102)

at com.nielsen.claritas.poc.Aspose.main(Aspose.java:182)

When I try to do:

templateWB.save(new FileOutputStream(filename + ".xml"), FileFormatType.ASPOSE_PDF);

Should I convert the chart to image or is this taken care of in the fix you sent me, So all I have to do is save excel(with chart) as a pdf?

I have sent my class and the workbook as an attachment.

Thanks

Hi,

"Should I convert the chart to image or is this taken
care of in the fix you sent me, So all I have to do is save excel(with
chart) as a pdf?"
You don’t need to convert the chart to image as Aspose.Cells for Java would take care of it.

I have used the following code with my attached version and Aspose.Pdf for Java v2.5 and it works fine.
Attached is my output pdf file.

//Instantiate the Workbook object
Workbook workbook = new Workbook();
workbook.open(“d:\files\template.xlsx”);

//Save the document in Aspose.Pdf.Xml format
workbook.save(“d:\files\MySpreadsheetn.xml”, FileFormatType.ASPOSE_PDF);

//Bind the XML file (containing spreadsheet data) with the Pdf object
Pdf pdf = new Pdf();
pdf.bindXML(“d:\files\MySpreadsheetn.xml”, null);

//Create the PDF document by calling its save method
pdf.save(“d:\files\MyPdfn.pdf”);

The gap(some extra rows are inserted after the chart picture in the pdf
file - I am sure Aspose.Pdf team would look into it soon.) is still
there.


Thank you.

Thank you for the quick reply.

I still get the same exception. I even created a new class, replaced my code with yours. Still the same. I'm using Java 1.5 version and Aspose.CellsV2.1.2.9. Is there any other jar file I need to use.

Exception in thread "main" java.lang.NoSuchMethodError: com.aspose.a.c.a.a(Lcom/aspose/cells/Chart;)Z

at com.aspose.cells.Chart.toImage(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.c(Unknown Source)

at com.aspose.cells.cR.b(Unknown Source)

at com.aspose.cells.cR.c(Unknown Source)

at com.aspose.cells.cR.b(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.cR.a(Unknown Source)

at com.aspose.cells.Workbook.save(Unknown Source)

at com.nielsen.claritas.poc.AsposePdfNew.main(AsposePdfNew.java:23)

public static void main(String[] args) throws IOException {

//Instantiate the Workbook object

Workbook workbook = new Workbook();

workbook.open("C:\\temp\\aspose\\template.xlsx");

//Save the document in Aspose.Pdf.Xml format

workbook.save("c:\\temp\\aspose\\MySpreadsheetn.xml", FileFormatType.ASPOSE_PDF);

//Bind the XML file (containing spreadsheet data) with the Pdf object

Pdf pdf = new Pdf();

pdf.bindXML("c:\\temp\\aspose\\MySpreadsheetn.xml", null);

//Create the PDF document by calling its save method

pdf.save("c:\\temp\\aspose\\MyPdfn.pdf");

}

Hi,

That looks strange to us as it works fine here.

Anyways, we will further look into your issue and get back to you soon.

Thank you.

Thank you very much.

It does seem strange.Not sure if I'm missing something. But if I remove the chart image from the excel template, conversion to pdf works fine.

Thanks

I'm attaching the intermediate xml file that is created which has an error. Hope it helps.

Thanks

As you said I was able to create the pdf with the chart image from the excel. The problem was I also had pdf.kit in my reference. Once I removed this reference, the pdf conversion worked well. Can you please check this one.

Your support is greatly appreciated and we are impressed. :-)

Thanks

I still have one question about chart images from excel to pdf. The lines in the chart that was exported from excel to pdf are not the same as in the excel template. Excel to excel conversion retains the exact chart in the source excel file.

I have attached the template.

Thank you

Hi,

Could you post your generated pdf file here, we will check it soon.

Thank you.