Forum


Replies: 3   Views: 724
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 philip0908  · 18-03-2021 - 14:50

Hello, 

I have a Word document with a couple of placeholders that I want to replace. Some of the placeholders are formatted, e.g. in bold, italic, and so on.

However, I wanted the replacements to be highlighted in yellow, so I can clearly see, where I need to doublecheck.

When I got a formatted document like this:

     This is a text with a $Variable$.

I want the outcome to be like this:

     This is a text with a replaced Text. (Also highlighted in yellow but this editor cannot do this).

However, the texts comes like this:

   This is a text with a replaced Text. (Also highlighted in yellow but this editor cannot do this).

 

My code:

...

$docx = new CreateDocxFromTemplate($document);

$word_fragment = new WordFragment($docx);

$word_fragment->addText("replaced Text", array("highlightColor" => "yellow")); 

$docx->replaceVariableByWordFragment(array("Variable" => $word_fragment), $options);

 

Any ideas/help?

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.

Posted by philip0908  · 18-03-2021 - 18:53

So I cannot test it in the trial version? DocxUtilities.php is not included, right? That's a shame. I need to get the thing going before I can tell my company to buy a licence.

Posted by admin  · 18-03-2021 - 20:13

Hello,

The trial package is based on the Basic license. Advanced and Premium licenses don't have a trial available.

As the trial package doesn't include DocxUtilities and DOCXCustomizer classes, we recommend you to check the samples available on the documentation pages. Although we guarantee that everything will work as explained on the documentation pages, in you send us the DOCX template you are using we'll generate two sample scripts (using DocxUtilities and DOCXCustomizer) that highlight and replace the placeholders, and we'll send them back with their outputs, so you can chek the output quality.

Regards.