Page 1 of 1

Best way to use styles and fonts from an existing document

PostPosted: Thu Jul 30, 2009 6:26 pm
by jbeltran
Hi all,

Quick question about best practices for dealing with styles. I have a "template" docx file that has all the styles I want to use. If I want to apply those styles from my template to a new document, whats the best way to go about doing that?

I know all the styles are stored in styles.xml. Is it simply a matter moving that template's styles.xml into the newly created document?

Thanks again!
Justin

Re: Best way to use styles and fonts from an existing document

PostPosted: Fri Jul 31, 2009 1:34 am
by jbeltran
Hi,

I used this code and it seems to work....

Code: Select all
//get styles from old document
WordprocessingMLPackage oldPkg = WordprocessingMLPackage.load(oldDoc);
Styles oldStyles = (Styles)oldPkg.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement();

//apply styles from old document to new document
WordprocessingMLPackage newPkg = WordprocessingMLPackage.createPackage();
StyleDefinitionsPart styleDefinitionsPart = new StyleDefinitionsPart();
styleDefinitionsPart.setPackage(newPkg);
styleDefinitionsPart.setJaxbElement(oldStyles);
newPkg.getMainDocumentPart().addTargetPart(styleDefinitionsPart);


Is this the best way to do this? At a high level, what does "setting a target part" actually mean in regards to the document itself?

Thanks!
Justin

Re: Best way to use styles and fonts from an existing document

PostPosted: Sun Aug 02, 2009 9:07 am
by jason
Yes, that's a good way to do it. If you don't want to have to load your old document first, you can always save styles.xml to the file system, and load it from there.

At a high level, what does "setting a target part" actually mean in regards to the document itself?


newPkg.getMainDocumentPart().addTargetPart(styleDefinitionsPart) will add the part to document.xml.rels (creating it if necessary), as per the OPC spec. It doesn't change document.xml at all.

When docx4j loads or saves a package, it walks the relationship tree to work out what to zip up /unzip.

Re: Best way to use styles and fonts from an existing document

PostPosted: Sun Aug 02, 2009 11:33 pm
by jbeltran
Hi Jason,

Thanks for the reply. When you say "always save styles.xml to the file system, and load it from there", what code should I used to actually load the "styles.xml" file?

Justin

Re: Best way to use styles and fonts from an existing document

PostPosted: Mon Aug 03, 2009 10:43 am
by jason
After getting oldStyles, thus:

Code: Select all
      java.io.InputStream is = org.docx4j.utils.ResourceUtils.getResource("org/docx4j/openpackaging/parts/WordprocessingML/KnownStyles.xml");                  
         
      JAXBContext jc = Context.jc;
      Unmarshaller u = jc.createUnmarshaller();         
      u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());

      org.docx4j.wml.Styles oldStyles = (org.docx4j.wml.Styles)u.unmarshal( is );   



use the code you posted earlier in this thread to apply styles from old document to new document