Skip to content

Support for tab stops in Word 2007 documents #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

*.docx
*.xml
/.settings
/.buildpath
/.project
/docs
/docs
38 changes: 38 additions & 0 deletions src/Examples/TabStops.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

$PHPWord->addParagraphStyle('multipleTab', array(
'tabs' => array(
new PHPWord_Style_Tab("left", 1550),
new PHPWord_Style_Tab("center", 3200),
new PHPWord_Style_Tab("right", 5300)
)
));

$PHPWord->addParagraphStyle('rightTab', array(
'tabs' => array(
new PHPWord_Style_Tab("right", 9090)
)
));

$PHPWord->addParagraphStyle('centerTab', array(
'tabs' => array(
new PHPWord_Style_Tab("center", 4680)
)
));

// New portrait section
$section = $PHPWord->createSection();

// Add listitem elements
$section->addText("Multiple Tabs:\tOne\tTwo\tThree", NULL, 'multipleTab');
$section->addText("Left Aligned\tRight Aligned", NULL, 'rightTab');
$section->addText("\tCenter Aligned", NULL, 'centerTab');

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('TabStops.docx');
?>
File renamed without changes
21 changes: 19 additions & 2 deletions src/PHPWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ class PHPWord_Style_Paragraph {
* @var int
*/
private $_spacing;



/**
* Set of Custom Tab Stops
*
* @var array
*/
private $_tabs;

/**
* New Paragraph Style
*/
Expand All @@ -72,6 +78,7 @@ public function __construct() {
$this->_spaceBefore = null;
$this->_spaceAfter = null;
$this->_spacing = null;
$this->_tabs = null;
}

/**
Expand All @@ -83,6 +90,8 @@ public function __construct() {
public function setStyleValue($key, $value) {
if($key == '_spacing') {
$value += 240; // because line height of 1 matches 240 twips
} else if($key === '_tabs') {
$value = new PHPWord_Style_Tabs($value);
}
$this->$key = $value;
}
Expand Down Expand Up @@ -170,5 +179,13 @@ public function setSpacing($pValue = null) {
$this->_spacing = $pValue;
return $this;
}

/**
*
* @return PHPWord_Style_Tabs
*/
public function getTabs() {
return $this->_tabs;
}
}
?>
147 changes: 147 additions & 0 deletions src/PHPWord/Style/Tab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2011 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 010 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version {something}
*/

/**
* PHPWord_Style_Tabs
*
* @category PHPWord
* @package PHPWord_Style_Paragraph
* @copyright Copyright (c) 2011 PHPWord
* @link http://www.schemacentral.com/sc/ooxml/e-w_tab-1.html w:tab
*/
class PHPWord_Style_Tab {

/**
* Tab Stop Type
*
* @var string
*/
private $_val;

/**
* Tab Leader Character
*
* @var string
*/
private $_leader;

/**
* Tab Stop Position
*
* @var int
*/
private $_position;

/**
* Tab Stop Type
*
* @var array
* @link http://www.schemacentral.com/sc/ooxml/a-w_val-26.html Tab Stop Type
*/
private static $_possibleStopTypes = array(
'clear', // No Tab Stop
'left', // Left Tab Stop
'center', // Center Tab Stop
'right', // Right Tab Stop
'decimal', // Decimal Tab
'bar', // Bar Tab
'num' // List tab
);

/**
* Tab Leader Character
*
* @var array
* @link http://www.schemacentral.com/sc/ooxml/a-w_leader-1.html Tab Leader Character
*/
private static $_possibleLeaders = array(
'none', // No tab stop leader
'dot', // Dotted leader line
'hyphen', // Dashed tab stop leader line
'underscore', // Solid leader line
'heavy', // Heavy solid leader line
'middleDot' // Middle dot leader line
);

/**
* Create a new instance of PHPWord_Style_Tab. Both $val and $leader
* must conform to the values put forth in the schema. If they do not
* they will be changed to default values.
*
* @param string $val Defaults to 'clear' if value is not possible.
* @param int $position Must be an integer; otherwise defaults to 0.
* @param string $leader Defaults to NULL if value is not possible.
*/
public function __construct($val = NULL, $position = 0, $leader = NULL) {
// Default to clear if the stop type is not matched
$this->_val = (self::isStopType($val)) ? $val : 'clear';

// Default to 0 if the position is non-numeric
$this->_position = (is_numeric($position)) ? intval($position) : 0;

// Default to NULL if no tab leader
$this->_leader = (self::isLeaderType($leader)) ? $leader : NULL;
}

/**
* Creates the XML DOM for the instance of PHPWord_Style_Tab.
*
* @param PHPWord_Shared_XMLWriter $objWriter
*/
public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) {
if(isset($objWriter)) {
$objWriter->startElement("w:tab");
$objWriter->writeAttribute("w:val", $this->_val);
if(!is_null($this->_leader)) {
$objWriter->writeAttribute("w:leader", $this->_leader);
}
$objWriter->writeAttribute("w:pos", $this->_position);
$objWriter->endElement();
}
}

/**
* Test if attribute is a valid stop type.
*
* @param string $attribute
* @return bool True if it is; false otherwise.
*/
private static function isStopType($attribute) {
return in_array($attribute, self::$_possibleStopTypes);
}

/**
* Test if attribute is a valid leader type.
*
* @param string $attribute
* @return bool True if it is; false otherwise.
*/
private static function isLeaderType($attribute) {
return in_array($attribute, self::$_possibleLeaders);
}
}
?>
67 changes: 67 additions & 0 deletions src/PHPWord/Style/Tabs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* PHPWord
*
* Copyright (c) 2011 PHPWord
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPWord
* @package PHPWord
* @copyright Copyright (c) 010 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version {something}
*/

/**
* PHPWord_Style_Tabs
*
* @category PHPWord
* @package PHPWord_Style_Paragraph
* @copyright Copyright (c) 2011 PHPWord
* @link http://www.schemacentral.com/sc/ooxml/e-w_tabs-1.html w:tabs
*/
class PHPWord_Style_Tabs {

/**
* Tabs
*
* @var array
*/
private $_tabs;

/**
*
* @param array $tabs
*/
public function __construct(array $tabs) {
$this->_tabs = $tabs;
}

/**
*
* @param PHPWord_Shared_XMLWriter $objWriter
*/
public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) {
if(isset($objWriter)) {
$objWriter->startElement("w:tabs");
foreach ($this->_tabs as &$tab) {
$tab->toXml($objWriter);
}
$objWriter->endElement();
}
}
}
?>
7 changes: 6 additions & 1 deletion src/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = nu
$spaceBefore = $style->getSpaceBefore();
$spaceAfter = $style->getSpaceAfter();
$spacing = $style->getSpacing();
$tabs = $style->getTabs();


if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter)) {
if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($tabs)) {

if(!$withoutPPR) {
$objWriter->startElement('w:pPr');
Expand Down Expand Up @@ -145,6 +146,10 @@ protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = nu
$objWriter->endElement();
}

if(!is_null($tabs)) {
$tabs->toXml($objWriter);
}

if(!$withoutPPR) {
$objWriter->endElement(); // w:pPr
}
Expand Down