# **容器 Containers**
[TOC]
容器是可以放置元素(文本,列表,表格等)的對象。有3個主要容器,即部分,頁眉和頁腳。有3個元素也可以作為容器,即textrns,表格單元格和腳注。
## [章節 Sections](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#sections)
Every visible element in word is placed inside of a section. To create a section, use the following code:
單詞中的每個可見元素都放在一個部分內。要創建節,請使用以下代碼:
~~~
$section = $phpWord->addSection($sectionStyle);
~~~
`$sectionStyle`是一個可選的關聯數組,用于設置節。例:
~~~
$sectionStyle = array(
' orientation ' => ' landscape ',' marginTop ' => 600,' colsNum ' => 2,);
~~~
### [頁碼 page-number ](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#page-number)
您可以使用`pageNumberingStart`該部分的樣式更改部分頁碼。
~~~l
//方法1
$ section = $ phpWord - > addSection( array( 'pageNumberingStart ' => 1));
//方法2
$ section = $ phpWord - > addSection();
$ section - > getStyle() - > setPageNumberingStart( 1);
~~~
### [多列 multicolumn ](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#multicolumn)
您可以使用部分的樣式`breakType`和`colsNum`樣式將部分布局更改為多列(例如在報紙中)。
~~~
//方法1(Method 1)
$ section = $ phpWord - > addSection( array( ' breakType ' => ' continuous ', ' colsNum ' => 2));
//方法2 (Method 2)
$ section = $ phpWord - > addSection();
$ section - > getStyle() - > setBreakType( ' continuous ');
$ section - > getStyle() - > setColsNum( 2);
~~~
### [行號 line-numbering](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#line-numbering)
您可以使用該部分的`lineNumbering`樣式將行編號應用于部分。
~~~html
//方法1
$ section = $ phpWord - > addSection( array( ' lineNumbering ' => array()));
//方法2
$ section = $ phpWord - > addSection();
$ section - > getStyle() - > setLineNumbering( array());
~~~
以下是行編號樣式的屬性。
* `start`行號起始值
* `increment`行號增量
* `distance`文本和行號之間的距離以*緹為單位*
* `restart`行編號重啟設置連續| newPage | newSection
## [頭部 headers](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#headers)
每個部分都可以有自己的標題引用。要創建標頭,請使用以下`addHeader`方法:
~~~
$ header = $ section - > addHeader();
~~~
務必將結果保存在本地對象中。您可以使用頁腳可用的所有元素。有關詳細信息,請參閱“頁腳”部分。此外,只有標題參考內部您可以添加水印或背景圖片。請參閱“水印”部分。
您可以傳遞一個可選參數來指定應該應用頁眉/頁腳的位置,它可以是
* `Footer::AUTO`默認情況下,所有頁面除非被第一個或偶數覆蓋
* `Footer::FIRST`該部分的每個第一頁
* `Footer::EVEN`該部分的每個偶數頁面。只有在phpWord-> settings中將evenAndOddHeaders設置為true時才會應用
要更改evenAndOddHeaders,請使用該`getSettings`方法返回Settings對象,然后調用`setEvenAndOddHeaders`方法:
~~~
$ phpWord - > getSettings()- > setEvenAndOddHeaders(true);
~~~
## [頁腳 Footers](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#footers)
每個部分都有自己的頁腳引用。要創建頁腳,請使用以下`addFooter`方法:
~~~
$ footer = $ section - > addFooter();
~~~
確保將結果保存在本地對象中以向頁腳添加元素。您可以將以下元素添加到頁腳:
* 文本`addText`和`createTextrun`
* 文字休息
* 圖片
* 表
* 保留文字
有關每個元素的詳細信息,請參閱“元素”部分。
## [其他容器 other-containers](https://github.com/PHPOffice/PHPWord/blob/develop/docs/containers.rst#other-containers)
Textrns,表格單元格和腳注是也可以充當容器的元素。有關每個元素的詳細信息,請參見相應的“元素”部分。