【问题标题】:iTextSharp unable to find text rotated at a specific angeliTextSharp 无法找到以特定角度旋转的文本
【发布时间】:2016-01-12 07:39:09
【问题描述】:

我正在使用以下类在 pdf 中查找文本位置:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using iTextSharp.text.pdf.parser;

namespace PdfTextHighlighter.Code
{
public class RectAndText
{
    public iTextSharp.text.Rectangle Rect;
    public String Text;

    public RectAndText(iTextSharp.text.Rectangle rect, String text)
    {
        this.Rect = rect;
        this.Text = text;
    }
}


public class MyLocationTextExtractionStrategy : LocationTextExtractionStrategy
{
    //Hold each coordinate
    public List<RectAndText> MyPoints = new List<RectAndText>();

    //The string that we're searching for
    public String TextToSearchFor { get; set; }

    //How to compare strings
    public System.Globalization.CompareOptions CompareOptions { get; set; }

    public MyLocationTextExtractionStrategy(String textToSearchFor,
        System.Globalization.CompareOptions compareOptions = System.Globalization.CompareOptions.None)
    {
        this.TextToSearchFor = textToSearchFor;
        this.CompareOptions = compareOptions;
    }

    //Automatically called for each chunk of text in the PDF
    public override void RenderText(TextRenderInfo renderInfo)
    {
        base.RenderText(renderInfo);

        //See if the current chunk contains the text
        var startPosition = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(
            renderInfo.GetText(), this.TextToSearchFor, this.CompareOptions);



        //If not found bail
        if (startPosition < 0)
        {
            return;
        }

        if (renderInfo.PdfString.ToString() != this.TextToSearchFor)
        {
            return;
        }

        //Grab the individual characters
        var chars =
            renderInfo.GetCharacterRenderInfos().Skip(startPosition).Take(this.TextToSearchFor.Length).ToList();



        //Grab the first and last character
        var firstChar = chars.First();
        var lastChar = chars.Last();


        //Get the bounding box for the chunk of text
        var bottomLeft = firstChar.GetDescentLine().GetStartPoint();
        var topRight = lastChar.GetAscentLine().GetEndPoint();

        //Create a rectangle from it
        var rect = new iTextSharp.text.Rectangle(
            bottomLeft[Vector.I1],
            bottomLeft[Vector.I2],
            topRight[Vector.I1],
            topRight[Vector.I2]
            );

        //Add this to our main collection
        this.MyPoints.Add(new RectAndText(rect, this.TextToSearchFor));
      }
     }
   }

它适用于水平或垂直文本。但是,它无法找到以一定角度旋转的文本。我该如何解决这个问题?

【问题讨论】:

  • 我很惊讶您的代码适用于水平或垂直的文本。您的检查 renderInfo.PdfString.ToString() != this.TextToSearchFor 期望立即呈现整个搜索文本,并且它很常见(在水平和垂直文本中也是如此)甚至将单词拆分为单独呈现的单独块。
  • 嗨 mkl,我在 PDF 中搜索的文本是电子元件名称,它们都是 pdf 中的一大块,如“C100”、“R501”等。也许这就是代码有效的原因。

标签: c# .net pdf-generation itextsharp


【解决方案1】:

当前开源版本的 iTextSharp 将无法做到这一点。如果您有许可证,您可以登录 itext 论坛并询问他们。我在我的项目中使用与上面相同的类,但在我的情况下,文本是水平的,所以我没有问题。

【讨论】:

    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 2020-04-24
    • 2016-09-06
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多