首页 > 解决方案 > How to add custom properties on docx files in ruby

问题描述

I am trying to figure out how to properly add custom properties to .docx files programmatically in ruby. I also have an office-js add in which reads these custom properties - but more on that later.

Here are the steps I am currently taking to add custom properties to a .docx file in ruby:

  1. Opening the document using the Docx gem and reading the contents of the archive using Docx:Document.open(filepath).zip
  2. Adding in a docProps/custom.xml file with the custom property elements which I want eg:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="matter.id">
    <vt:lpwstr>518</vt:lpwstr>
</property>
  1. Adding the following Override element to the [Content_Types].xml file:
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>
  1. Adding the following Relationship element to the _rels/.rels file:
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/>

Note: The Id attribute value is worked out by the number of <Relationship> elements present.

After these steps: After running this code over a blank / newly created document I can open the file in Word on my computer and my office-js add in can successfully read the custom properties which I have set in my ruby application.

The issue: When I upload & open the document in Word Online my office-js add in fails to recognised the custom properties which have been set.

My office-js add in reads and write the custom properties using Microsoft's office-js apis and when setting the properties this way my add in is able to recognise them in both Word and Word Online so I am pretty sure I'm missing something when trying to set the properties manually in my ruby function.

I've tried to compare the archives of both a .docx file which has had the custom properties set by my ruby function and the same file with the custom properties set using my add in and I just can't see any noticeable differences which would be causing Word Online to fail to recognise the custom properties set using my ruby code.

Unfortunately there doesn't seem to be any ruby gems which would allow me to edit the custom properties on a docx file like with the office-js apis and that is why I have resorted to doing it manually.

标签: rubyms-wordoffice365office-jsdocx

解决方案


推荐阅读