Hi All,
I'm using docx4j to edit some docx properties and it all works fine except that when i open the document using Word 2007 i have to manualy update de fields.
Is there any way to prevent this behaviour?
Thanks in advance
Hugo Pais
hugopais wrote:when i open the document using Word 2007 i have to manualy update de fields.
package org.docx4j.samples;
import javax.xml.bind.JAXBContext;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class MacroEnable {
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String inputfilepath = "/home/dev/workspace/docx4all/sample-docs/earth.docx";
String outputfilepath = "/home/dev/docm.docm";
// Open a document from the file system
// 1. Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart wordDocumentPart = wordMLPackage.getMainDocumentPart();
// Get vbaProject.bin, and attach it to wordDocumentPart
java.io.InputStream is = new java.io.FileInputStream("/home/dev/vbaProject.bin" );
org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart vbaProject
= new org.docx4j.openpackaging.parts.WordprocessingML.VbaProjectBinaryPart();
vbaProject.setBinaryData(is);
wordDocumentPart.addTargetPart(vbaProject);
// Get /word/vbaData.xml, and attach it to vbaProject
org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart vbaData
= new org.docx4j.openpackaging.parts.WordprocessingML.VbaDataPart();
java.io.InputStream is2 = new java.io.FileInputStream("/home/dev/vbaData.xml" );
vbaData.setDocument( is2 );
vbaProject.addTargetPart( vbaData);
// 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 :(
// it doesn't actually make the change in ContentTypeManager. So do that:
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);
// Save it
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
}
<w:instrText xml:space="preserve"> FILENAME \* MERGEFORMAT </w:instrText>
Users browsing this forum: No registered users and 25 guests