我想知道是否有任何方法可以在同一行上左右对齐一些文本。例如,简历将在同一行上将公司名称左对齐,日期向右对齐。
我试图通过文本运行来做到这一点,但似乎不起作用。我知道我可以使用tt
但是左侧文本的长度会有所不同,因此选项卡将非常不一致。
这只是一个简单的例子:
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText("Left Text", array(), array("align" => "left"));
$textrun->addText("Right Text", array(), array("align" => "right"));
您可以使用具有单个自定义制表位的段落样式在同一行文本上实现不同对齐的效果,该样式与右边距右对齐。
$section = $phpWord->addSection();
$section_style = $section->getStyle();
$position =
$section_style->getPageSizeW()
- $section_style->getMarginRight()
- $section_style->getMarginLeft();
$phpWord->addParagraphStyle(
'leftRight',
['tabs' => [new PhpOfficePhpWordStyleTab('right', $position)]]);
$section->addText('Left TexttRight Text', [], 'leftRight');
你应该这样做,只需通过分开定义样式并在addTextRun中作为参数发送。
$phpWord->addParagraphStyle('pStyler', array('align' => 'right'));
$textrun = $section->addTextRun('pStyler');
$textrun->addText(htmlspecialchars("Some text here..."));