This project is read-only.

Adding Bold Styling and Italics

May 21, 2009 at 8:08 PM
Edited May 21, 2009 at 10:16 PM

After a bit of a struggle I figured out how to add a bold style. It may not be the most elegant solution, but it works.

Step 1: Create new file called OpenXML_WordPropertyElementBold.php in the WordElements folder.

Inside of that paste this code

class OpenXML_WordPropertyElementBold extends OpenXML_WordElementPropertyBase {
  public function __construct(OpenXML_WordElementPropertiesBase $parent) {
        parent::__construct($parent->ownerDocument, 'b');
    }

    public function SetBold() {
      $this->_SetBold();
    }

   protected function _SetBold() {
      return true;
   }

}

Step 2:Open the OpenXML_WordElementRunProperties.php file then add the following code:

const PROP_BOLD = 'b';

public function SetBold() {
     if(!$this->_PropertyExists(self::PROP_BOLD)) {
            $this->_SetComplexProperty(self::PROP_BOLD, new OpenXML_WordPropertyElementBold($this));
        }
        $this->_SetComplexProperty(self::PROP_BOLD, 'SetBold');

     return $this;
  }

 

Step 3: Use the new property like this: $doc->GetBody()->GetParagraph('main')->GetRun('main')->GetProperties()->SetBold();

 

 

For Italics, just rename the class and methods, and in the constructor, change the 'b' to an 'i'.