将文本均匀分成2个链接框,而不会使一个框文本小于另一个

时间:2014-03-17 17:44:52

标签: php pdflib

我有一个问题,我有2个链接块。一旦有最多的字符(在缩小到适合之前),它就会移动到我的下一个链接块。问题是,如果有很多文本被推送到下一个块,它就变得非常小。有没有办法均匀地划分所有字符和每个框?即使收缩也没关系,只需要看起来一样。示例下面的图像和代码:

http://i.imgur.com/ytadphF.png

public function addTextToMultiBlock($text,$baseBlockName,$numberOfBlocks)
{
    $tf = 0;
    for ($i = 1; $i <= $numberOfBlocks; $i++)
    {
        $optlist ="encoding=unicode textflowhandle=" . $tf;
        $tf = $this->p->fill_textblock($this->page, $baseBlockName.$i, $text, $optlist);
        //Set text to null ( $tf handle holds extra text from now on )
        $text = null;
        if ($tf == 0) {
         trigger_error("Warning: " . $this->p->get_errmsg() . "\n");
         break;
        }
    $reason = (int) $this->p->info_textflow($tf, "returnreason");
    $result = $this->p->get_parameter("string",  $reason);
    //Break if all text is placed
     if ($result == "_stop")
        {
            $this->p->delete_textflow($tf);
           break;
        }
    }
}

//call below to block
    if(!empty($this->orderData->remarks))
    {
    $addRemarks.= $this->orderData->remarks;
    $helper->addTextToMultiBlock($this->orderData->remarks, 'info', 2);
    }
    else
    {
        //nothing
    }

2 个答案:

答案 0 :(得分:0)

我在想的是计算一下这些词:

<?php

// SET THE NUMBER OF BOXES YOU WANT
$boxes = 2;

// SAMPLE INPUT STRING
$string = 'Life it seems to fade away, drifting further every day.  Getting lost within myself.  Nothing matters no one else.';

// MATCH EACH WORD AND STORE IT INTO AN ARRAY
preg_match_all('/\S+/', $string, $matches);

// COUNT HOW MANY WORDS WE HAVE TOTAL
$word_count = count($matches[0]);

// DIVIDE THE TOTAL WORDS BY THE NUMBER OF BOXES
// TO FIND HOW MANY WORDS WE SHOULD HAVE IN EACH BOX
$words_per_box = round($word_count / $boxes);

// SPLIT THE ARRAY OF WORDS INTO CHUNKS BASED ON THE
// - NUMBER OF WORDS PER BOX THAT WE SHOULD HAVE   
$chunks = array_chunk($matches[0], $words_per_box);

// START OUTPUTTING THE TABLE
print '
    <table border=1 cellpadding=5 cellspacing=0>
    <tr>';

// LOOP THROUGH EACH CHUNK OF WORDS AND TURN IT BACK
// - INTO A STRING THAT WE CAN PRINT OUT
foreach ($chunks AS $word_block) {
    //PRINT BLOCK TEXT
    print '
      <td>'.implode(' ', $word_block).'</td>';  
}

// CLOSE OUT THE TABLE
print '
    </table>';

这输出以下内容:

<table border="1" cellpadding="5" cellspacing="0">
<tr>
  <td>Life it seems to fade away, drifting further every day.</td>
  <td>Getting lost within myself. Nothing matters no one else.</td>
</table>

这不是一个完美的解决方案,但希望它能让你非常接近。

答案 1 :(得分:0)

你应该检查块中的“fitmethod”属性。如果fitmethod设置为“auto”,则完整文本将适合文本块。

从PDFlib食谱样本中: http://www.pdflib.com/pdflib-cookbook/block-handling-and-pps/linked-textblocks/

在填写块之前请注意以下注释:

         * "fitmethod=clip" to clip the text when it doesn't fit completely
         * into the block while avoiding any text shrinking.