Forum


Replies: 5   Views: 2610
Issue with addtext(..) in addlist() item
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 dev@ram  · 28-03-2017 - 17:46

I think I am doing something wrong but not sure what it is.

I am trying to create a list with some items underlined. I followed 

the Example #3 in your API sample https://www.phpdocx.com/api-documentation/word-content/insert-list-nested-Word-document-with-PHP

I tried to include the underlined text but it screws up.

Please see my code


require_once SITE_ROOT . 'include/PHPDocx/classes/CreateDocx.inc';
  
    //create document from template
$docx = new CreateDocxFromTemplate(WORD_TEMPLATE_PATH.'radarWordTemplate_uk.docx');
$bullets = array();
foreach($data['bullets'] as $bullet){
    if(is_array($bullet['text'])){
    //Put underline for items in the special array
       $textData = new WordFragment($docx);
       $textData->addText(array('text' => $bullet['text']['data'], 'underline' => 'single'));
       $bullets[] = $textData;
       unset($textData);
//     $bullets[] = array('text' => $bullet['text']['data']);  
     }else{//normal text
       $bullets[] = array('text' => $bullet['text']); 
     }
}
$docx->addList($bullets);
$docId = sha1(microtime());
if(!mkdir(SITE_ROOT . 'docdump/' . $docId . '/')) exit;
$docfile = SITE_ROOT.'docdump/'. $docId . '/RAM';
$docx->createDocx($docfile);

If I uncomment and use $bullets[] = array('text' => $bullet['text']['data']); instead of the active code  it displays perfectly but when I use the current code inside the if() instead of the commented one(to make it underlined) the list is fully screwed up. any idea guys ???

just to make it easy this code works (but it does not underline the item which i need)

require_once SITE_ROOT . 'include/PHPDocx/classes/CreateDocx.inc';
  //create document from template
$docx = new CreateDocxFromTemplate(WORD_TEMPLATE_PATH.'radarWordTemplate_uk.docx');
$bullets = array();
foreach($data['bullets'] as $bullet){
    if(is_array($bullet['text'])){
    //Put underline for items in the special array
       //$textData = new WordFragment($docx);
      // $textData->addText(array('text' => $bullet['text']['data'], 'underline' => 'single'));
       //$bullets[] = $textData;
       //unset($textData);
     $bullets[] = array('text' => $bullet['text']['data']);  
     }else{//normal text
       $bullets[] = array('text' => $bullet['text']); 
     }
}
$docx->addList($bullets);
$docId = sha1(microtime());
if(!mkdir(SITE_ROOT . 'docdump/' . $docId . '/')) exit;
$docfile = SITE_ROOT.'docdump/'. $docId . '/RAM';
$docx->createDocx($docfile);

 

Posted by admin  · 28-03-2017 - 18:20

Hello,

You need to use a multidimensional array:

$textData->addText(array(array('text' => $bullet['text']['data'], 'underline' => 'single')));

Or a string:

$textData->addText($bullet['text']['data'], array('underline' => 'single'));

But not a single array as you are using to add text to the WordFragment. A multidimensional array is needed to be able to add texts with different properties:

$textData->addText(
  array(
    array('text' => $bullet['text']['data'], 'underline' => 'single'),
    array('text' => ' Other text', 'bold' => true),
  )
);

Regards.

Posted by dev@ram  · 03-04-2017 - 13:37

One last question,

If i am adding 

$docx->addList($bullets,3);

how do i increase linespacing of the list?

Posted by admin  · 03-04-2017 - 14:44

Hello,

You need to use a custom paragraph style or a custom style list imported from an existing DOCX , use an existing style in a template or create a custom paragraph style using createParagraphStyle.

This is a simple sample of a custom paragraph style created dynamically to set custom spacings:

// style options
$style = array(
    'lineSpacing' => 480,
    'spacingTop' => 360,
    'spacingBottom' => 360,
);
$docx->createParagraphStyle('myStyle', $style);

$item1 = new WordFragment($docx);
$item1->addText('Line 1', array('pStyle' => 'myStyle'));
$item2 = new WordFragment($docx);
$item2->addText('Line 2', array('pStyle' => 'myStyle'));
$item3 = new WordFragment($docx);
$item3->addText('Line 3', array('pStyle' => 'myStyle'));

$itemList = array(
    $item1,
    $item2,
    $item3,
);

$docx->addList($itemList, 1, array('useWordFragmentStyles' => true));

Regards.

Posted by dev@ram  · 03-04-2017 - 23:27

Thanks for your example but I am not able to get the importListStyle function to work. I created a new file called customStyles.docx and created a new list style within it called "RAMList1". However, whenever I call $docx->parseStyles I do not see the new style so I'm not able to obtain the id. Do you have an example of how to create a new style template containing custom List styles?

I then tried to use importStyles with the code below but this does not work either.

 

  $docx->importStyles(WORD_TEMPLATE_PATH.'customStyles.docx', 'merge', array('RAMList1'));
                    
  $itemList = array(
                    'Line 1',
                    'Line 2',
                    'Line 3',
                    'Line 4',
                    'Line 5'
    );

   $docx->addList($itemList, 'RAMList1');

Any suggestions?

I understand we can we use createParagraphStyle and then create a WordFragment with a 'pStyle' for every bullet but I'd like to avoid doing this as our arrays of data have already been built and we have a ton of different styles to apply within the document we are building. Creating and importing a document with custom styles would really suit our needs best.

Thanks in advance!

 

 

 

 

Posted by admin  · 04-04-2017 - 06:25

Hello,

In the examples folder included in the package of the license you can find examples of how to use importListStyle, importStyles and parseStyles methods. You can find them in the examples/LayoutAndGeneral folder.

To import a list style you need to use the importListStyle method (https://www.phpdocx.com/api-documentation/layout-and-general/import-list-style-from-a-Word-document-with-PHP). For example:

$docx->importListStyle('template.docx', '1', 'myliststyle');

$itemList = array(
    'Line 1',
    'Line 2',
    'Line 3',
    'Line 4',
    'Line 5'
);

$docx->addList($itemList, 'myliststyle');

If the style is not applied using your custom template, please check that you are setting the correct list ID.

Regards.