Thanks for your inquiry. Currently, this feature is missing in Aspose.Words. However, we logged this feature request as WORDSNET-17990 in our issue tracking system. You will be notified via this forum thread once this feature is available.
@mannanfazil
Thank you very much.
What if there are more than one office math in the document?
Is it more consuming and less efficient to generate replacement in this way?
It is an efficient workaround to meet the requirements. We have already logged this feature request and we will share the update via this forum thread once this feature is available. Please use the following code to get the desired result. Hope, this helps.
Document doc = new Document("D:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection oMath = doc.getChildNodes(NodeType.OFFICE_MATH, true);
int i = 1;
for (OfficeMath math: (Iterable < OfficeMath > ) oMath) {
math.getMathRenderer().save("D:\\temp\\awjava-19.1.00" + i + ".svg", new ImageSaveOptions(SaveFormat.SVG));
builder.moveTo(math);
Shape img = builder.insertImage("D:\\temp\\awjava-19.1.00" + i + ".svg");
img.setAlternativeText("OfficeMath");
math.remove();
i++;
}
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setPrettyFormat(true);
opts.setImageSavingCallback(new HandleImageSaving());
doc.save("D:\\temp\\awjava-19.1.html", opts);
Please ZIP and attach the following resources here for testing:
Your input document.
Create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.
@mannanfazil
Thank you very much for your help with this code,
but it is only in certain cases that the svg file name generated by mathml is the same as the svg name that needs to be transformed in the subsequent doc document. In fact, the svg in the doc transformation htm is not generated.
If you add a picture to the doc document, the result will be wrong, as in the following document. input0112.zip (18.3 KB)
Thanks for your patience. By default, when Aspose.Words saves a document to HTML, it saves each image into a separate file. Aspose.Words uses the document file name and a unique number to generate unique file name for each image found in the document.
In the first case, there were only Office Math objects so generated SVG and Aspose.Words assigned names were the same and it worked fine. In the second case, Aspose.Words iterate over each image found in the document i.e. (Office Math, Image) and assigned a unique name to each image. Due to the conflict between generated SVG file name and Aspose.Words assigned file name, SVG does not show same in the output document as generated.
Please use the following code to get desired results. Hope, this helps.
Document doc = new Document("D:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection oMath = doc.getChildNodes(NodeType.OFFICE_MATH, true);
int i = 1;
for (OfficeMath math: (Iterable < OfficeMath > ) oMath) {
math.getMathRenderer().save("D:\\temp\\svg\\awjava-officemath.00" + i + ".svg", new ImageSaveOptions(SaveFormat.SVG));
builder.moveTo(math);
Shape img = builder.insertImage("D:\\temp\\svg\\awjava-officemath.00" + i + ".svg");
img.setAlternativeText("OfficeMath");
math.remove();
i++;
}
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setPrettyFormat(true);
opts.setImageSavingCallback(new HandleImageSaving());
doc.save("D:\\temp\\svg\\awjava-19.1.html", opts);
static class HandleImageSaving implements IImageSavingCallback {
int i = 1;
public void imageSaving(ImageSavingArgs args) throws Exception {
Shape shape = ((Shape) args.getCurrentShape());
if (shape.hasImage() && shape.getAlternativeText().equals("OfficeMath")) {
args.setImageFileName("awjava-officemath.00" + i + ".svg");
args.setImageStream(new ByteArrayOutputStream());
args.setKeepImageStreamOpen(false);
i++;
}
}
}
@mannanfazil
Thank you very much for your patience.
Now the code works.
However, I want to know, if it’s a picture in the word document itself, how does the image be generated by formatting the svg when you convert the doc to htm?
This is not specific only to SVG formatting where an image is generated during document to HTML conversion. It is a default functionality of Aspose.Words, when saves a document to HTML, it saves each shape i.e. (Office Math, Picture, Art etc.) into a separate PNG file.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.