1

I am trying to add a Wingdings symbol using the following code to the docx.

P symp = factory.createP();
R symr = factory.createR();
Sym sym = factory.createRSym();
sym.setFont("Wingdings");
sym.setChar("FOFC");
symr.getContent().add(sym);         
symp.getContent().add(symr);
mainPart.getContent().add(symp);

I get invalid content errors on opening the document. When I tried to add the symbols directly to a word docx, unzipped the docx and looked at the document.xml, I see the paragraph has rsidR and rsidDefault attributes. When I read about these attributes from this link, How to generate RSID attributes correctly in Word .docx files using Apache POI?, I see that they are random and only necessary to track changes in the document. So then, why does Microsoft word keeps expecting it and gives me the errors?

Any ideas/suggestions?

Community
  • 1
  • 1
Artin
  • 745
  • 1
  • 7
  • 14
  • As a heads-up, the docx corruption issue is to do with the code adding the `Sym` attribute, rather than the `RSID` stuff. – Ben Apr 22 '14 at 10:53

1 Answers1

3

I wonder whether Sym support is in docx4j in the way you expect.

I tried your code and got the same issue, but I must confess to not having investigated symbols before. As an experiment, I added a symbol using the relevant “Insert” menu command in Word 2010, and then checked the resulting OpenXML—it’s really quite different to the mark-up expected when inserting an Sym element.

Rather than manipulating symbols, have you tried inserting the text directly instead? For example, this will insert a tick character (not sure if that’s what you’re after):

        P p = factory.createP();
        R r = factory.createR();
        RPr rpr = factory.createRPr();
        Text text = factory.createText();

        RFonts rfonts = factory.createRFonts();
        rfonts.setAscii("Wingdings");
        rfonts.setCs("Wingdings");
        rfonts.setHAnsi("Wingdings");
        rpr.setRFonts(rfonts);
        r.setRPr(rpr);

        text.setValue("\uF0FC");
        r.getContent().add(text);
        p.getContent().add(r);
        mainPart.getContent().add(p);
Ben
  • 7,548
  • 31
  • 45
  • I got to give it to you. I tried this code snippet, but added the symbol unicode from the code I pasted in my question. Looks like I have FOFC instead of F0FC. :( IDE immediately complained about invalid unicode. Thanks for ending my agony. :D – Artin Apr 22 '14 at 17:43
  • 1
    (Yes, I should have mentioned that `FOFC` was the key to the invalid content in the document.xml file). Glad you got somewhere with it! – Ben Apr 22 '14 at 20:21