Page 1 of 1

ContentTypes

PostPosted: Fri Jan 02, 2009 1:40 pm
by jecmenb
Hello in new Year("2009") ;-)
If I need create an macro-enabled document, I do something like this:
Code: Select all
            WordprocessingMLPackage wmlPack = WordprocessingMLPackage.createPackage();
            wmlPack.getMainDocumentPart().setContentType(new ContentType(ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED));
            wmlPack.save(new File("withMacro.docm"));


My problem is, that in file [Content_Types].xml I expect this:
<Override PartName="/word/document.xml" ContentType="application/vnd.ms-word.document.macroEnabled.main+xml" />
, but in real I find there this:
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />


What I am doing wrong, please?

Thank you very much.

Re: ContentTypes

PostPosted: Sat Jan 03, 2009 1:51 pm
by jecmenb
I found another way, how change ContentType of the /word/document.xml:
Code: Select all
         ContentTypeManager ctm = wmlPack.getContentTypeManager();
         ctm.addOverrideContentType(new URI("/word/document.xml"), ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED)

where wmlPack is WordprocessingMLPackage. I don't know, if this way is right, but it works ;-) .

Re: ContentTypes

PostPosted: Tue Jan 06, 2009 6:38 am
by jason
Hi Bob

You worked it out :)

Here is the code I use to macro-enable an existing document:

Code: Select all
      // Change the Word document's content type!
      wordDocumentPart.setContentType( new org.docx4j.openpackaging.contenttype.ContentType(
            org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED ) );
      // .. but that's a dumb setter right now :(
      org.docx4j.openpackaging.contenttype.ContentTypeManager ctm
         = wordMLPackage.getContentTypeManager();
      org.docx4j.openpackaging.parts.PartName partName
         = wordDocumentPart.getPartName();
      
      ctm.removeContentType( partName  );
      ctm.addOverrideContentType( new java.net.URI("/word/document.xml"),
            org.docx4j.openpackaging.contenttype.ContentTypes.WORDPROCESSINGML_DOCUMENT_MACROENABLED);
   


Happy new year to you as well,

cheers

Jason