Merge branch 'develop' of https://github.com/PHPOffice/PHPWord into develop

This commit is contained in:
Progi1984 2014-03-09 19:09:36 +01:00
commit 99b43ced29
18 changed files with 910 additions and 77 deletions

View File

@ -39,8 +39,23 @@ if (!defined('PHPWORD_BASE_PATH')) {
class PHPWord
{
/**
* Default font name (Arial)
*/
const DEFAULT_FONT_NAME = 'Arial';
const DEFAULT_FONT_SIZE = 20;
/**
* Default font size in points (10pt)
*
* OOXML defined font size values in halfpoints, i.e. twice of what PHPWord
* use, and the conversion will be conducted during XML writing.
*/
const DEFAULT_FONT_SIZE = 10;
/**
* Default font color (black)
*/
const DEFAULT_FONT_COLOR = '000000';
/**
* Document properties

View File

@ -128,7 +128,7 @@ class PHPWord_Shared_Drawing
public static function centimetersToPixels($pValue = 0)
{
if ($pValue != 0) {
return $pValue * 0.028;
return $pValue / 0.028;
} else {
return 0;
}

View File

@ -198,11 +198,6 @@ class PHPWord_Style_Cell
$this->_bgColor = $pValue;
}
public function setHeight($pValue = null)
{
$this->_height = $pValue;
}
public function setBorderSize($pValue = null)
{
$this->_borderTopSize = $pValue;

View File

@ -80,17 +80,82 @@ class PHPWord_Style_Font
*/
private $_paragraphStyle;
private $_size;
/**
* Font name
*
* @var int|float
*/
private $_name;
/**
* Font size
*
* @var int|float
*/
private $_size;
/**
* Bold
*
* @var bool
*/
private $_bold;
/**
* Italics
*
* @var bool
*/
private $_italic;
/**
* Superscript
*
* @var bool
*/
private $_superScript;
/**
* Subscript
*
* @var bool
*/
private $_subScript;
/**
* Underline mode
*
* @var string
*/
private $_underline;
/**
* Strikethrough
*
* @var bool
*/
private $_strikethrough;
/**
* Font color
*
* @var string
*/
private $_color;
/**
* Foreground/highlight
*
* @var string
*/
private $_fgColor;
/**
* New font style
*
* @param string $type Type of font
* @param array $styleParagraph Paragraph styles definition
*/
public function __construct($type = 'text', $styleParagraph = null)
{
$this->_type = $type;
@ -102,7 +167,7 @@ class PHPWord_Style_Font
$this->_subScript = false;
$this->_underline = PHPWord_Style_Font::UNDERLINE_NONE;
$this->_strikethrough = false;
$this->_color = '000000';
$this->_color = PHPWord::DEFAULT_FONT_COLOR;
$this->_fgColor = null;
if (!is_null($styleParagraph)) {
@ -119,78 +184,139 @@ class PHPWord_Style_Font
}
}
/**
* Set style value
*
* @param string $key
* @param mixed $value
*/
public function setStyleValue($key, $value)
{
$method = 'set' . substr($key, 1);
if (method_exists($this, $method)) {
$this->$method($value);
}
}
/**
* Get font name
*
* @return bool
*/
public function getName()
{
return $this->_name;
}
public function setStyleValue($key, $value)
{
if ($key == '_size') {
$value *= 2;
}
$this->$key = $value;
}
/**
* Set font name
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setName($pValue = PHPWord::DEFAULT_FONT_NAME)
{
if ($pValue == '') {
if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_NAME;
}
$this->_name = $pValue;
return $this;
}
/**
* Get font size
*
* @return int|float
*/
public function getSize()
{
return $this->_size;
}
/**
* Set font size
*
* @param int|float $pValue
* @return PHPWord_Style_Font
*/
public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE)
{
if ($pValue == '') {
if (!is_numeric($pValue)) {
$pValue = PHPWord::DEFAULT_FONT_SIZE;
}
$this->_size = ($pValue * 2);
$this->_size = $pValue;
return $this;
}
/**
* Get bold
*
* @return bool
*/
public function getBold()
{
return $this->_bold;
}
/**
* Set bold
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setBold($pValue = false)
{
if ($pValue == '') {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_bold = $pValue;
return $this;
}
/**
* Get italics
*
* @return bool
*/
public function getItalic()
{
return $this->_italic;
}
/**
* Set italics
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setItalic($pValue = false)
{
if ($pValue == '') {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_italic = $pValue;
return $this;
}
/**
* Get superscript
*
* @return bool
*/
public function getSuperScript()
{
return $this->_superScript;
}
/**
* Set superscript
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSuperScript($pValue = false)
{
if ($pValue == '') {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_superScript = $pValue;
@ -198,14 +324,25 @@ class PHPWord_Style_Font
return $this;
}
/**
* Get superscript
*
* @return bool
*/
public function getSubScript()
{
return $this->_subScript;
}
/**
* Set subscript
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setSubScript($pValue = false)
{
if ($pValue == '') {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_subScript = $pValue;
@ -213,11 +350,22 @@ class PHPWord_Style_Font
return $this;
}
/**
* Get underline
*
* @return string
*/
public function getUnderline()
{
return $this->_underline;
}
/**
* Set underline
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE)
{
if ($pValue == '') {
@ -227,49 +375,90 @@ class PHPWord_Style_Font
return $this;
}
/**
* Get strikethrough
*
* @return bool
*/
public function getStrikethrough()
{
return $this->_strikethrough;
}
/**
* Set strikethrough
*
* @param bool $pValue
* @return PHPWord_Style_Font
*/
public function setStrikethrough($pValue = false)
{
if ($pValue == '') {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_strikethrough = $pValue;
return $this;
}
/**
* Get font color
*
* @return string
*/
public function getColor()
{
return $this->_color;
}
public function setColor($pValue = '000000')
/**
* Set font color
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setColor($pValue = PHPWord::DEFAULT_FONT_COLOR)
{
if (is_null($pValue) || $pValue == '') {
$pValue = PHPWord::DEFAULT_FONT_COLOR;
}
$this->_color = $pValue;
return $this;
}
/**
* Get foreground/highlight color
*
* @return bool
*/
public function getFgColor()
{
return $this->_fgColor;
}
/**
* Set foreground/highlight color
*
* @param string $pValue
* @return PHPWord_Style_Font
*/
public function setFgColor($pValue = null)
{
$this->_fgColor = $pValue;
return $this;
}
/**
* Get style type
*
* @return string
*/
public function getStyleType()
{
return $this->_type;
}
/**
* Get Paragraph style
* Get paragraph style
*
* @return PHPWord_Style_Paragraph
*/

View File

@ -94,6 +94,34 @@ class PHPWord_Style_Paragraph
*/
private $_next;
/**
* Allow first/last line to display on a separate page
*
* @var bool
*/
private $_widowControl;
/**
* Keep paragraph with next paragraph
*
* @var bool
*/
private $_keepNext;
/**
* Keep all lines on one page
*
* @var bool
*/
private $_keepLines;
/**
* Start paragraph on next page
*
* @var bool
*/
private $_pageBreakBefore;
/**
* New Paragraph Style
*/
@ -108,6 +136,10 @@ class PHPWord_Style_Paragraph
$this->_hanging = null;
$this->_basedOn = 'Normal';
$this->_next = null;
$this->_widowControl = true;
$this->_keepNext = false;
$this->_keepLines = false;
$this->_pageBreakBefore = false;
}
/**
@ -118,19 +150,16 @@ class PHPWord_Style_Paragraph
*/
public function setStyleValue($key, $value)
{
if ($key == '_indent') {
$value = $value * 720; // 720 twips per indent
}
if ($key == '_hanging') {
if ($key == '_indent' || $key == '_hanging') {
$value = $value * 720;
}
if ($key == '_spacing') {
$value += 240; // because line height of 1 matches 240 twips
}
if ($key === '_tabs') {
$value = new PHPWord_Style_Tabs($value);
$method = 'set' . substr($key, 1);
if (method_exists($this, $method)) {
$this->$method($value);
}
$this->$key = $value;
}
/**
@ -279,6 +308,20 @@ class PHPWord_Style_Paragraph
return $this->_tabs;
}
/*
* Set tabs
*
* @param array $pValue
* @return PHPWord_Style_Paragraph
*/
public function setTabs($pValue = null)
{
if (is_array($pValue)) {
$this->_tabs = new PHPWord_Style_Tabs($pValue);
}
return $this;
}
/**
* Get parent style ID
*
@ -323,4 +366,104 @@ class PHPWord_Style_Paragraph
return $this;
}
/**
* Get allow first/last line to display on a separate page setting
*
* @return bool
*/
public function getWidowControl()
{
return $this->_widowControl;
}
/**
* Set keep paragraph with next paragraph setting
*
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setWidowControl($pValue = true)
{
if (!is_bool($pValue)) {
$pValue = true;
}
$this->_widowControl = $pValue;
return $this;
}
/**
* Get keep paragraph with next paragraph setting
*
* @return bool
*/
public function getKeepNext()
{
return $this->_keepNext;
}
/**
* Set keep paragraph with next paragraph setting
*
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepNext($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_keepNext = $pValue;
return $this;
}
/**
* Get keep all lines on one page setting
*
* @return bool
*/
public function getKeepLines()
{
return $this->_keepLines;
}
/**
* Set keep all lines on one page setting
*
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setKeepLines($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_keepLines = $pValue;
return $this;
}
/**
* Get start paragraph on next page setting
*
* @return bool
*/
public function getPageBreakBefore()
{
return $this->_pageBreakBefore;
}
/**
* Set start paragraph on next page setting
*
* @param bool $pValue
* @return PHPWord_Style_Paragraph
*/
public function setPageBreakBefore($pValue = false)
{
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_pageBreakBefore = $pValue;
return $this;
}
}

View File

@ -115,7 +115,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
$numPStyles++;
$pPHPWord->addParagraphStyle('P' . $numPStyles, array());
$element->setParagraph('P' . $numPStyles);
$element->setParagraphStyle('P' . $numPStyles);
}
}
}
@ -338,4 +338,15 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section)
{
}
/**
* Dummy function just to make all samples produce ODT
*
* @todo Create the real function
*/
private function _writeSection(
PHPWord_Shared_XMLWriter $objWriter = null,
PHPWord_Section $section)
{
}
}

View File

@ -133,16 +133,16 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
$objWriter->startElement('style:text-properties');
$objWriter->writeAttribute('style:use-window-font-color', 'true');
$objWriter->writeAttribute('style:font-name', PHPWord::DEFAULT_FONT_NAME);
$objWriter->writeAttribute('fo:font-size', '10pt');
$objWriter->writeAttribute('fo:font-size', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('fo:language', 'fr');
$objWriter->writeAttribute('fo:country', 'FR');
$objWriter->writeAttribute('style:letter-kerning', 'true');
$objWriter->writeAttribute('style:font-name-asian', 'Arial2');
$objWriter->writeAttribute('style:font-size-asian', '10pt');
$objWriter->writeAttribute('style:font-name-asian', PHPWord::DEFAULT_FONT_NAME . '2');
$objWriter->writeAttribute('style:font-size-asian', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('style:language-asian', 'zh');
$objWriter->writeAttribute('style:country-asian', 'CN');
$objWriter->writeAttribute('style:font-name-complex', 'Arial2');
$objWriter->writeAttribute('style:font-size-complex', '10pt');
$objWriter->writeAttribute('style:font-name-complex', PHPWord::DEFAULT_FONT_NAME . '2');
$objWriter->writeAttribute('style:font-size-complex', PHPWord::DEFAULT_FONT_SIZE . 'pt');
$objWriter->writeAttribute('style:language-complex', 'hi');
$objWriter->writeAttribute('style:country-complex', 'IN');
$objWriter->writeAttribute('fo:hyphenate', 'false');
@ -168,9 +168,9 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart
// style:text-properties
$objWriter->startElement('style:text-properties');
$objWriter->writeAttribute('fo:font-size', ($style->getSize() / 2) . 'pt');
$objWriter->writeAttribute('style:font-size-asian', ($style->getSize() / 2) . 'pt');
$objWriter->writeAttribute('style:font-size-complex', ($style->getSize() / 2) . 'pt');
$objWriter->writeAttribute('fo:font-size', ($style->getSize()) . 'pt');
$objWriter->writeAttribute('style:font-size-asian', ($style->getSize()) . 'pt');
$objWriter->writeAttribute('style:font-size-complex', ($style->getSize()) . 'pt');
if ($style->getItalic()) {
$objWriter->writeAttribute('fo:font-style', 'italic');
$objWriter->writeAttribute('style:font-style-asian', 'italic');

View File

@ -176,7 +176,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
// Point size (in half-points) above which to kern character pairs
$sRTFContent .= '\kerning1';
// Set the font size in half-points
$sRTFContent .= '\fs20';
$sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
// Body
$sRTFContent .= $this->_getDataContent();
@ -252,10 +252,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
if ($style instanceof PHPWord_Style_Font) {
$color = $style->getColor();
$fgcolor = $style->getFgColor();
if (in_array($color, $arrColors) == FALSE && $color != '000000' && !empty($color)) {
if (in_array($color, $arrColors) == FALSE && $color != PHPWord::DEFAULT_FONT_COLOR && !empty($color)) {
$arrColors[] = $color;
}
if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != '000000' && !empty($fgcolor)) {
if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != PHPWord::DEFAULT_FONT_COLOR && !empty($fgcolor)) {
$arrColors[] = $fgcolor;
}
}
@ -400,7 +400,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFText .= '\i';
}
if ($styleFont->getSize()) {
$sRTFText .= '\fs' . $styleFont->getSize();
$sRTFText .= '\fs' . ($styleFont->getSize() * 2);
}
}
if ($this->_lastParagraphStyle != '' || $styleFont) {
@ -419,7 +419,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFText .= '\i0';
}
if ($styleFont->getSize()) {
$sRTFText .= '\fs20';
$sRTFText .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2);
}
}

View File

@ -117,27 +117,47 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
protected function _writeParagraphStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Paragraph $style, $withoutPPR = false)
/**
* Write paragraph style
*
* @param PHPWord_Shared_XMLWriter $objWriter
* @param PHPWord_Style_Paragraph $style
* @param bool $withoutPPR
* @return void
*/
protected function _writeParagraphStyle(
PHPWord_Shared_XMLWriter $objWriter = null,
PHPWord_Style_Paragraph $style,
$withoutPPR = false)
{
$align = $style->getAlign();
$spacing = $style->getSpacing();
$spaceBefore = $style->getSpaceBefore();
$spaceAfter = $style->getSpaceAfter();
$spacing = $style->getSpacing();
$indent = $style->getIndent();
$hanging = $style->getHanging();
$tabs = $style->getTabs();
$widowControl = $style->getWidowControl();
$keepNext = $style->getKeepNext();
$keepLines = $style->getKeepLines();
$pageBreakBefore = $style->getPageBreakBefore();
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) || !is_null($hanging) ||
!is_null($tabs) || !is_null($widowControl) || !is_null($keepNext) ||
!is_null($keepLines) || !is_null($pageBreakBefore)) {
if (!$withoutPPR) {
$objWriter->startElement('w:pPr');
}
// Alignment
if (!is_null($align)) {
$objWriter->startElement('w:jc');
$objWriter->writeAttribute('w:val', $align);
$objWriter->endElement();
}
// Indentation
if (!is_null($indent) || !is_null($hanging)) {
$objWriter->startElement('w:ind');
$objWriter->writeAttribute('w:firstLine', 0);
@ -150,7 +170,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
if (!is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($spacing)) {
// Spacing
if (!is_null($spaceBefore) || !is_null($spaceAfter) ||
!is_null($spacing)) {
$objWriter->startElement('w:spacing');
if (!is_null($spaceBefore)) {
$objWriter->writeAttribute('w:before', $spaceBefore);
@ -165,6 +187,29 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
// Pagination
if (!$widowControl) {
$objWriter->startElement('w:widowControl');
$objWriter->writeAttribute('w:val', '0');
$objWriter->endElement();
}
if ($keepNext) {
$objWriter->startElement('w:keepNext');
$objWriter->writeAttribute('w:val', '1');
$objWriter->endElement();
}
if ($keepLines) {
$objWriter->startElement('w:keepLines');
$objWriter->writeAttribute('w:val', '1');
$objWriter->endElement();
}
if ($pageBreakBefore) {
$objWriter->startElement('w:pageBreakBefore');
$objWriter->writeAttribute('w:val', '1');
$objWriter->endElement();
}
// Tabs
if (!is_null($tabs)) {
$tabs->toXml($objWriter);
}
@ -328,7 +373,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$color = $style->getColor();
$size = $style->getSize();
$fgColor = $style->getFgColor();
$striketrough = $style->getStrikethrough();
$strikethrough = $style->getStrikethrough();
$underline = $style->getUnderline();
$superscript = $style->getSuperScript();
$subscript = $style->getSubScript();
@ -345,7 +390,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
}
// Color
if ($color != '000000') {
if ($color != PHPWord::DEFAULT_FONT_COLOR) {
$objWriter->startElement('w:color');
$objWriter->writeAttribute('w:val', $color);
$objWriter->endElement();
@ -354,10 +399,10 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
// Size
if ($size != PHPWord::DEFAULT_FONT_SIZE) {
$objWriter->startElement('w:sz');
$objWriter->writeAttribute('w:val', $size);
$objWriter->writeAttribute('w:val', $size * 2);
$objWriter->endElement();
$objWriter->startElement('w:szCs');
$objWriter->writeAttribute('w:val', $size);
$objWriter->writeAttribute('w:val', $size * 2);
$objWriter->endElement();
}
@ -379,8 +424,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
$objWriter->endElement();
}
// Striketrough
if ($striketrough) {
// Strikethrough
if ($strikethrough) {
$objWriter->writeElement('w:strike', null);
}

View File

@ -380,11 +380,11 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base
$objWriter->endElement();
$objWriter->startElement('w:sz');
$objWriter->writeAttribute('w:val', $fontSize);
$objWriter->writeAttribute('w:val', $fontSize * 2);
$objWriter->endElement();
$objWriter->startElement('w:szCs');
$objWriter->writeAttribute('w:val', $fontSize);
$objWriter->writeAttribute('w:val', $fontSize * 2);
$objWriter->endElement();
$objWriter->endElement();

View File

@ -34,11 +34,14 @@ the following lines to your ``composer.json``.
### Table of contents
1. [Basic usage](#basic-usage)
* [Measurement units](#measurement-units)
2. [Sections](#sections)
* [Change Section Page Numbering](#sections-page-numbering)
3. [Tables](#tables)
* [Section settings](#section-settings)
* [Section page numbering](#section-page-numbering)
3. [Texts](#texts)
4. [Tables](#tables)
* [Cell Style](#tables-cell-style)
4. [Images](#images)
5. [Images](#images)
<a name="basic-usage"></a>
#### Basic usage
@ -48,21 +51,25 @@ The following is a basic example of the PHPWord library.
```php
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
// Every element you want to append to the word document is placed in a section.
// To create a basic section:
$section = $PHPWord->createSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
$section->addText('Hello world! I am formatted.',
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');
// If you often need the same style again you can create a user defined style
// to the word document and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle',
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style',
'myOwnStyle');
// You can also putthe appended element to local object an call functions like this:
// You can also put the appended element to local object like this:
$fontStyle = new PHPWord_Style_Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
@ -70,11 +77,12 @@ $fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);
// At least write the document to webspace:
// Finally, write the document:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');
```
<a name="measurement-units"></a>
##### Measurement units
The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch.
@ -98,8 +106,51 @@ $sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
<a name="sections"></a>
#### Sections
<a name="sections-page-numbering"></a>
##### Change Section Page Numbering
Every visible element in word is placed inside of a section. To create a section, use the following code:
```php
$section = $PHPWord->createSection($sectionSettings);
```
The `$sectionSettings` is an optional associative array that sets the section. Example:
```php
$sectionSettings = array(
'orientation' => 'landscape',
'marginTop' => 600,
'colsNum' => 2,
);
```
<a name="section-settings"></a>
##### Section settings
Below are the available settings for section:
* `orientation` Page orientation, i.e. 'portrait' (default) or 'landscape'
* `marginTop` Page margin top in twips
* `marginLeft` Page margin left in twips
* `marginRight` Page margin right in twips
* `marginBottom` Page margin bottom in twips
* `borderTopSize` Border top size in twips
* `borderTopColor` Border top color
* `borderLeftSize` Border left size in twips
* `borderLeftColor` Border left color
* `borderRightSize` Border right size in twips
* `borderRightColor` Border right color
* `borderBottomSize` Border bottom size in twips
* `borderBottomColor` Border bottom color
* `headerHeight` Spacing to top of header
* `footerHeight` Spacing to bottom of footer
* `colsNum` Number of columns
* `colsSpace` Spacing between columns
* `breakType` Section break type (nextPage, nextColumn, continuous, evenPage, oddPage)
The following two settings are automatically set by the use of the `orientation` setting. You can alter them but that's not recommended.
* `pageSizeW` Page width in twips
* `pageSizeH` Page height in twips
<a name="section-page-numbering"></a>
##### Section page numbering
You can change a section page numbering.
@ -108,6 +159,27 @@ $section = $PHPWord->createSection();
$section->getSettings()->setPageNumberingStart(1);
```
<a name="texts"></a>
#### Texts
Text can be added by using `addText` and `createTextRun` method. `addText` is used for creating simple paragraphs that only contain texts with the same style. `createTextRun` is used for creating complex paragraphs that contain text with different style (some bold, other italics, etc) or other elements, e.g. images or links.
`addText` sample:
```php
$fontStyle = array('name' => 'Times New Roman', 'size' => 9);
$paragraphStyle = array('align' => 'both');
$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
```
`createTextRun` sample:
```php
$textrun = $section->createTextRun();
$textrun->addText('I am bold', array('bold' => true));
$textrun->addText('I am italic, array('italic' => true));
$textrun->addText('I am colored, array('color' => 'AACC00'));
```
<a name="tables"></a>
#### Tables

View File

@ -0,0 +1,80 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Cell;
/**
* Class PHPWord_Style_CellTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Style_CellTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style with normal value
*/
public function testSetGetNormal()
{
$object = new PHPWord_Style_Cell();
$attributes = array(
'valign' => 'left',
'textDirection' => PHPWord_Style_Cell::TEXT_DIR_BTLR,
'bgColor' => 'FFFF00',
'borderTopSize' => 120,
'borderTopColor' => 'FFFF00',
'borderLeftSize' => 120,
'borderLeftColor' => 'FFFF00',
'borderRightSize' => 120,
'borderRightColor' => 'FFFF00',
'borderBottomSize' => 120,
'borderBottomColor' => 'FFFF00',
'gridSpan' => 2,
'vMerge' => 2,
);
//'defaultBorderColor' => null,
foreach ($attributes as $key => $value) {
$set = "set{$key}";
$get = "get{$key}";
$object->$set($value);
$this->assertEquals($value, $object->$get());
}
}
/**
* Test border color
*/
public function testBorderColor()
{
$object = new PHPWord_Style_Cell();
$default = '000000';
$value = 'FF0000';
$this->assertEquals($default, $object->getDefaultBorderColor());
$object->setStyleValue('_defaultBorderColor', $value);
$this->assertEquals($value, $object->getDefaultBorderColor());
$object->setStyleValue('_borderColor', $value);
$expected = array($value, $value, $value, $value);
$this->assertEquals($expected, $object->getBorderColor());
}
/**
* Test border size
*/
public function testBorderSize()
{
$object = new PHPWord_Style_Cell();
$value = 120;
$expected = array($value, $value, $value, $value);
$object->setStyleValue('_borderSize', $value);
$this->assertEquals($expected, $object->getBorderSize());
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord;
use PHPWord_Style_Font;
/**
* Class PHPWord_Style_FontTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Style_FontTest extends \PHPUnit_Framework_TestCase
{
/**
* Test initiation for style type and paragraph style
*/
public function testInitiation()
{
$object = new PHPWord_Style_Font('text', array('align' => 'both'));
$this->assertEquals('text', $object->getStyleType());
$this->assertInstanceOf('PHPWord_Style_Paragraph', $object->getParagraphStyle());
}
/**
* Test setting style values with null or empty value
*/
public function testSetStyleValueWithNullOrEmpty()
{
$object = new PHPWord_Style_Font();
$attributes = array(
'name' => PHPWord::DEFAULT_FONT_NAME,
'size' => PHPWord::DEFAULT_FONT_SIZE,
'bold' => false,
'italic' => false,
'superScript' => false,
'subScript' => false,
'underline' => PHPWord_Style_Font::UNDERLINE_NONE,
'strikethrough' => false,
'color' => PHPWord::DEFAULT_FONT_COLOR,
'fgColor' => null,
);
foreach ($attributes as $key => $default) {
$get = "get{$key}";
$object->setStyleValue("_$key", null);
$this->assertEquals($default, $object->$get());
$object->setStyleValue("_$key", '');
$this->assertEquals($default, $object->$get());
}
}
/**
* Test setting style values with normal value
*/
public function testSetStyleValueNormal()
{
$object = new PHPWord_Style_Font();
$attributes = array(
'name' => 'Times New Roman',
'size' => 9,
'bold' => true,
'italic' => true,
'superScript' => true,
'subScript' => true,
'underline' => PHPWord_Style_Font::UNDERLINE_HEAVY,
'strikethrough' => true,
'color' => '999999',
'fgColor' => '999999',
);
foreach ($attributes as $key => $value) {
$get = "get{$key}";
$object->setStyleValue("_$key", $value);
$this->assertEquals($value, $object->$get());
}
}
}

View File

@ -0,0 +1,90 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Style_Paragraph;
use PHPWord_Style_Tab;
/**
* Class PHPWord_Style_ParagraphTest
*
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class PHPWord_Style_ParagraphTest extends \PHPUnit_Framework_TestCase
{
/**
* Test setting style values with null or empty value
*/
public function testSetStyleValueWithNullOrEmpty()
{
$object = new PHPWord_Style_Paragraph();
$attributes = array(
'tabs' => null,
'widowControl' => true,
'keepNext' => false,
'keepLines' => false,
'pageBreakBefore' => false,
);
foreach ($attributes as $key => $default) {
$get = "get{$key}";
$object->setStyleValue("_$key", null);
$this->assertEquals($default, $object->$get());
$object->setStyleValue("_$key", '');
$this->assertEquals($default, $object->$get());
}
}
/**
* Test setting style values with normal value
*/
public function testSetStyleValueNormal()
{
$object = new PHPWord_Style_Paragraph();
$attributes = array(
'align' => 'justify',
'spaceAfter' => 240,
'spaceBefore' => 240,
'indent' => 1,
'hanging' => 1,
'spacing' => 120,
'basedOn' => 'Normal',
'next' => 'Normal',
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
);
foreach ($attributes as $key => $value) {
$get = "get{$key}";
$object->setStyleValue("_$key", $value);
if ($key == 'align') {
if ($value == 'justify') {
$value = 'both';
}
} elseif ($key == 'indent' || $key == 'hanging') {
$value = $value * 720;
} elseif ($key == 'spacing') {
$value += 240;
}
$this->assertEquals($value, $object->$get());
}
}
/**
* Test tabs
*/
public function testTabs()
{
$object = new PHPWord_Style_Paragraph();
$object->setTabs(array(
new PHPWord_Style_Tab('left', 1550),
new PHPWord_Style_Tab('right', 5300),
));
$this->assertInstanceOf('PHPWord_Style_Tabs', $object->getTabs());
}
}

View File

@ -78,5 +78,35 @@ class PHPWord_Writer_Word2007_BaseTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(5, $element->getAttribute('w:val'));
}
}
/**
* Test write paragraph pagination
*/
public function testWriteParagraphStyle_Pagination()
{
// Create the doc
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$attributes = array(
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
);
foreach ($attributes as $attribute => $value) {
$section->addText('Test', null, array($attribute => $value));
}
$doc = TestHelperDOCX::getDocument($PHPWord);
// Test the attributes
$i = 0;
foreach ($attributes as $key => $value) {
$i++;
$path = "/w:document/w:body/w:p[{$i}]/w:pPr/w:{$key}";
$element = $doc->getElement($path);
$expected = $value ? 1 : 0;
$this->assertEquals($expected, $element->getAttribute('w:val'));
}
}
}

View File

@ -44,6 +44,11 @@ Changes in branch for release 0.7.1 :
- Feature: (ivanlanin) GH-87 - Paragraph: Ability to define parent style (basedOn) and style for following paragraph (next)
- Feature: (jeroenmoors) GH-44 GH-88 - Clone table rows on the fly when using a template document
- Feature: (deds) GH-16 - Initial addition of basic footnote support
- Feature: (ivanlanin) GH-92 - Paragraph: Ability to define paragraph pagination: widow control, keep next, keep lines, and page break before
- General: (ivanlanin) GH-93 - General: PHPWord_Style_Font refactoring
- General: (ivanlanin) GH-93 - Font: Use points instead of halfpoints internally. Conversion to halfpoints done during XML Writing.
- Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion
- Feature: (ivanlanin) - Paragraph: setTabs() function
- QA: (Progi1984) - UnitTests
Changes in branch for release 0.7.0 :

View File

@ -51,4 +51,4 @@ $document->setValue('userFirstName#3', 'Michael');
$document->setValue('userName#3', 'Ray');
$document->setValue('userPhone#3', '+1 428 889 775');
$document->save('Sample_03_TemplateCloneRow_result.docx');
$document->saveAs('Sample_07_TemplateCloneRow_result.docx');

View File

@ -0,0 +1,76 @@
<?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();
$PHPWord->setDefaultParagraphStyle(array(
'align' => 'both',
'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(12),
'spacing' => 120,
));
// Sample
$section = $PHPWord->createSection();
$section->addText('Below are the samples on how to control your paragraph ' .
'pagination. See "Line and Page Break" tab on paragraph properties ' .
'window to see the attribute set by these controls.',
array('bold' => true), null);
$section->addText('Paragraph with widowControl = false (default: true). ' .
'A "widow" is the last line of a paragraph printed by itself at the top ' .
'of a page. An "orphan" is the first line of a paragraph printed by ' .
'itself at the bottom of a page. Set this option to "false" if you want ' .
'to disable this automatic control.',
null, array('widowControl' => false));
$section->addText('Paragraph with keepNext = true (default: false). ' .
'"Keep with next" is used to prevent Word from inserting automatic page ' .
'breaks between paragraphs. Set this option to "true" if you do not want ' .
'your paragraph to be separated with the next paragraph.',
null, array('keepNext' => true));
$section->addText('Paragraph with keepLines = true (default: false). ' .
'"Keep lines together" will prevent Word from inserting an automatic page ' .
'break within a paragraph. Set this option to "true" if you do not want ' .
'all lines of your paragraph to be in the same page.',
null, array('keepLines' => true));
$section->addText('Keep scrolling. More below.');
$section->addText('Paragraph with pageBreakBefore = true (default: false). ' .
'Different with all other control above, "page break before" separates ' .
'your paragraph into the next page. This option is most useful for ' .
'heading styles.',
null, array('pageBreakBefore' => true));
// 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;