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

Free Support Forum - aspose.com

Not able to convert doc object image clearly while converting to svg

Dear Sir/Ma’am,

I am getting an issue while converting an image from excel into svg image. The horizontal bar of the chart in image getting some text overlapped.

public List ReadExcelImages(string file, string outputDirectory)
{
try
{
String dataDir = file; // Utils.getDataDir(ExportChartToSVG.class);
Workbook workbook = new Workbook(file);
List names = new List();
List displayNames = new List();
List sortedNames = new List();
bool flag = false;
foreach (Worksheet worksheet in workbook.Worksheets)
{
worksheet.PageSetup.Zoom = 100;
int i = 1;
foreach (Aspose.Cells.Charts.Chart chart in worksheet.Charts)
{
if (chart.Name.ToLower().StartsWith(“ct_”))
{
displayNames.Add(chart.Name.ToLower());
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.SaveFormat = Aspose.Cells.SaveFormat.SVG;
opts.SVGFitToViewPort = true;

                        names.Add(chart.Name.ToLower() + "ch.svg");
            
                        chart.ToImage(outputDirectory + chart.Name.ToLower() + "ch.svg", opts);
                    }

                    else if (chart.Name.ToLower().StartsWith("ex_"))
                    {
                        displayNames.Add(chart.Name.ToLower());
                        ImageOrPrintOptions opts = new ImageOrPrintOptions();
                        opts.SaveFormat = Aspose.Cells.SaveFormat.SVG;
                        opts.SVGFitToViewPort = true;

                        names.Add(chart.Name.ToLower() + "ch.svg");
            
                        chart.ToImage(outputDirectory + chart.Name.ToLower() + "ch.svg", opts);
                    }
                }


                foreach (Aspose.Cells.Drawing.Picture picture in worksheet.Pictures)
                {
                    if (picture.Name.ToLower().StartsWith("ct_"))
                    {
                        ImageOrPrintOptions opts = new ImageOrPrintOptions();
                        opts.SaveFormat = Aspose.Cells.SaveFormat.SVG;
                        opts.SVGFitToViewPort = true;

                        names.Add(picture.Name.ToLower() + ".svg");
            
                        picture.ToImage(outputDirectory + picture.Name.ToLower() + ".svg", opts);
                    }

                    if (picture.Name.ToLower().StartsWith("ex_"))
                    {
                        ImageOrPrintOptions opts = new ImageOrPrintOptions();
                        opts.SaveFormat = Aspose.Cells.SaveFormat.SVG;
                        opts.SVGFitToViewPort = true;

                        names.Add(picture.Name.ToLower() + ".svg");
            
                        picture.ToImage(outputDirectory + picture.Name.ToLower() + ".svg", opts);
                    }
                }

                if (flag == false)
                {
                    flag = true;
                    var collection = from Aspose.Cells.Name nm in workbook.Worksheets.Names where nm.Text.ToLower().StartsWith("ex_") == true || nm.Text.ToLower().StartsWith("ct_") == true select nm;
                    int count = 0;
                    foreach (var name in collection.ToList())//wb.Worksheets.Names)
                    {
                        string text = name.Text.ToLower();
                        if (text.ToLower().StartsWith("ex_") || text.ToLower().StartsWith("ct_"))
                        {
                            if (name.GetRanges() != null)
                            {
                                foreach (var range in name.GetRanges())
                                {

                                    string ranges = range.Address;

                                    string startrow = (range.FirstRow + 1).ToString();
                                    string startcol = Convert.ToChar(65 + range.FirstColumn).ToString();
                                    string startrange = startcol + startrow;

                                    workbook.Worksheets[name.GetRange().Worksheet.Name].PageSetup.PrintArea = range.Address;
                                    workbook.Worksheets[name.GetRange().Worksheet.Name].PageSetup.LeftMargin = 0;
                                    workbook.Worksheets[name.GetRange().Worksheet.Name].PageSetup.TopMargin = 0;
                                    workbook.Worksheets[name.GetRange().Worksheet.Name].PageSetup.RightMargin = 0;
                                    workbook.Worksheets[name.GetRange().Worksheet.Name].PageSetup.BottomMargin = 0;
                                    workbook.Worksheets[name.GetRange().Worksheet.Name].PageSetup.Zoom = 100;

                                    ImageOrPrintOptions options = new ImageOrPrintOptions();
                                    options.OnePagePerSheet = true;
                                    //options.ImageType = Aspose.Cells.Drawing.ImageType.Svg;
                                    options.SaveFormat = Aspose.Cells.SaveFormat.SVG;
                                    SheetRender render = new SheetRender(workbook.Worksheets[name.GetRange().Worksheet.Name], options);
                                    int index = workbook.Worksheets[name.GetRange().Worksheet.Name].Index;
                                    render.ToImage(0, outputDirectory + "\\" + text + ".svg");
                                    names.Add(text + ".svg");
                                    count++;
                                }
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < names.Count; i++)
            {
                for (int j = i + 1; j < names.Count; j++)
                {
                    int n1 = int.Parse(names[i].ToLower().Replace("ex_", "").Replace("ch", "").Replace("ct_", "").Replace(".svg", "").Trim());
                    int n2 = int.Parse(names[j].ToLower().Replace("ex_", "").Replace("ch", "").Replace("ct_", "").Replace(".svg", "").Trim());
                    if (n1 > n2)
                    {
                        string temp = names[i];
                        names[i] = names[j];
                        names[j] = temp;
                    }
                }
            }

            var cvr = (from str in names where str.ToLower().StartsWith("ct_") select str).ToList<string>();

            for (int i = 0; i < cvr.Count; i++)
            {
                for (int j = i + 1; j < cvr.Count; j++)
                {
                    int n1 = int.Parse(cvr[i].ToLower().Replace("ch", "").Replace("ct_", "").Replace(".svg", "").Trim());
                    int n2 = int.Parse(cvr[j].ToLower().Replace("ch", "").Replace("ct_", "").Replace(".svg", "").Trim());
                    if (n1 > n2)
                    {
                        string temp = cvr[i];
                        cvr[i] = cvr[j];
                        cvr[j] = temp;
                    }
                }
            }

            for (int i = 0; i < cvr.Count; i++)
            {
                names.Remove(cvr[i]);
                names.Insert(i, cvr[i]);
            }
            //names.Sort();

            for (int i = 0; i < names.Count; i++)
            {
                XDocument doc = XDocument.Load(outputDirectory + "\\" + names[i]);
                var root = doc.Root;
                root.AddFirst(new XElement("style", "@import url(\"../../../Content/css/svgfonts.css\");"));// "<style>@import url(\"../../../Content/css/svgfonts.css\");</style>");
                doc.Save(outputDirectory + "\\" + names[i]);

                File.WriteAllText(outputDirectory + "\\" + names[i], File.ReadAllText(outputDirectory + "\\" + names[i]).Replace("xmlns=\"\"", "type='text/css'"));
                AddAnchor(outputDirectory + "\\" + names[i]);
            }

            return names;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

@yogeshrawle,

Could you please zip and attach the template Excel file (containing charts, images, etc.) in which you are converting to SVG format. Also share output images by Aspose.Cells. Moreover, share some screenshots to highlight the problems with the output image(s). We will check your issue soon.

Issue.zip (813.4 KB)

@yogeshrawle,

Thanks for the template file.

Please notice, I am able to reproduce the issue as you mentioned (via screenshot) by rendering the picture in the sheet to SVG format. I found in the horizontal bar for Date labels of the chart image, text is overlapped. I can use the following sample code segment to find the issue.
e.g.
Sample code:

            Workbook workbook = new Workbook("e:\\test2\\CV note_2.xls");
            Worksheet worksheet = workbook.Worksheets["Sheet1"];            
            worksheet.PageSetup.Zoom = 100;

            foreach (Aspose.Cells.Drawing.Picture picture in worksheet.Pictures)
            {
                if (picture.Name == "CT_4")
                {
                    ImageOrPrintOptions opts = new ImageOrPrintOptions();
                    opts.ImageType = ImageType.Svg;
                    opts.SVGFitToViewPort = true;
                    picture.ToImage("e:\\test2\\"+ picture.Name.ToLower() + ".svg", opts);
                }
            }

I have logged a ticket with an id “CELLSNET-52410” for your issue. We will look into it soon.

Once we have an update on it, we will let you know.

Thank you sir for your prompt response. Waiting for the solution.

@yogeshrawle,

We will keep you posted with new updates (once available) on your issue. Hopefully, it will be fixed soon.

@yogeshrawle,

This is to inform you that your issue has been resolved now. The fix will be included in our upcoming release (Aspose.Cells v23.1) which is scheduled in the first half of January 2023. You will be notified once the next release is published.

Hello Sir.
I’m glad to know that issue is resolved. I hereby want to request you to please provide me at least the beta version as soon as possible to implement that in my application to get approval from my client to further work on their requirements.

@yogeshrawle,

We are sorry but we do not share fixes in the normal support forums. Either you may wait for the next (official) version to be released or avail paid support. We got to follow the policy across the board, the policy refers to fixes are posted in paid support desk only. In normal support forums, we can only share the results (if required) tested by our (internal) fix once we fix the issue.

The issues you have found earlier (filed as CELLSNET-52410) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi