0

Envirment: word2007 on windows7.

I parse a word(.docx) file by using the latest Apache POI library(3.16), and exact the texts, pictures and embedded mathtype equations successfully.

To parse the embedded mathtype equations, I used the following api

List<XWPFPictureData> allPictures = doc.getAllPictures();
int i = 0;
for (XWPFPictureData picture: allPictures) {
    if (picture.getFileName().endsWith(".wmf")) {
        FileUtils.writeByteArrayToFile(new File(i + ".wmf"), 
        picture.getData());
        i++;
    }
}

Now I want to create a new word(docx) file, adding the texts,pictures and embedded mathtype equations back to it.

Add text: XWPFRun run = para.createRun(); run.setText("xxx").

Add picture: XWPFPicture xwpfPicture = run.addPicture(pictureData, pictureType, filename, Units.pixelToEMU(width), Units.pixelToEMU(height));

Add wmf: String rId = doc.addPictureData(new FileInputStream(wmfFilePath), Document.PICTURE_TYPE_WMF);

The problem is that wmf is now invisible, and how to make it visible and place it in the right position? or to say make the wmf related to a created run.

Seeing the poi examples and test cases, but still can't find any api or method. http://svn.apache.org/repos/asf/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/ http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/

There is one related question: How can I add embedded equations to docx files by using Apache POI?

The difference is that I just want to add the parsed wmf files to a new word file, not to write some mathtype equations programaticly.

Can someone please give some help?


eg: I have a word which has one mathtype equation, when double click it, the mathtype editor is open since the mathtype soft is installed on the machine.

Parsed it use POI api:

doc.getAllPictures(); FileUtils.writeByteArrayToFile(new File(i + ".wmf"), picture.getData());

Now the mathtype equation is convert to wmf files on the disk.

run.addPicture(pictureData, Document.PICTURE_TYPE_WMF, filename, Units.pixelToEMU(width), Units.pixelToEMU(height)) it can add wmf as picture, but I want to restore the word, when the user does double click on the word equation, the mathtype editor is open.

POI can parse the mathtype equation in word to wmf files, is there any way to write back to a word as original?

five
  • 1
  • 2
  • "POI can parse the mathtype equation in word to wmf files": No it cannot. If `doc.getAllPictures()` finds `WMF` pictures in `*.docx` files, then they must had come in there before poi was reading. And `Word 2007` itself will not putting formulas as `WMF` pictures into the `*.docx` file. It will convert the formulas to pictures while saving as `Word 97/2003 format` `*.doc`. But after that they are pictures only and not more formulas. – Axel Richter Jul 08 '17 at 08:11
  • Open a new blank word, firstly insert a png picture, then insert a object,choose MathType 6.0 Equation,insert a mathtype equation. The equation can be double click. Then doc.getAllPictures() will return two XWPFPictureData object.Print the fileName one is image1.png ,the other is image2.wmf. Can this not prove poi XWPFPictureData.getData() parse the mathtype equation?Reopen the word, the equation can also doubleclick. – five Jul 08 '17 at 08:39
  • "then insert a object,choose MathType 6.0": Ah. But this is then a `OLEObject` embedded in `Word` instead of a real formula inserted using `Insert - Formula`. No, `apache poi` does not supports creating such `OLEObject`s in `XWPF` until now. One could achieve this by bothering oneself with the special `XML` for this in `/word/document.xml` and getting the needed `/word/embeddings/oleObject1.bin` from somewhere. But I will not do this since this kind of approach is ancient (more than 10 years old). – Axel Richter Jul 08 '17 at 08:59
  • "apache poi does not supports creating such OLEObjects in XWPF until now." Oh,It's sadness.So if a word has mathtype equation(OLEObject), the POI parse is one-way process, can't restore the word as original.right? I have text pic and wmf files, which are all parsed by POI. The requirement is to export word programaticly. If the mathtype equation(OLEObject) can be add to the new created word, users can use it more conveniently.To achieve this, is there any good modern ways to recommend? – five Jul 08 '17 at 09:34
  • As said you will need creating the special `XML` in `/word/document.xml`. Create a `*.docx` having `OLEObjects`of type `MathType 6.0 Equation` using `Word`. Those `*.docx` files are `ZIP`archives. Unzip it and have a look at `/word/document.xml`. There you will see what is needed. And you will need the `/word/embeddings/oleObject1.bin` also from that archive. This is the binary which reacts on the double click then. – Axel Richter Jul 08 '17 at 09:40
  • I understand a bit, and then I'll have it a try.Thanks a lot. – five Jul 08 '17 at 09:52
  • I tried it and unzip the word, but found it difficult to understatnd the xml.When parsing word, I googled and write test code a lot, finally found xwpfPictureData.getRelationId(xwpfPictureData.getParent()), and run.getCTR().toString(), in the xml, – five Jul 08 '17 at 14:35
  • Another thing is that I only have wmf or latex files, I wonder that if it is possible to create word file with correct mathtype OLEObject in this case.To be honest,I feel confused how to do next and almost want to give up.Hope some suggestions. – five Jul 08 '17 at 14:35

0 Answers0