Excel replace image made math equation lost

When I tried to replace Excel image, math equation lost.
Before replace
1.png (110.3 KB)

After replace
2.png (129.6 KB)

I use this code

public static void ReplaceExcelImage(string excelPath, string imagePath)
{
byte[] signBytes = Ruibu.Core.Library.ConvertUtil.fileToBytes(imagePath);

List<Aspose.Cells.Drawing.Picture> listPicture = new List<Aspose.Cells.Drawing.Picture>();
using (Workbook workbook = new Workbook(excelPath))
{
    foreach (Worksheet worksheet in workbook.Worksheets)
    {
        foreach (Aspose.Cells.Drawing.Picture picture in worksheet.Pictures)
        {
            if (picture.AlternativeText == "检测人")
            {
                listPicture.Add(picture);
            }
        }
    }

    foreach (Aspose.Cells.Drawing.Picture picture in listPicture)
    {
        picture.Data = signBytes;
        picture.UpdateSelectedValue();
    }

    workbook.Save(excelPath);
}

}

Sample File
Test.zip (128.2 KB)

@mymbrooks,

Thanks for the template XLSX file and image file.

After an initial testing, I was able to reproduce the issue as you mentioned by using your template XLSX file and image file. I found replacing image makes math equation lost in the output Excel file

We require thorough evaluation of the issue. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-56177

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

@amjad.sahi
Thanks for quick reply and another question :slight_smile:
How to extract math equation to an image or mathml string from Excel ?
Is it possible to get mathml string like this?

\int_{\theta=0}^{\pi}

I tried this but not work:

public static void ExcelMathml(string excelPath, string dirPath)
{
    using (Workbook workbook = new Workbook(excelPath))
    {
        int index;
        string imagePath;
        ImageOrPrintOptions options = new ImageOrPrintOptions();
        options.ImageType = Aspose.Cells.Drawing.ImageType.Svg;

        foreach (Worksheet worksheet in workbook.Worksheets)
        {
            foreach (Aspose.Cells.Drawing.Shape shape in worksheet.Shapes)
            {
                // always false
                if (shape.IsEquation)
                {
                    index = worksheet.Shapes.IndexOf(shape) + 1;
                    imagePath = Path.Combine(dirPath, index.ToString() + ".svg");
                    shape.ToImage(imagePath, options);
                }
            }
        }
    }
}

In word I can get svg like this:

public static void WordMathml(string wordPath, string dirPath)
{
    Document document = new Document(wordPath);
    OfficeMath math;
    int index;
    string imagePath;
    Aspose.Words.Saving.ImageSaveOptions imageSaveOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Svg);
    foreach (Node node in document.GetChildNodes(NodeType.OfficeMath, true))
    {
        index = document.GetChildNodes(NodeType.OfficeMath, true).IndexOf(node) + 1;
        math = (OfficeMath)node;
        imagePath = Path.Combine(dirPath, index.ToString() + ".svg");
        math.GetMathRenderer().Save(imagePath, imageSaveOptions);
    }
}

use sample file from Test.zip above

@mymbrooks,

It seems rendering Math equations in Excel spreadsheets to image format is not supported. Anyways, we need to evaluate it in details.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-56180

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @mymbrooks
For Aspose.Cells, We did an investigation, We use the following code, can get the following SVG file:

        using (Workbook workbook = new Workbook(path + "Excel.xlsx"))
        {
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.ImageType = Aspose.Cells.Drawing.ImageType.Svg;

            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                foreach (Aspose.Cells.Drawing.Shape shape in worksheet.Shapes)
                {
                    // always false
                    if (shape.IsEquation)
                    {
                        index = worksheet.Shapes.IndexOf(shape) + 1;
                        imagePath = Path.Combine(path, "cellsOutput/cells" + index.ToString() + ".svg");
                        shape.ToImage(imagePath, options);
                    }
                }
            }
        }

240712.zip (7.9 KB)

Please describe in detail what you mean by this:

\int_{\theta=0}^{\pi}

You can upload the requirements output as a file attachment as TXT or XML.
So that we can better analyze your needs, thank you.

@leoluo
Test my Test.xlsx, can’t export svg.
I mean is it possible to convert Excel math equation to mathml format like this:


<html>
    <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mtable columnalign="right left right left right left right left right left" columnspacing="0em" displaystyle="true"><mtr><mtd><mi>V</mi><mo stretchy="false">(</mo><mi>R</mi><mo stretchy="false">)</mo></mtd><mtd><mo>=</mo><msubsup><mo>∫</mo><mrow><mi>φ</mi><mo>=</mo><mn>0</mn></mrow><mrow><mn>2</mn><mi>π</mi></mrow></msubsup><msubsup><mo>∫</mo><mrow><mi>θ</mi><mo>=</mo><mn>0</mn></mrow><mi>π</mi></msubsup><msubsup><mo>∫</mo><mrow><mi>r</mi><mo>=</mo><mn>0</mn></mrow><mi>R</mi></msubsup><msup><mi>r</mi><mn>2</mn></msup><mo lspace="0em" rspace="0em">sin</mo><mo stretchy="false">(</mo><mi>θ</mi><mo stretchy="false">)</mo><mi>d</mi><mi>r</mi><mi>d</mi><mi>θ</mi><mi>d</mi><mi>φ</mi></mtd></mtr><mtr><mtd></mtd><mtd><mo>=</mo><mrow><mo>(</mo><mrow><msubsup><mo>∫</mo><mrow><mi>φ</mi><mo>=</mo><mn>0</mn></mrow><mrow><mn>2</mn><mi>π</mi></mrow></msubsup><mi>d</mi><mi>φ</mi></mrow><mo>)</mo></mrow><mrow><mo>(</mo><mrow><msubsup><mo>∫</mo><mrow><mi>θ</mi><mo>=</mo><mn>0</mn></mrow><mi>π</mi></msubsup><mo lspace="0em" rspace="0em">sin</mo><mo stretchy="false">(</mo><mi>θ</mi><mo stretchy="false">)</mo><mi>d</mi><mi>θ</mi></mrow><mo>)</mo></mrow><mrow><mo>(</mo><mrow><msubsup><mo>∫</mo><mrow><mi>r</mi><mo>=</mo><mn>0</mn></mrow><mi>R</mi></msubsup><msup><mi>r</mi><mn>2</mn></msup><mi>d</mi><mi>r</mi></mrow><mo>)</mo></mrow></mtd></mtr><mtr><mtd></mtd><mtd><mo>=</mo><msubsup><mrow><mo>[</mo><mi>φ</mi><mo>]</mo></mrow><mrow><mi>φ</mi><mo>=</mo><mn>0</mn></mrow><mrow><mn>2</mn><mi>π</mi></mrow></msubsup><msubsup><mrow><mo>[</mo><mrow><mo>−</mo><mo lspace="0em" rspace="0em">cos</mo><mo stretchy="false">(</mo><mi>θ</mi><mo stretchy="false">)</mo></mrow><mo>]</mo></mrow><mrow><mi>θ</mi><mo>=</mo><mn>0</mn></mrow><mi>π</mi></msubsup><msubsup><mrow><mo>[</mo><mfrac><msup><mi>r</mi><mn>3</mn></msup><mn>3</mn></mfrac><mo>]</mo></mrow><mrow><mi>r</mi><mo>=</mo><mn>0</mn></mrow><mi>R</mi></msubsup></mtd></mtr><mtr><mtd></mtd><mtd><mo>=</mo><mfrac><mn>4</mn><mn>3</mn></mfrac><mi>π</mi><msup><mi>R</mi><mn>3</mn></msup></mtd></mtr></mtable><annotation encoding="TeX">
      \begin{aligned}
      V(R) &amp;=
      \int_{\varphi=0}^{2 \pi}
      \int_{\theta=0}^{\pi}
      \int_{r=0}^{R}
      r^2 \sin(\theta) dr d\theta d\varphi \\ &amp;=
      \left( \int_{\varphi=0}^{2 \pi}  d\varphi \right)
      \left( \int_{\theta=0}^{\pi} \sin(\theta)  d\theta \right)
      \left( \int_{r=0}^{R} r^2 dr \right) \\ &amp;=
      \left[   \varphi \right]_{\varphi=0}^{2 \pi}
      \left[  -\cos(\theta) \right]_{\theta=0}^{\pi}
      \left[ \frac{r^3}{3} \right]_{r=0}^{R} \\ &amp;=
      \frac{4}{3} \pi R^3
      \end{aligned}
    </annotation></semantics></math>
</html>

Sample file
Test2.zip (14.0 KB)

@mymbrooks
1). We have fixed the issue of reading equations from your template file.
2).

Which tool created this html?
“\int_{\theta=0}^{\pi}” is the annotation part in your attached html, the format is “TeX” ,not MathML. So is “Tex” your expected format?

Both MathML and Text are new file format for us. And they are different from “http://schemas.openxmlformats.org/officeDocument/2006/math” (office math).
We have to check whether we should support them first.

Now you can get EquationNode with the following codes:

TextBox textBox = workbook.Worksheets[0].TextBoxes[0];
EquationNode node = textBox.GetEquationParagraph();

Sorry for my mistake. Yes, It’s Tex and difference from mathml.
Because modern browsers support Text、mathml, so I ask for this feature for some customer requirement.

@mymbrooks,

Thanks for your feedback and confirmation.

We will investigate if we could support rendering math equation to TeX format. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-56213

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@mymbrooks
Did you want to directly extract equation to a TeX String or Embed the equation into html as a TeX string (now Aspose.Cells exports an image)?
1). We plan to add EquationNode.ToTeX() method to directly extract equation to a Tex String:

TextBox textBox = workbook.Worksheets[0].TextBoxes[0];
EquationNode node = textBox.GetEquationParagraph();
node.ToTeX();

2). If exporting TeX to html, the most common and user-friendly methods are using MathJax or KaTeX to make the TeX work. So if exporting to html, would you like to add those external JS references?

@simon.zhao
Thanks for quick solution.
No need to export HTML, just Tex string.
I will show it with front framework(Vue etc…)

@mymbrooks
Thanks for your confirm.
We will start to support EquationNode.ToTeX(); first.
And it’s better that you can share files contains your excepted equations. We will prioritize implementing the ones you frequently use because there are two many TeX Equation syntax.

@simon.zhao
Take it easy, this feature is in my wish list, not for production now.
I want to serialization math equation in my work.
I will share some example equation
Example.zip (75.5 KB)

@mymbrooks,

Thank you for providing your desired TeX equations. We have logged the archive with your existing ticket “CELLSNET-56213” into our database. We will look into the equations list soon.

@mymbrooks,

We are pleased to inform you that your issue (Ticket ID: “CELLSNET-56177”) has been resolved now. The fix will be included in the upcoming release (Aspose.Cells v24.8) which we plan to release in the first half of August 2024. We will notify you when the next release is released.

@mymbrooks
tab2.zip (175 Bytes)

Please check the attached file with the equation in your test.xlsx.

The first character is Mathematical Italic Capital X ( UTF-16 Encoding: `0xD835 0xDC4B’).

@mymbrooks
After closely comparing LaTeX and Tex syntax, we decided to add EquationNode.ToLaTeX(); not ToTex().
Sorry for confusion in the previous post.

@simon.zhao
Thanks it’s OK, support for LaTeX is enough and LaTeX is widely used too.

@mymbrooks
Thank you for your feedback. You are welcome. Once there are any updates, we will notify you promptly.

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