iTextSharp无法找到以特定天使旋转的文本

时间:2015-10-14 09:29:09

标签: c# .net pdf-generation itextsharp

我正在使用以下课程在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));
      }
     }
   }

它可以很好地处理水平或垂直文本。但是,它无法找到以某个角度旋转的文本。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

iTextSharp的当前开源版本将无法执行此操作。如果您有许可证,可以登录itext forums&amp;问问他们。我使用与上面相同的类用于我的项目,但在我的情况下,文本是水平的,所以我没有问题。