1

I have an MS Word 2013 document containing mathematical formula which I want to transform into image, programmatically.

What I tried is:
Since Office 2007 Microsoft has changed representation of mathematical formulas into Office MathML (OMML) format.

  1. I could covert this formula in OMML format into MathML format. My Java code for the same:

//get xslt

StreamSource xlsStreamSource = new StreamSource("D:/TEMP/OMML2MML.XSL");
//Get the document.xml from .docx file

StreamSource xmlStreamSource = new StreamSource("D:/TEMP/word/document.xml");

//Using xalan transformer factory.

TransformerFactory transformerFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl", null);

File pathToHTMLFile = new File("D:/TEMP/Newfolder/word/documentModified.xml");

StreamResult result = new StreamResult(pathToHTMLFile);
Transformer transformer = transformerFactory.newTransformer(xlsStreamSource);
transformer.transform(xmlStreamSource, result);
  1. Now I have "result" containing formula into MathML format. When I try to put this formula into Clipboard to get image flavor out of it, it is stored as plain XML text, there is no image flavor available.

//Writing and reading provided XMLstring into and from Clipboard

/**
 * Write to Clipboard.
 * @param s string containing MathML representation
 * @param owner
 */
public static void writeToClipboard(String s, ClipboardOwner owner)
{
   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   Transferable transferable = new StringSelection(s);
   clipboard.setContents(transferable, owner);
}



/**
 * Read clipboard data as image
 * @return Image
 * @throws Exception
 */
public static Image getImageFromClipboard() throws Exception 
{
    Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
    if (transferable != null&& transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
        return (Image) transferable.getTransferData(DataFlavor.imageFlavor);
    } else {
        return null;
    }
}

I want to write MathML(XML) into clipboard, so i can read image flavor out of it. How can I write this MathML formula format into Clipboard, so I will get image flavor out of it?

Note:I am not sure whether I am going in right direction or not.If there is any existing API available for the same that would be more easier to do this.

Kishor Jadhav
  • 196
  • 2
  • 16

0 Answers0