Forum


Replies: 3   Views: 1292
Using replacevariablebywordfragment() replaces the intial format from the word document
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 18-03-2021 - 15:24

Hello,

UPDATE: phpdocx 12 allows using the stylesReplacementType option in Premium licenses to mix existing and new styles in pPr (paragraph) and rPr (run-of-text) styles.

When you do an inline replacement using replaceVariableByWordFragment, the inline (character) styles of the placeholder are overwritten with the styles of the new content (paragraph styles remain when doing inline replacements; block replacements remove paragraph styles).

replaceVariableByText (https://www.phpdocx.com/api-documentation/templates/replace-variable-text-Word-document) doesn't overwrite placeholder styles but it doesn't allow applying new styles.

If you need to replace an inline placeholder keeping their existing inline styles and highlight it you can use the following approaches:

  •  Use searchAndHighlight to highlight the contents and then use replaceVariableByText to replace the placeholders by the new contents. This approach is only available in Advanced and Premium licenses.
  • Use DOCXCustomizer to change styles on-the-fly of the placeholders, adding the highlight style, and then use replaceVariableByText to replace the placeholdesr by the new contents. This approach is only available in Premium licenses.

The Basic license (and also the trial one) doesn't include a method to replace a text and add new styles keeping the existing inline styles. As explained, when using replaceVariableByWordFragment with an inline replacement, existing placeholder inline styles are removed.

Regards.