PHPWord之解析首行缩进字符数量

首先将word转化为xml,查看首行缩进几个字符是通过什么标签决定的。

经过观察,首行缩进解析为xml时的表现形式是中的

  1. 修改Reader\Word2007\AbstractPart.php
 protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode)
    {
        if (!$xmlReader->elementExists('w:pPr', $domNode)) {
            return null;
        }

        $styleNode = $xmlReader->getElement('w:pPr', $domNode);
        $styleDefs = array(
            // ......
            // 解析首行缩进的值
            'firstLineChars'      => array(self::READ_VALUE, 'w:ind', 'w:firstLineChars'), 
            // ......
        );
        return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
    }

  1. 修改Style\Paragraph.php

namespace PhpOffice\PhpWord\Style;

class Paragraph extends Border
{
	// ...
	/**
     * 缩进字符
     *
     * @var integer
     * @Author 3273 [email protected]
     * @DateTime 2022-11-03
     */
    private $firstLineChars = 0; //新增首行缩进属性

	// ......
	// 修改方法
    public function getStyleValues()
    {
        $styles = array(
            // ...
            'indentation'         => $this->getIndentation(),
            // ------新增内容开始
            'firstLineChars'      => $this->getFirstLineChars(),
            // ------新增内容结束
            'spacing'             => $this->getSpace(),
           // ...
        );
        return $styles;
    }
	// 新增方法
    /**
     * Get getFirstLineChars
     *
     * firstLineChars 的首字母大写
     */
    public function getFirstLineChars()
    {
        return $this->firstLineChars;
    }
    /**
     * Set getFirstLineChars
     *
     * firstLineChars 的首字母大写
     */
    public function setFirstLineChars($num = 0)
    {
        $this->firstLineChars = $num;
    }
}
  1. 使用
		if ($element instanceof \PhpOffice\PhpWord\Element\TextRun) { //判断是否普通文本
            $pStyle = $element->getParagraphStyle();
            $firstLevel = $pStyle->getFirstLineChars();
            // 首行缩进的单位采用em
            $firstLevel > 0 && $pStyleString .= "text-indent:{$firstLevel}em;";
        }

撒花★,°:.☆( ̄▽ ̄)/$:.°★

你可能感兴趣的:(Laravel,PHP,php,PHPWord)