Forum


Replies: 7   Views: 2411
Can't pass 'pstyle' from one wordfragment to another
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  · 04-09-2019 - 06:42

Hello,

The problem with your code is the same than the explained in our first reply:

If the first parameter of addText is an array (...) The method gets the inline contents of the WordFragments, so paragraph tags are removed

To accomplish that task you need to add the WordML of the WordFragments to avoid removing block tags such as paragraphs:

$text1 = new WordFragment($docx);                                       
$text1->addText('Text one', array('pStyle' => 'Titel2'));
$text2 = new WordFragment($docx);                                       
$text2->addText('Text two', array('pStyle' => 'Titel3'));
$text3 = new WordFragment($docx);                                       
$text3->addText('Text three', array('pStyle' => 'H2Selbstbestimmung', 'border' => 'single', 'borderWidth' => '24', 'borderColor' => 'FF0000'));

$text4_1 = new WordFragment($docx);
$text4_1->addText('Text 4_1, ');
$text4_2 = new WordFragment($docx);
$text4_2->addText(
    array(
        array(
           'text'  =>   'Text 4_2 ',
           'b'     =>   'on',
        )
    )
);

$text4_collect[1] = $text4_1;
$text4_collOpt[1] = array('pStyle' => 'H2Interessenvertretung');
$text4_collect[2] = $text4_2;
$text4_collOpt[2] = array('pStyle' => 'H2Selbstbestimmung');

$text4 = new WordFragment($docx);                                       
$text4->addText(array($text4_collect[1]), $text4_collOpt[1]);
$text4->addText('');
$text4->addText(array($text4_collect[2]), $text4_collOpt[2]);

$singleparts[1] = $text1;
$singleparts[2] = $text2;
$singleparts[3] = $text3;
$singleparts[4] = $text4;

ksort($singleparts, SORT_NUMERIC);

$wf_top = new WordFragment($docx);
foreach($singleparts as $idx => $sp) {
    $wf_top->addWordML((string)$sp);
}

$docx->replaceVariableByWordFragment(array('content' => $wf_top), array('type' => 'block'));

Or use DOCXPath to insert the whole WordFragments:

$text1 = new WordFragment($docx);                                       
$text1->addText('Text one', array('pStyle' => 'Titel2'));
$text2 = new WordFragment($docx);                                       
$text2->addText('Text two', array('pStyle' => 'Titel3'));
$text3 = new WordFragment($docx);                                       
$text3->addText('Text three', array('pStyle' => 'H2Selbstbestimmung', 'border' => 'single', 'borderWidth' => '24', 'borderColor' => 'FF0000'));

$text4_1 = new WordFragment($docx);
$text4_1->addText('Text 4_1, ');
$text4_2 = new WordFragment($docx);
$text4_2->addText(
    array(
        array(
           'text'  =>   'Text 4_2 ',
           'b'     =>   'on',
        )
    )
);

$text4_collect[1] = $text4_1;
$text4_collOpt[1] = array('pStyle' => 'H2Interessenvertretung');
$text4_collect[2] = $text4_2;
$text4_collOpt[2] = array('pStyle' => 'H2Selbstbestimmung');

$text4 = new WordFragment($docx);                                       
$text4->addText(array($text4_collect[1]), $text4_collOpt[1]);
$text4->addText('');
$text4->addText(array($text4_collect[2]), $text4_collOpt[2]);

$singleparts[1] = $text1;
$singleparts[2] = $text2;
$singleparts[3] = $text3;
$singleparts[4] = $text4;

ksort($singleparts, SORT_NUMERIC);
// insert in reverse order to keep the correct order after the chosen placeholder
$singleparts = array_reverse($singleparts);

$referenceNode = array(
    'type' => 'paragraph',
    'occurrence' => 1,
    'contains' => 'content',
);
foreach($singleparts as $idx => $sp) {
    $docx->insertWordFragment($sp, $referenceNode);
}

Or you could replace the placeholder with the first WordFragment adding a new placeholder at the end, then replace this new placeholder with the second WordFragment and so on.

For this kind of support we recommend opening a support ticket (https://www.phpdocx.com/support), so you can attach the template you are using and the dev team can generate a whole sample.

About is there a way to get a 'hard' line-break? Because addBreak(array('type' => 'line')); always makes a 'soft' line-break, hard line breaks are done using paragraphs (https://word.tips.net/T000170_Understanding_Hard_and_Soft_Returns.html), but, as your script does, if you add a WordFragment as an array in addText the paragraph tags are removed from the WordFragment. To add a hard line break you can use addText('').

Regards.