Page 1 of 1

Adding custom properties in docx file

PostPosted: Thu Jan 30, 2014 10:58 pm
by Amarendra Thakur
Hello Everyone,

I need to add some custom properties in docx file.Can some one tell me how to doit

My flows goes something like below

1 - export some content from webapp in a docx file
2 - add some custom meta data in this docx file
3 - upload this file sharepoint
4 - Sharepoint should be able to retrieve these metedata after uploading the file


Can someone tell me how add custom properties in docx file.Please see the how i have tried doing it.But it is adding the property in the docx advance property section which sharepoint is not able to retrive

Code: Select all
WordprocessingMLPackage wordMLPackage =
                 WordprocessingMLPackage.load(new java.io.File(Input file path));

             // Create a new Part
             org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart
                 = new org.docx4j.openpackaging.parts.DocPropsCustomPart();
            
             // Read the contents of the file into the part
             java.io.InputStream is = new java.io.FileInputStream("custom.xml");
             docPropsCustomPart.unmarshal(is);
             docPropsCustomPart.setProperty("Abstract", "nanotechnology");
             docPropsCustomPart.setProperty("Citation", "Medicine");   
             // Attach the part to the package   
             wordMLPackage.addTargetPart(docPropsCustomPart);
            wordMLPackage.getMainDocumentPart().addParagraphOfText("Testing");
            URI uri = new URI("/word/document.xml");
            wordMLPackage.getContentTypeManager().addOverrideContentType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
             wordMLPackage.save(new java.io.File(output file path));
             System.out.println("Docx created!!!");


and my customm.xml looks like the following

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<!--<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ContentTypeId"><vt:lpwstr>0x01010034D2D07E1C911441AAB1B507BCEE86AD</vt:lpwstr></property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="_dlc_policyId"><vt:lpwstr>0x0101|2132935403</vt:lpwstr></property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="ItemRetentionFormula"><vt:lpwstr>&lt;formula id="xyz.Collab.Policies.GlobalPolicy" /&gt;</vt:lpwstr></property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="6" name="RCSExpiration"><vt:lpwstr>2;#3|7ec9b844-99bd-4405-bc7a-8f0c53bd09c7</vt:lpwstr></property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="7" name="TaxKeyword"><vt:lpwstr>20;#COP|06824ca1-e94f-419c-9416-0ddfd763ff32;#21;#CO|2fa3969e-d791-4b56-9e30-515ec76c57a0</vt:lpwstr></property>-->
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="7" name="Abstract"><vt:lpwstr></vt:lpwstr></property>
</Properties>


But the code above adds the abstract and citation in advance property dialog box which sharepoint in not able to retrive.

Can someone suggest me how to achieve it?

Thanks & Regards

Amarendra Thakur

Re: Adding custom properties in docx file

PostPosted: Fri Jan 31, 2014 7:20 am
by jason
Please review the following:

docx-java-f6/docx-create-new-core-property-t1724.html

and we can take it from there...

Re: (Solved) Adding custom properties in docx file

PostPosted: Mon Feb 03, 2014 8:13 pm
by Amarendra Thakur
Thanks JSON,

I got it working!!

For the users in Forum, here it is how i achieved it
Code: Select all
         WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
             // Create a new Part
             org.docx4j.openpackaging.parts.DocPropsCustomPart docPropsCustomPart
                 = new org.docx4j.openpackaging.parts.DocPropsCustomPart();
            
             // Read the contents of the file into the part
             java.io.InputStream is = new java.io.FileInputStream("temp/custom.xml");
             docPropsCustomPart.unmarshal(is);
             docPropsCustomPart.setProperty("Technology", "Your value goes here");
             docPropsCustomPart.setProperty("Citation", "Your value goes here");   
             // Attach the part to the package
             wordMLPackage.addTargetPart(docPropsCustomPart);
            wordMLPackage.getMainDocumentPart().addParagraphOfText("your content goes here");
             wordMLPackage.save(new java.io.File("temp/result.docx"));
             System.out.println("Docx created with custom property created!!!");



and this is how my custom.xml looks like

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ContentTypeId"><vt:lpwstr>0x01010034D2D07E1C911441AAB1B507BCEE86AD</vt:lpwstr></property></Properties>


when you unzip the result.docx and see the custom.xml you will find your custom properties "Technology" and "Citation" written in it and in the docx file you will see these custom properties in the advance properties dialog box.

Many thanks

Amarendra Thakur