【问题标题】:In PHPWord, Right and Left Align Words在 PHPWord 中,左右对齐单词
【发布时间】:2016-03-02 22:38:54
【问题描述】:

我想知道是否有左右对齐同一行上的某些文本的方法。因此,例如,在同一行中,简历的左侧对齐公司名称,右侧对齐日期。

我试图通过文本运行来执行此操作,但似乎不起作用。我知道我可以使用\t\t,但是左边的文本会是不同的长度,所以标签会很不一致。

这只是一个简单的例子:

$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText("Left Text", array(), array("align" => "left"));
$textrun->addText("Right Text", array(), array("align" => "right"));

【问题讨论】:

  • 如何在 MS Word 中设置单个单词的对齐方式?据我所知,您只能在段落上设置对齐,而不是在一行中的单词上设置对齐

标签: php phpword


【解决方案1】:

您可以使用带有单个自定义制表位的段落样式来实现在同一行文本上不同对齐的效果,与右边距右对齐。

$section = $phpWord->addSection();

$section_style = $section->getStyle();
$position =
    $section_style->getPageSizeW()
    - $section_style->getMarginRight()
    - $section_style->getMarginLeft();
$phpWord->addParagraphStyle("leftRight", array("tabs" => array(
    new \PhpOffice\PhpWord\Style\Tab("right", $position)
)));

$section->addText("Left Text\tRight Text", array(), "leftRight");

【讨论】:

  • 8500 看起来很神奇,所以我决定这样计算它:$section_style = $section->getStyle(); $position = $section_style->getPageSizeW() - $section_style->getMarginRight() - $section_style->getMarginLeft(); 这样正确的文本就会出现在页面的右边距。
【解决方案2】:

您应该这样做,只需定义样式并在 addTextRun 中作为参数发送。

   $phpWord->addParagraphStyle('pStyler', array('align' => 'right'));

   $textrun = $section->addTextRun('pStyler');

   $textrun->addText(htmlspecialchars("Some text here..."));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2013-05-14
    相关资源
    最近更新 更多