(The open source Java library for manipulating docx files)
by saravanaprabhusr » Thu Dec 29, 2022 7:13 pm
Recently I added some code to add a web extension to the word docx file. But once the change is done. its not loading the add in automatically. I am trying to achieve the part mentioned in this document
https://learn.microsoft.com/en-us/office/dev/add-ins/develop/automatically-open-a-task-pane-with-a-document#use-open-xml-to-tag-the-document- Code: Select all
WebExtensionPart webExtensionPart = new WebExtensionPart(new PartName("/word/webextensions/webextension.xml"));
webExtensionPart.setPackage(opcPackage);
CTOsfWebExtension ctOsfWebExtension = new CTOsfWebExtension();
ctOsfWebExtension.setId("635BF0CD-42CC-4174-B8D2-6D375C9A759E");
CTOsfWebExtensionReference ctOsfWebExtensionReference = new CTOsfWebExtensionReference();
ctOsfWebExtensionReference.setId("WA104380862");
ctOsfWebExtensionReference.setVersion("1.5.0.0");
ctOsfWebExtensionReference.setStore("en-US");
ctOsfWebExtensionReference.setStoreType("OMEX");
ctOsfWebExtension.setReference(ctOsfWebExtensionReference);
CTOsfWebExtensionPropertyBag bag = new CTOsfWebExtensionPropertyBag();
CTOsfWebExtensionProperty prop = new CTOsfWebExtensionProperty();
prop.setName("Office.AutoShowTaskpaneWithDocument");
prop.setValue("true");
bag.getProperty().add(prop);
// bag.setProperty(prop); <-- no setter for properties available
ctOsfWebExtension.setProperties(bag);
webExtensionPart.setContents(ctOsfWebExtension);
CTOsfWebExtensionBindingList ctOsfWebExtensionBindingList = new CTOsfWebExtensionBindingList();
CTOsfWebExtensionBinding ctOsfWebExtensionBinding = new CTOsfWebExtensionBinding();
ctOsfWebExtensionBindingList.getBinding().add(ctOsfWebExtensionBinding);
ctOsfWebExtension.setBindings(ctOsfWebExtensionBindingList);
doc.addTargetPart(webExtensionPart);
addTaskPane(docxFile, opcPackage, doc, ctOsfWebExtension);
private void addTaskPane(File docxFile, OpcPackage opcPackage, WordprocessingMLPackage doc, CTOsfWebExtension ctOsfWebExtension) throws Docx4JException {
TaskpanesPart taskpanesPart = addWebTaskPaneContent();
taskpanesPart.setPackage(opcPackage);
CTBlip snapshot = new CTBlip();
ctOsfWebExtension.setSnapshot(snapshot);
doc.addTargetPart(taskpanesPart);
RelationshipsPart relationshipsPart = RelationshipsPart.createRelationshipsPartForPart(taskpanesPart);
Relationship relationship = new Relationship();
relationship.setId("Rb0860d496d59410f");
relationship.setTarget("/word/webextensions/webextension.xml");
relationship.setType("http://schemas.microsoft.com/office/2011/relationships/webextension");
relationship.setParent(relationshipsPart);
relationshipsPart.addRelationship(relationship);
doc.save(docxFile);
}
TaskpanesPart addWebTaskPaneContent() throws InvalidFormatException {
TaskpanesPart taskpanesPart = new TaskpanesPart(new PartName("/word/webextensions/taskpanes.xml"));
CTOsfTaskpane ctOsfTaskpane = new CTOsfTaskpane();
ctOsfTaskpane.setDockstate("right");
ctOsfTaskpane.setVisibility(true);
ctOsfTaskpane.setWidth(350);
ctOsfTaskpane.setRow(1);
CTWebExtensionPartRef ctWebExtensionPartRef = new CTWebExtensionPartRef();
ctWebExtensionPartRef.setId("Rb0860d496d59410f");
ctOsfTaskpane.setWebextensionref(ctWebExtensionPartRef);
CTOsfTaskpanes ctOsfTaskpanes = new CTOsfTaskpanes();
ctOsfTaskpanes.getTaskpane().add(ctOsfTaskpane);
taskpanesPart.setContents(ctOsfTaskpanes);
return taskpanesPart;
}
- Attachments
-
- test9468.docx
- Attached the docx file generated using docx4j
- (7.57 KiB) Downloaded 1435 times
-
saravanaprabhusr
-
- Posts: 1
- Joined: Thu Dec 29, 2022 7:07 pm
Return to docx4j
Who is online
Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 71 guests