# 一般用法[](https://phpword.readthedocs.io/en/latest/general.html#general-usage "永久鏈接到這個標題")
## 基本的例子[](https://phpword.readthedocs.io/en/latest/general.html#basic-example "永久鏈接到這個標題")
以下是PHPWord庫的基本示例。[樣本文件夾](https://github.com/PHPOffice/PHPWord/tree/master/samples/)中提供了更多示例。
~~~
<?php
require_once 'bootstrap.php';
// 創建一個新文檔
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* Note: any element you append to a document must reside inside of a Section. */
// 添加一個章節至phpword文檔
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
);
/*
* Note: it's possible to customize font style of the Text element you add in three ways:
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
// Adding Text element with font customized inline...
$section->addText(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)',
array('name' => 'Tahoma', 'size' => 10)
);
// 添加自定義字體樣式文本元素
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)',
$fontStyleName
);
// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');
/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
~~~
## PHPWord設置[](https://phpword.readthedocs.io/en/latest/general.html#phpword-settings "永久鏈接到這個標題")
本`PhpOffice\PhpWord\Settings`類提供了一些選項,這將影響PHPWord的行為。以下是選項。
### XML Writer兼容性[](https://phpword.readthedocs.io/en/latest/general.html#xml-writer-compatibility "永久鏈接到這個標題")
此選項設置[XMLWriter :: setIndent](http://www.php.net/manual/en/function.xmlwriter-set-indent.php)和[XMLWriter :: setIndentString](http://www.php.net/manual/en/function.xmlwriter-set-indent-string.php)。此選項的默認值為`true`(兼容),這是[OpenOffice](https://github.com/PHPOffice/PHPWord/issues/103)正確呈現OOXML文檔所[必需的](https://github.com/PHPOffice/PHPWord/issues/103)。您可以`false`在開發期間將此選項設置為使得生成的XML文件打開更兼容。
~~~
\PhpOffice\PhpWord\Settings::setCompatibility(false);
~~~
### Zip類[](https://phpword.readthedocs.io/en/latest/general.html#zip-class "永久鏈接到這個標題")
默認情況下,PHPWord使用[Zip擴展](http://php.net/manual/en/book.zip.php)來處理ZIP壓縮存檔和其中的文件。如果您的服務器上沒有安裝Zip擴展,則可以使用[PHPWord](http://www.phpconcept.net/pclzip/)中包含的純PHP庫替代PclZip。
~~~
\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
~~~
### 輸出轉義[](https://phpword.readthedocs.io/en/latest/general.html#output-escaping "永久鏈接到這個標題")
編寫某些格式的文檔,尤其是基于XML的文檔,需要正確的輸出轉義。如果沒有它,當您在其中添加“&”符號,引號和其他字符時,您的文檔可能會被破壞。
轉義可以通過兩種方式執行:軟件開發人員在庫外部,內置機制在庫內部。默認情況下,禁用內置機制以向后兼容v0.13.0之前的版本。要在PHPWord配置文件中打開set`outputEscapingEnabled`選項,`true`或在運行時使用以下指令:
~~~
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
~~~
### 默認字體[](https://phpword.readthedocs.io/en/latest/general.html#default-font "永久鏈接到這個標題")
默認情況下,每個文本都顯示在Arial 10磅中。您可以使用以下兩個函數更改默認字體:
~~~
$phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontSize(12);
~~~
## 文件設定[](https://phpword.readthedocs.io/en/latest/general.html#document-settings "永久鏈接到這個標題")
可以使用設置生成的文檔的設置`$phpWord->getSettings()`
### 放大設定[](https://phpword.readthedocs.io/en/latest/general.html#magnification-setting "永久鏈接到這個標題")
默認縮放值為100%。這可以更改為另一個百分比
~~~
$phpWord->getSettings()->setZoom(75);
~~~
或預定義的值`fullPage`,`bestFit`,`textFit`
~~~
$phpWord->getSettings()->setZoom(Zoom::BEST_FIT);
~~~
### 鏡像頁邊距[](https://phpword.readthedocs.io/en/latest/general.html#mirroring-the-page-margins "永久鏈接到這個標題")
使用鏡像邊距為雙面文檔(如書籍或雜志)設置對開頁面。
~~~
$phpWord->getSettings()->setMirrorMargins(true);
~~~
### 拼寫和語法檢查[](https://phpword.readthedocs.io/en/latest/general.html#spelling-and-grammatical-checks "永久鏈接到這個標題")
默認情況下,只要打開word文檔,就會顯示拼寫和語法錯誤。對于大文檔,這可能會減慢文檔的打開速度。您可以隱藏拼寫和/或語法錯誤:
~~~
$phpWord->getSettings()->setHideGrammaticalErrors(true);
$phpWord->getSettings()->setHideSpellingErrors(true);
~~~
您還可以指定拼寫和語法檢查的狀態,標記拼寫或語法,因為臟將在打開文檔時強制重新檢查。
~~~
$proofState = new ProofState();
$proofState->setGrammar(ProofState::CLEAN);
$proofState->setSpelling(ProofState::DIRTY);
$phpWord->getSettings()->setProofState(proofState);
~~~
### 跟蹤修訂[](https://phpword.readthedocs.io/en/latest/general.html#track-revisions "永久鏈接到這個標題")
可以使用激活跟蹤更改`setTrackRevisions`,您可以進一步指定
* 不使用移動語法,而是移動的項目將被視為在一個地方刪除并添加到另一個地方
* 不跟蹤格式修訂
~~~
$phpWord->getSettings()->setTrackRevisions(true);
$phpWord->getSettings()->setDoNotTrackMoves(true);
$phpWord->getSettings()->setDoNotTrackFormatting(true);
~~~
### 十進制符號[](https://phpword.readthedocs.io/en/latest/general.html#decimal-symbol "永久鏈接到這個標題")
表示小數的默認符號是`.`英文。在法語中,您可能希望將其更改`,`為例如。
~~~
$phpWord->getSettings()->setDecimalSymbol(',');
~~~
### 文件語言[](https://phpword.readthedocs.io/en/latest/general.html#document-language "永久鏈接到這個標題")
可以使用以下內容更改文檔的默認語言。
~~~
$phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE));
~~~
`Language`有3個參數,一個用于拉丁語言,一個用于東亞語言,一個用于復雜(雙向)語言。`PhpOffice\PhpWord\ComplexType\Language`類中提供了幾種語言代碼,但可以使用任何有效的代碼/ ID。
如果您要生成RTF文檔,則需要以不同方式設置語言。
~~~
$lang = new Language();
$lang->setLangId(Language::EN_GB_ID);
$phpWord->getSettings()->setThemeFontLang($lang);
~~~
## 文件信息[](https://phpword.readthedocs.io/en/latest/general.html#document-information "永久鏈接到這個標題")
您可以設置文檔信息,例如標題,創建者和公司名稱。使用以下功能:
~~~
$properties = $phpWord->getDocInfo();
$properties->setCreator('My name');
$properties->setCompany('My factory');
$properties->setTitle('My title');
$properties->setDescription('My description');
$properties->setCategory('My category');
$properties->setLastModifiedBy('My name');
$properties->setCreated(mktime(0, 0, 0, 3, 12, 2014));
$properties->setModified(mktime(0, 0, 0, 3, 14, 2014));
$properties->setSubject('My subject');
$properties->setKeywords('my, key, word');
~~~
## 測量單位[](https://phpword.readthedocs.io/en/latest/general.html#measurement-units "永久鏈接到這個標題")
Open Office XML中的基本長度單位是twip。緹意味著“英寸點的第二十”,即1緹= 1/1440英寸。
您可以使用PHPWord輔助函數將英寸,厘米或點轉換為twip。
~~~
// Paragraph with 6 points space after
$phpWord->addParagraphStyle('My Style', array(
'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6))
);
$section = $phpWord->addSection();
$sectionStyle = $section->getStyle();
// half inch left margin
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
// 2 cm right margin
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
~~~
## 文件保護[](https://phpword.readthedocs.io/en/latest/general.html#document-protection "永久鏈接到這個標題")
文檔(或其中的一部分)可以受密碼保護。
~~~
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
$documentProtection->setEditing(DocProtect::READ_ONLY);
$documentProtection->setPassword('myPassword');
~~~
## 打開時自動重新計算字段[](https://phpword.readthedocs.io/en/latest/general.html#automatically-recalculate-fields-on-open "永久鏈接到這個標題")
要強制更新文檔中存在的字段,請將updateFields設置為true
~~~
$phpWord->getSettings()->setUpdateFields(true);
~~~
## 連字[](https://phpword.readthedocs.io/en/latest/general.html#hyphenation "永久鏈接到這個標題")
連字符描述了用連字符打破單詞的過程。有幾種控制連字符的選項。
### 自動連字[](https://phpword.readthedocs.io/en/latest/general.html#auto-hyphenation "永久鏈接到這個標題")
自動連接文本設置`autoHyphenation`為`true`。
~~~
$phpWord->getSettings()->setAutoHyphenation(true);
~~~
### 連續連字符限制[](https://phpword.readthedocs.io/en/latest/general.html#consecutive-hyphen-limit "永久鏈接到這個標題")
可以通過`consecutiveHyphenLimit`選項控制以連字符結尾的連續文本行的最大數量。如果未設置選項或提供的值,則沒有限制`0`。
~~~
$phpWord->getSettings()->setConsecutiveHyphenLimit(2);
~~~
### 連字區[](https://phpword.readthedocs.io/en/latest/general.html#hyphenation-zone "永久鏈接到這個標題")
連字符區域(以*twip為單位*)是在應用連字符之前允許的空白量。連字區越小,連字越多。或者換句話說,連字區域越寬,連詞越少。
~~~
$phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1));
~~~
### 連字帽[](https://phpword.readthedocs.io/en/latest/general.html#hyphenate-caps "永久鏈接到這個標題")
要控制是否所有大寫字母的單詞都應使用連字符,請使用doNotHyphenateCaps選項。
~~~
$phpWord->getSettings()->setDoNotHyphenateCaps(true);
~~~
[下一條【容器官方說明】](https://phpword.readthedocs.io/en/latest/containers.html "容器")[上一條【安裝/配置】](https://phpword.readthedocs.io/en/latest/installing.html "安裝/配置")