1、PHPWordDocs062PHPWord Beta 0.6.2Developers GuideTable of contentsWhat the hell are twips?Youll often find the unit twips in this documentation. Twips are the base length unit in Open Office. Twip means TWentieth of an Inch Point. So 1 twip = 1/1440 inch. Set default FontBy default every text appears
2、 in Arial font with font size of 10 point. You can alter the default font by using the following two functions:$PHPWord-setDefaultFontName(Tahoma);$PHPWord-setDefaultFontSize(12);Set document propertiesYou can set the following document properties:NameTypeCreatorStringCompanyStringTitleStringDescrip
3、tionStringCategoryStringLast modified byStringCreatedDatetimeModifiedDatetimeSubjectStringKeywordsStringTo set these properties use the following code:$properties = $PHPWord-getProperties();$properties-setCreator(My name); $properties-setCompany(My factory);$properties-setTitle(My title);$properties
4、-setDescription(My description); $properties-setCategory(My category);$properties-setLastModifiedBy(My name);$properties-setCreated( mktime(0, 0, 0, 3, 12, 2010) );$properties-setModified( mktime(0, 0, 0, 3, 14, 2010) );$properties-setSubject(My subject); $properties-setKeywords(my, key, word);Secti
5、onsAdd a sectionEvery visible element in word is placed inside of a section. To create a default section use the following code:$section = $PHPWord-createSection();The code above creates a default section (portrait oriented) with default margins.Style sectionsThere are two ways to style your section
6、s:Style with given array:$sectionStyle = array(orientation = null, marginLeft = 900, marginRight = 900, marginTop = 900, marginBottom = 900);$section = $PHPWord-createSection(sectionStyle);Style after creating the section:$section = $PHPWord-createSection();$sectionStyle = $section-getSettings();$se
7、ctionStyle-setLandscape();$sectionStyle-setPortrait();$sectionStyle-setMarginLeft(900);$sectionStyle-setMarginRight(900);$sectionStyle-setMarginTop(900);$sectionStyle-setMarginBottom(900);Available style propertiesProperties are all case sensitive !NameDescriptionOrientationPage orientation. Possibl
8、e values are:null (means portrait orientation)landscapemarginTopPage margin top in twips.marginLeftPage margin left in twips.marginRightPage margin right in twips.marginBottomPage margin bottom in twips.borderTopSizeBorder top size in twips.borderTopColorBorder top color in HEX.borderLeftSizeBorder
9、left size in twips.borderLeftColorBorder left color in HEX.borderRightSizeBorder right size in twips.borderRightColorBorder right color in HEX.borderBottomSizeBorder bottom size in twips.borderBottomColorBorder bottom color in HEX.The following two properties are automatically set by the use of the
10、orientation property. You can alter them but thats not recommended!NameDescriptionpageSizeWPage width in twips.pageSizeHPage height in twips.Text ElementsAdd text elementsTo add text elements to your word document use the function addText.$section-addText( $text, $fontStyle, $paragraphStyle );addTex
11、t()ParametersTypeDescription$textStringText that appears in the document.$fontStyleString / ArrayFont style of the text.$paragraphStyleString / ArrayParagraph style of the text.Add textrun elementsA textrun element can contain text or link elements. All elements are placed inside one paragraph with
12、the optionally given paragraph style. To add a textrun use the function createTextrun.createTextRun()ParametersTypeDescription$paragraphStyleString / ArrayParagraph style of the text.After creating the textrun you can add text or link elements with individual styles.$textrun = $section-createTextRun
13、();$textrun-addText(I am bold, array(bold=true); $textrun-addText(I am italic, array(italic=true);$textrun-addText(I am colored, array(color=AACC00);Of course you can use the pre defined paragraph or font styles. See chapter “Style text elements” for more information.Style text elementsThere are 2 o
14、ptions to style the inserted text elements:Inline Style:$fontStyle = array(color=006699, size=18, bold=true);$section-addText(helloWorld, $fontStyle);$text = $section-addText(helloWorld);$style = $text-getStyle();$style-setColor(006699);$style-setSize(18);$style-setBold();Or you can style your text
15、by using a style definition. After defining a style you have to set the second parameter to the name of your style. Use the function addFontStyle:$PHPWord-addFontStyle( $styleName, $fontStyle);addFontStyle()ParametersTypeDescription$styleNameStringName of the font style.$fontStyleArrayFont style.Exa
16、mple:$fontStyle = array(color=006699, size=18, bold=true);$PHPWord-addFontStyle(myOwnStyle, $fontStyle);$text = $section-addText(helloWorld, myOwnStyle);To add a paragraph style definition, use the function addParagraphStyle:addParagraphStyle()ParametersTypeDescription$styleNameStringName of the par
17、agraph style.$paragraphStyleArrayParagraph style.Available style propertiesProperties are all case sensitive !Font StyleNameDescriptionsizeFont size in points.nameFont familyboldBolditalicItalicsuperScriptSuperScriptsubScriptSubScriptunderlineUse the defined constants: PHPWord_Style_Font:UNDERLINE_.
18、ColorFont colorfgColorForeground color. You can only use the predefined constants:PHPWord_Style_Font:FGCOLOR_.Paragraph StyleNameDescriptionalignHorizontal alignment. Possible are:leftrightcenterboth / justify spaceBeforeTop margin of the text in twips.spaceAfterBottom margin of the text in twipsspa
19、cingMargin between the text lines in twips.Add text breaksTo add a text break use the function addTextBreak:$section-addTextBreak();If you need more than one text break you can set a multiple text break like this:$section-addTextBreak(15);The code above inserts 15 text breaks.Add page breaksTo add a
20、 manual page break you can use the function addPageBreak:$section-addPageBreak();List elementsAdd list elementsTo add a list item use the function addListItem:$section-addListItem( $text, $depth, $styleText, $styleList, $styleParagraph );addListItem()ParametersTypeDescription$textStringText that app
21、ears in the document.$depthIntegerDe$styleTextString / ArrayFont style of the current list element. See the chapter Style text elements for more information.$styleListArrayList style of the current element.$styleParagraphString / ArrayParagraph style of the current list element. See the chapter Styl
22、e text elements for more information.Style list elementsExample of styling list elements:$listStyle = array(listType = PHPWord_Style_ListItem:TYPE_NUMBER);$section-addListItem(Listitem 1, 0, null, $listStyle);Available list style propertiesProperties are all case sensitive !NameDescriptionlistTypeLi
23、st point style.Use the constants in PHPWord_Style_ListItem:TYPE_.HyperlinksAdd HyperlinksYou can add Hyperlinks to the document by using the function addLink:$section-addLink( $linkSrc, $linkName, $styleFont, $styleParagraph);addListItem()ParametersTypeDescription$linkSrcStringThe URL of the Link.$l
24、inkNameStringPlaceholder of the URL that appears in the document.$styleFontString / ArrayStyle of the Link. See the chapter Style text elements for more information.$styleParagraphString / ArrayParagraph style of the link. See the chapter Style text elements for more information.Style HyperlinksTher
25、e are two ways to style a hyperlink.Inline Style:$linkStyle = array(color=0000FF, underline=PHPWord_Style_Font:UNDERLINE_SINGLE);$section-addLink(http:/www.google.de, null, $linkStyle);Or you can style the hyperlink by using a style definition. After defining a style you have to set the third parame
26、ter to the name of your style.$linkStyle = array(color=0000FF, underline=PHPWord_Style_Font:UNDERLINE_SINGLE);$PHPWord-addLinkStyle(myHyperlinkStyle, $linkStyle);$section-addLink(http:/www.google.de, null, myHyperlinkStyle);addLinkStyle()ParametersTypeDescription$styleNameStringThe name of the Link
27、style.$stylesArrayLink style. All font style properties are possible. See the chapter Style text elements for more information.ImagesAdd imagesTo add an image use the function addImage:$section-addImage( $src, $style );addImage()ParametersTypeDescription$srcStringPath to the image on the web server.
28、 The path can be a relative or a absolute path.$styleArrayImage style.Style imagesTo style an image you can only use an array:$imageStyle = array(width=350, height=350, align=center);$section-addImage(EARTH.jpg, $imageStyle);Available style propertiesProperties are all case sensitive !NameDescriptio
29、nwidthImage width in pixelheightImage height in pixelalignImage alignment in the document. Possible are:leftrightcenterIf no width and height is given, PHPWord tries to detect the width / height properties by using the native PHP function getimagesize.PHPWord supports the image types gif, jpeg, png, bmp, tiff.Add memory imagesYou can also add images that have been created by GD. To add an GD image use the function addMemoryImage:$section-addMemoryImage( $link, $style );addMemoryImage()ParametersTypeDescription$linkStringThe first parameter indicates the source to the PHP File. I
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1