PDFTron:如何确定文本是否超出其绑定框

问题描述

我制作了一个包含多行文本字段的PDF文档。我想知道如何检查文本是否超出范围。

TextWidget::GetVisibleContentBox()

TextWidget::GetRect()

两者似乎都得到相同的Rect

好的。这是我经过稍微修改的函数,用于确定文本字段是否溢出。从TCPDF提取了部分逻辑:

function isTextWidgetOverflowing (PDFTron\TextWidget $annot,float $leading = 9.6) {
    $text = $annot->GetText();

    /** @var PDFTron\Font */
    $font = $annot->GetFont();

    /** @var PDFTron\Rect */
    $rect = $annot->GetRect();

    $length = strlen($text);

    $rectW = $rect->Width();

    $lastSeparator = -1;
    $lines = 1;
    $sum = 0;

    for ($index = 0; $index < $length; $index++) {
        $character = $text[$index];
        $charCode = mb_ord($character);

        if (!($chW = $font->GetWidth($charCode))) {
            $chW = $font->GetMissingWidth();
        }

        $chW /= 1000.0;

        if (($charCode !== 160)
                && (($charCode === 173)
                || preg_match('/[^\S\xa0]/',$character)
                || (($charCode === 45)
                    && ($index > 0) && ($index < ($length - 1))
                    && @preg_match('/[\p{L}]/',$text[$index - 1])
                    && @preg_match('/[\p{L}]/',$text[$index + 1])
                )
            )
        ) {
            $lastSeparator = $index;
        }

        if ((($sum + $chW) > $rectW) || ($charCode === 10)) {
            ++$lines;

            if ($charCode === 10) {
                $lastSeparator = -1;
                $sum = 0;
            } else if ($lastSeparator !== -1) {
                $index = $lastSeparator;
                $lastSeparator = -1;
                $sum = 0;
            } else {
                $sum = $chW;
            }
        } else {
            $sum += $chW;
        }
    }

    if (mb_ord($text[$length - 1]) === 10) {
        --$lines;
    }

    return ($lines * $leading) > $rect->Height();
}

$chW很可能有点错误。也许我需要考虑行距和字体拉伸。在哪里可以找到这些?

如果我能以某种方式抓取PDF文档,也可以不用硬编码$leading

解决方法

这是确定文本窗口小部件是否溢出文本的有效解决方案。我唯一想更改的就是不依赖参数进行引导。

function isTextWidgetOverflowing(PDFTron\TextWidget $annot,float $leading = 9.6) {
    $text = $annot->GetText();

    /** @var PDFTron\Font */
    $font = $annot->GetFont();

    $fontSize = $annot->GetFontSize();

    /** @var PDFTron\Rect */
    $rect = $annot->GetRect();

    $length = strlen($text);

    $rectW = $rect->Width();

    $lastSeparator = -1;
    $lines = 1;
    $sum = 0;

    for ($index = 0; $index < $length; $index++) {
        $char = $text[$index];
        $charCode = mb_ord($char);

        if (!($chW = $font->GetWidth($charCode))) {
            $chW = $font->GetMissingWidth();
        }

        $chW /= (1000.0 / $fontSize);

        if (($charCode !== 160)
            && (($charCode === 173)
                || preg_match('/[^\S\xa0]/',$char)
                || (($charCode === 45)
                    && ($index > 0) && ($index < ($length - 1))
                    && preg_match('/[\p{L}]/',$text[$index - 1])
                    && preg_match('/[\p{L}]/',$text[$index + 1])))
        ) {
            $lastSeparator = $index;
        }

        if ((($sum + $chW) > $rectW) || ($charCode === 10)) {
            ++$lines;

            if ($charCode === 10) {
                $lastSeparator = -1;
                $sum = 0;
            } else if ($lastSeparator !== -1) {
                $index = $lastSeparator;
                $lastSeparator = -1;
                $sum = 0;
            } else {
                $sum = $chW;
            }
        } else {
            $sum += $chW;
        }
    }

    if (mb_ord($text[$length - 1]) === 10) {
        --$lines;
    }

    return ($lines * $leading) > $rect->Height();
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...