symbolsdocx4jwingdings

Adding symbols to word document using docx4j


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?


Solution

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