Page 1 of 1

Changing single character's font

PostPosted: Sat Feb 22, 2014 8:36 am
by lpeg
I'm sorry to ask about this in my first post, but I'm having a little trouble grasping the concepts involved in editing a docx file via docx4j.

I want to parse through a file, and for each character I want to assign a unique font. I do not know what font the character will have to begin with. I'm aware of the drawbacks to doing this.

I do not need an exact solution, but I do need some pointers:

How do I parse the file for characters?
How do I select individual characters and give them their own font properties?

Re: Changing single character's font

PostPosted: Sun Feb 23, 2014 3:23 am
by lpeg
So I've gotten a bit farther on my own now.

This is the basic gist of the parser that I've gotten so far. I think the clear first step is to remove paragraph properties that specify a font for the whole paragraph. This is not necessary, of course.

I can get the P, get its PPr, even set it to a new, blank PPr.
But how do I get the changes into the document?

Code: Select all
for (Object obj : pars){
         
           P par = (P)obj;
           PPr blank = Factory.createPPr();
           par.setPPr(blank);

Re: Changing single character's font

PostPosted: Sun Feb 23, 2014 4:54 am
by lpeg
Well thank you all for your considerable help. As you can see I am making significant progress, and unless anyone posts a reply, I will stop posting in this thread.

Code: Select all
      //Remove all font formatting
      //NOTE: The default font will still exist, however
      //      We are going to assign a value for fonts to
      //      Each character, and thus it does not matter.
      List<Object> pars = getAllElementsFromObject(documentPart, P.class);
      for (Object obj : pars){
         
           P par = (P)obj;
           PPr props = par.getPPr();
           if (props != null){
              ParaRPr runProps = props.getRPr();
              if (runProps != null){
                 runProps.setRFonts(null);
              }
           }
           List<Object> runs = par.getContent();
           for (Object obj1 : runs){
              
              R run = (R)obj1;
              RPr rprops = run.getRPr();
              if (rprops != null){
                 rprops.setRFonts(null);
              }
              
           }
          
      }

Re: Changing single character's font

PostPosted: Mon Feb 24, 2014 9:06 pm
by jason
Support here is "community support", not commercial support. Your posts were on the weekend, so its even less reasonable to expect rapid turn around.

Nor is sarcasm appreciated. Having said that, if you do post again, I will see it, and may choose to respond.

What you need to do is to take the existing run, get its text content, then assuming text length n, replace it with n runs, each containing a single character.