Word2007 : Support for tab stops

This commit is contained in:
Progi1984 2013-12-15 14:35:18 +01:00
parent a610c34a18
commit 38a15e5da5
10 changed files with 332 additions and 33 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@
*.docx *.docx
*.rtf *.rtf
*.txt *.txt
*.xml

View File

@ -63,6 +63,12 @@ class PHPWord_Style_Paragraph {
*/ */
private $_spacing; private $_spacing;
/**
* Set of Custom Tab Stops
*
* @var array
*/
private $_tabs;
/** /**
* Indent by how much * Indent by how much
@ -80,6 +86,7 @@ class PHPWord_Style_Paragraph {
$this->_spaceBefore = null; $this->_spaceBefore = null;
$this->_spaceAfter = null; $this->_spaceAfter = null;
$this->_spacing = null; $this->_spacing = null;
$this->_tabs = null;
$this->_indent = null; $this->_indent = null;
} }
@ -90,11 +97,14 @@ class PHPWord_Style_Paragraph {
* @param mixed $value * @param mixed $value
*/ */
public function setStyleValue($key, $value) { public function setStyleValue($key, $value) {
if($key == '_indent') {
$value = (int)$value * 720; // 720 twips per indent
}
if($key == '_spacing') { if($key == '_spacing') {
$value += 240; // because line height of 1 matches 240 twips $value += 240; // because line height of 1 matches 240 twips
} }
if($key == '_indent') { if($key === '_tabs') {
$value = (int)$value * 720; // 720 twips per indent $value = new PHPWord_Style_Tabs($value);
} }
$this->$key = $value; $this->$key = $value;
} }
@ -202,5 +212,14 @@ class PHPWord_Style_Paragraph {
$this->_indent = $pValue; $this->_indent = $pValue;
return $this; return $this;
} }
/**
* Get tabs
*
* @return PHPWord_Style_Tabs
*/
public function getTabs() {
return $this->_tabs;
}
} }
?> ?>

View File

@ -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);
}
}
?>

View File

@ -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();
}
}
}
?>

View File

@ -114,10 +114,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$spaceAfter = $style->getSpaceAfter(); $spaceAfter = $style->getSpaceAfter();
$spacing = $style->getSpacing(); $spacing = $style->getSpacing();
$indent = $style->getIndent(); $indent = $style->getIndent();
$tabs = $style->getTabs();
if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) {
if(!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent)) {
if(!$withoutPPR) { if(!$withoutPPR) {
$objWriter->startElement('w:pPr'); $objWriter->startElement('w:pPr');
} }
@ -136,9 +135,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
if(!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) { if(!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) {
$objWriter->startElement('w:spacing'); $objWriter->startElement('w:spacing');
if(!is_null($spaceBefore)) { if(!is_null($spaceBefore)) {
$objWriter->writeAttribute('w:before', $spaceBefore); $objWriter->writeAttribute('w:before', $spaceBefore);
} }
@ -149,10 +146,13 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->writeAttribute('w:line', $spacing); $objWriter->writeAttribute('w:line', $spacing);
$objWriter->writeAttribute('w:lineRule', 'auto'); $objWriter->writeAttribute('w:lineRule', 'auto');
} }
$objWriter->endElement(); $objWriter->endElement();
} }
if(!is_null($tabs)) {
$tabs->toXml($objWriter);
}
if(!$withoutPPR) { if(!$withoutPPR) {
$objWriter->endElement(); // w:pPr $objWriter->endElement(); // w:pPr
} }

View File

@ -27,6 +27,7 @@ Fixed in branch for release 0.7 :
- Feature: (Progi1984) GH-1 - Implement RTF Writer - Feature: (Progi1984) GH-1 - Implement RTF Writer
- Feature: (Progi1984) GH-2 - Implement ODT Writer - Feature: (Progi1984) GH-2 - Implement ODT Writer
- Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells - Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells
- Feature: (RLovelett) - Word2007 : Support for tab stops
- General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer - General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer
- General: (deds) - add indentation support to paragraphs - General: (deds) - add indentation support to paragraphs
- General: (Progi1984) GH-27 - Support for Composer - General: (Progi1984) GH-27 - Support for Composer

View File

@ -20,7 +20,7 @@
], ],
"require": { "require": {
"php": ">=5.2.0", "php": ">=5.3.0",
"ext-xml": "*" "ext-xml": "*"
}, },
"recommend": { "recommend": {

View File

@ -0,0 +1,64 @@
<?php
error_reporting(E_ALL);
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php';
// New Word Document
echo date('H:i:s') , ' Create new PHPWord object' , EOL;
$PHPWord = new PHPWord();
// Ads styles
$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
echo date('H:i:s') , ' Write to Word2007 format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__));
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
$objWriter->save(str_replace('.php', '.odt', __FILE__));
echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;
?>

View File

@ -11,7 +11,7 @@ $section = $PHPWord->createSection();
$section->addImage('_mars.jpg'); $section->addImage('_mars.jpg');
$section->addTextBreak(2); $section->addTextBreak(2);
$section->addImage('_earth.JPG', array('width'=>210, 'height'=>210, 'align'=>'center')); $section->addImage('_earth.jpg', array('width'=>210, 'height'=>210, 'align'=>'center'));
$section->addTextBreak(2); $section->addTextBreak(2);
$section->addImage('_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right')); $section->addImage('_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right'));

View File

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 37 KiB