We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Graphs as Images Excel to PDF

I am using Aspose Cells and I want to generate the excel document as a pdf. However, instead of taking the graph in the excel document and converting it directly the pdf, I’d like to have it in the pdf as an image.


Currently I know that there is a way to save each graph as a .png file (http://www.aspose.com/docs/display/cellsjava/Converting+Chart+to+Image). Is there a way that when generating the pdf files, I am instead using the png files rather than the chart?

Hi Kelly,

Thanks for your posting and using Aspose.Cells.

You can save your charts into png images. If your Excel file has charts, and you save it into PDF, then all the charts will be saved as png images.

Besides, you can also specify that in which image format you want to save images inside the PDF. For example, in the following code, I am saving Excel file into PDF with images format as PNG.

Let us know if you still have any question, we will look into it and help you asap.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\Transportation_Note.xlsx”;


Workbook workbook = new Workbook(filePath);


PdfSaveOptions options = new PdfSaveOptions();

options.ImageType = ImageFormat.Png;


workbook.Save(filePath + “.out.pdf”, options);



Suppose we wanted to generate the graphs as .png images from another source and use those as the graphs in the pdf, is this possible? Is there an easier way than finding the location of the graph in the xlsx document and replacing it with an image before the pdf is generated? For example, when the pdf is generated, does it pull image1, image2, … and place them into the pdf before generation?

Hi Kelly,

Thanks for your posting and using Aspose.Cells.

Please see the following code, it imports all charts as images in PNG format from sample.xlsx into a new workbook and then save the new workbook in XLSX and PDF format.

I have attached sample.xlsx, output.xlsx and output.pdf files for your reference.

C#


Workbook srcWb = new Workbook(“sample.xlsx”);

Worksheet srcSheet = srcWb.Worksheets[0];


Workbook workbook = new Workbook();

Worksheet worksheet = workbook.Worksheets[0];



for (int i = 0; i < srcSheet.Charts.Count; i++)

{

Chart srcChart = srcSheet.Charts[i];


ImageOrPrintOptions options = new ImageOrPrintOptions();

options.ImageFormat = ImageFormat.Png;

options.HorizontalResolution = 200;

options.VerticalResolution = 200;


MemoryStream ms = new MemoryStream();


srcChart.ToImage(ms, options);


ms.Position = 0;


worksheet.Pictures.Add(srcChart.ChartObject.UpperLeftRow, srcChart.ChartObject.UpperLeftColumn,

srcChart.ChartObject.LowerRightRow, srcChart.ChartObject.LowerRightColumn, ms);


}


//Save the workbook into xlsx format

workbook.Save(“output.xlsx”);


//Save the workbook in pdf format

PdfSaveOptions opts = new PdfSaveOptions();

opts.OnePagePerSheet = true;

opts.ImageType = ImageFormat.Png;


workbook.Save(“output.pdf”, opts);