[MSWordUtils] Adding images to Headers

Forge Component
(3)
Published on 5 Jul by Stuart Harris
3 votes
Published on 5 Jul by Stuart Harris

Hi,

I'm struggling a little to add images on a previously created template.

I successfully added it to my header with the action "MSWordInsertImageInHeader" but it appears after all the content of my header. It's possible to add some kind of vertical alignment or the feature to insert replacing some text of the header (this solution will allow us to set exactly the place where we want the image to appear).

I also tried to expose the action DocX_replaceTextWithImage from the extension but it only works on the document body.


Can you help me with my issue?

Hi Marco,

Thank you for a great question.

I have had a look at it, I can confirm the library does not appear to do what you need.

There are a few options, but its tricky because the underlying DocX library being a free library does not fully expose all functionality.  I am starting to consider swapping out the underlying library, I initially did not choose OpenXml because at the time it did not work with the latest versions of MSOffice, but I think it does now.  On another project I required much deeper functionality and swapped out DocX for Aspose Words, which worked beautifully, but that is a paid library.

My plan is to experiment with setting the placement of the picture so it does not interfere with text as well as inserting a table without borders in the header.

It would be a big help if you could upload a sample document with the formatting you are trying to achieve, so I can confirm whatever works will support your scenario.

I plan to have a look at it over the weekend, if that timing suits.

Kind regards,

Stuart

Hi Stuart,

You can use for example the template that i attached to this post.

On this template the idea is to replace the text "#photo" by an image, however if i use MSWordInsertImageInHeader i am only capable to add the image after the text "Header Content".

Hope that i was clear :)

Best regards,


Marco


Hi Marco,

Great thank you, I am looking into it now.

Kind regards,

Stuart

Hi again Marco,

Just a bit of an update on progress.

So good news and bad news.  The good news is I have it replacing images in the header correctly now.  It was not really replacing images in the proper location anywhere previously, just adding it to the end of the main "container".

The bad news is I have had to upgrade the DocX library from 1.3 to 1.7, but this version is a free for non-commercial only license. So tomorrow I will see if I can change it over to OpenXml before publishing a new version, as it is unlikely anyone using a forge component is doing non-commercial work.

Kind regards,

Stuart

Hi again Marco,

Not good news I am afraid.

I have investigated converting to OpenXml, however, OpenXml appears to only produce Office 2007 documents, which I recall is one of the reasons I created this plugin, instead of using the OfficeUtils plugin, so people could open later Office documents and save them as the same format (also because of performance, that library created a new document with each modification - it may be different now though). So that option is unfortunately out.

Otherwise using the later version of the DocX library is not an option because of the licensing restrictions on commercial usage.

I know this is not what you were hoping for and I apologise for the problems this will cause you.

The only options I can see are open the document with the image already present, which may not meet your needs, or try another free plugin like OfficeUtils (uses NPOI) or with a paid library like AsposeWords.

If you have any other suggestions, please let me know.  Otherwise thank you for raising the issue, and I hope you find a solution to your requirements.

Kind regards,

Stuart