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;
}
}