PHPWord小记之图片悬浮定位

1、图片缩进和绝对相对悬浮定位

/Style/image.php添加以下属性和方法

	// 图片缩进+绝对定位
	private $_indentLeft = null;
	private $_indentRight = null;
	private $_position = null;
	private $_top = null;
	private $_left = null;
	private $_zIndex = null;

    public function getIndentLeft()
    {
        return $this->_indentLeft;
    }
    
    public function setIndentLeft($pValue = null)
    {
        $this->_indentLeft = $pValue;
        return $this;
    }
    
    public function getIndentRight()
    {
        return $this->_indentRight;
    }
    
    public function setIndentRight($pValue = null)
    {
        $this->_indentRight = $pValue;
        return $this;
    }
    
    public function getPosition()
    {
        return $this->_position;
    }
    
    public function setPosition($pValue = null)
    {
        $this->_position = $pValue;
        return $this;
    }

    public function getTop()
    {
        return $this->_top;
    }

    public function getLeft()
    {
        return $this->_left;
    }

    public function setTop($pValue = null)
    {
        $this->_top = $pValue;
        return $this;
    }

    public function setLeft($pValue = null)
    {
        $this->_left = $pValue;
        return $this;
    }

    public function getZIndex()
    {
        return $this->_zIndex;
    }

    public function setZIndex($pValue = null)
    {
        $this->_zIndex = $pValue;
        return $this;
    }

base.php方法修改为如下方法

	protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image, $is_textrun = false) {
		$rId = $image->getRelationId();
		
		$style = $image->getStyle();
		$width = $style->getWidth();
		$height = $style->getHeight();
		$align = $style->getAlign();
		
		$indentLeft = $style->getIndentLeft();
		$indentRight = $style->getIndentRight();
		$position = $style->getPosition();
		$top = $style->getTop();
		$left = $style->getLeft(); 
		$zIndex = $style->getZIndex();
		
		if (!$is_textrun)
		{
		  $objWriter->startElement('w:p');
		}
			if(!is_null($align) || !is_null($indentLeft) || !is_null($indentRight)) {
				$objWriter->startElement('w:pPr');
				
				if (!is_null($align))
				{
					$objWriter->startElement('w:jc');
						$objWriter->writeAttribute('w:val', $align);
					$objWriter->endElement();  // w:jc
				}
				
				if (!is_null($indentLeft) || !is_null($indentRight))
				{
				    $objWriter->startElement('w:ind');
				    
				    if (!is_null($indentLeft))
				    {
				        $objWriter->writeAttribute('w:left', $indentLeft);
				    }
				    if (!is_null($indentRight))
				    {
				        $objWriter->writeAttribute('w:right', $indentRight);
				    }
				    $objWriter->endElement();   // w:ind
				}
				
				$objWriter->endElement();   // w:pPr
			}
		
			$objWriter->startElement('w:r');
			
				$objWriter->startElement('w:pict');
					
					$objWriter->startElement('v:shape');
						$objWriter->writeAttribute('type', '#_x0000_t75');
						if (!empty($position) && $top !== null && $left !== null && $zIndex !== null)
						{
						    $objWriter->writeAttribute('style',
						           'width:'.$width.'px;height:'.$height.'px;position:'.$position.';top:'.$top.'px;left:'.$left.'px;z-index:'.$zIndex.';');
						}
						else 
						{
						  $objWriter->writeAttribute('style', 'width:'.$width.'px;height:'.$height.'px');
						}
						
						$objWriter->startElement('v:imagedata');
							$objWriter->writeAttribute('r:id', 'rId'.$rId);
							$objWriter->writeAttribute('o:title', '');
						$objWriter->endElement();
					$objWriter->endElement();  // v:shape
					
				$objWriter->endElement();// w:pict
				
			$objWriter->endElement();    // w:r
		if (!$is_textrun)
		{	
		  $objWriter->endElement();         // w:p
		}
	}


同文件_writeTextRun方法中修改

		if(count($elements) > 0) {
			foreach($elements as $element) {
				if($element instanceof PHPWord_Section_Text) {
					$this->_writeText($objWriter, $element, true);
				} elseif($element instanceof PHPWord_Section_Link) {
					$this->_writeLink($objWriter, $element, true);
				} elseif ($element instanceof PHPWord_Section_Image) {
				    $this->_writeImage($objWriter, $element,true); 
				}
			}
		}

调用方法

    $tmp_textrun = $section->createTextRun(array('indentLeft' => 2600);
    $tmp_textrun->addText("段落", array('size' => 11), array('indentLeft' => 0));
    $tmp_textrun->addImage('./test.jpg', array('position' => 'absolute', 'top' => 0, 'left' => 25, 'zIndex' => 4));


2、使用PHPWord循环生成word文件样式bug问题。

在项目中需要循环生成不同的word文件,有个很奇怪的问题就是除了第一个生成的以外后面生成的word表格样式会有各种bug,和写完预期的不一样。

这是因为存储表格样式用的是一个静态变量,当重新实例化PHPWord时没有重置此变量引起的。通过以下代码可修复此问题,如果没有循环生成不同样式word需求可不做修改。

/PHPWord/Style.php添加一个静态变量和一个静态方法

    private static $_initElements = array();

    /**
     * clear the $_styleElements static property
     */
    public static function clearStyles() {
       //self::$_styleElements = array();     
       self::$_styleElements = self::$_initElements;  
    }

/PHPWord/PHPWord.php构造方法中添加一行

    PHPWord_Style::clearStyles();



版权声明:本文为yukirin_fans原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。