Free Support Forum - aspose.com

When change docx to htm,how set officemath to svg image


#1

when change docx to htm,how set officemath to svg image ,
like api

setExportTextBoxAsSvg

i found the way to change math

OfficeMath math = (OfficeMath)doc.getChild(NodeType.OFFICE_MATH, 0, true);
math.getMathRenderer().save(getMyDir() + “formula.svg”, new ImageSaveOptions(SaveFormat.SVG));

but how to set in HtmlSaveOptions
thanks


#2

@hlgao

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.

We apologize for your inconvenience.


#3

@mannanfazil
Thank you very much and look forward to the good news


#4

@hlgao

Please use the following code to meet the requirement. Hope, this helps.

Document doc = new Document("D:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

OfficeMath math = (OfficeMath) doc.getChild(NodeType.OFFICE_MATH, 0, true);
math.getMathRenderer().save("D:\\temp\\awjava-19.1.001.svg", new ImageSaveOptions(SaveFormat.SVG));

builder.moveTo(math);
Shape img = builder.insertImage("D:\\temp\\awjava-19.1.001.svg");
img.setAlternativeText("OfficeMath");

math.remove();

HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setPrettyFormat(true);
opts.setImageSavingCallback(new HandleImageSaving());

doc.save("D:\\temp\\awjava-19.1.html", opts);

static class HandleImageSaving implements IImageSavingCallback {
 public void imageSaving(ImageSavingArgs args) throws Exception {
  Shape shape = ((Shape) args.getCurrentShape());
  if (shape.hasImage() && shape.getAlternativeText().equals("OfficeMath")) {
   args.setImageFileName(args.getImageFileName().replace(".png", ".svg"));

   args.setImageStream(new ByteArrayOutputStream());
   args.setKeepImageStreamOpen(false);
  }
 }
}

#5

@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?


#6

@hlgao

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

static class HandleImageSaving implements IImageSavingCallback {
 public void imageSaving(ImageSavingArgs args) throws Exception {
  Shape shape = ((Shape) args.getCurrentShape());
  if (shape.hasImage() && shape.getAlternativeText().equals("OfficeMath")) {
   args.setImageFileName(args.getImageFileName().replace(".png", ".svg"));

   args.setImageStream(new ByteArrayOutputStream());
   args.setKeepImageStreamOpen(false);
  }
 }
}

#7

@mannanfazil
Thank you very much,
but when I tested the code you provided, I found if use the code

args.setImageStream(new ByteArrayOutputStream());
args.setKeepImageStreamOpen(false);

in HandleImageSaving
then images of svg would not be generated


#8

@hlgao

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.

Thanks for your cooperation.


#9

@mannanfazil
math.zip (16.3 KB)
Please check
Thanks


#10

@hlgao

Thank you for sharing document. 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-19.1.00" + i + ".svg", new ImageSaveOptions(SaveFormat.SVG));
 builder.moveTo(math);
 Shape img = builder.insertImage("D:\\temp\\svg\\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\\svg\\awjava-19.1.html", opts); 

static class HandleImageSaving implements IImageSavingCallback {
 public void imageSaving(ImageSavingArgs args) throws Exception {
  Shape shape = ((Shape) args.getCurrentShape());
  if (shape.hasImage() && shape.getAlternativeText().equals("OfficeMath")) {
   args.setImageFileName(args.getImageFileName().replace(".png", ".svg"));
   args.setImageStream(new ByteArrayOutputStream());
   args.setKeepImageStreamOpen(false);
  }
 }
}

Please check attached files for your reference. output.zip (14.1 KB)


#11

@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)


#12

@hlgao

We are working on your query and will get back to you soon.


#13

@hlgao

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

#14

@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?


#15

@hlgao

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.