找出重叠的文字

时间:2018-02-15 13:45:06

标签: c# itext

我想突出显示PDF中的重叠文字。使用“PDFExchangeviewer.exe”获得所有单词坐标,然后转换为dot.net矩形。在dotnet矩形中找到矩形的交点并使用itextsharp突出显示相交的单词。但它突出显示PDF中不需要的单词而不是重叠文本。请将解决方案发布到此处。下面显示的是我在c#

中的代码部分
foreach (var pdfrect in WordpageCoordinates)
{
    float X = (float)pdfrect.Item1;
    float Y = reader.GetPageSize(pdfPg).Top - (float)pdfrect.Item4;
    float Width = (float)pdfrect.Item3 - X;
    float Height = (float)(pdfrect.Item4) - (float)(pdfrect.Item2);

    DotNetRect.Add(new System.Drawing.RectangleF(X, Y, Width, Height));
}
for(int j = 0; j < DotNetRect.Count; j++)
{
    System.Drawing.RectangleF MasterRect = DotNetRect[j];
    System.Drawing.RectangleF ChildRect = new System.Drawing.RectangleF();

    if (j == DotNetRect.Count - 1)
    {
        break;
    }
    for (int k = j + 1; k < DotNetRect.Count; k++)
    {
        ChildRect = DotNetRect[k];

        System.Drawing.RectangleF NewRect = new System.Drawing.RectangleF(ChildRect.X, ChildRect.Y , ChildRect.Width, ChildRect.Height);

        if (MasterRect.IntersectsWith(NewRect))
        {                           
            {
                iTextSharp.text.Rectangle Annotrect = new iTextSharp.text.Rectangle((float)WordpageCoordinates[k].Item1, (float)WordpageCoordinates[k].Item2, (float)WordpageCoordinates[k].Item3, (float)WordpageCoordinates[k].Item4);
                //iTextSharp.text.Rectangle Annotrect = new iTextSharp.text.Rectangle((float)ClsGlobal.TextCoordinatesList[k].Item1, (float)ClsGlobal.TextCoordinatesList[k].Item2, (float)ClsGlobal.TextCoordinatesList[k].Item3, (float)ClsGlobal.TextCoordinatesList[k].Item4);
                //float[] quad = { Annotrect.Left, Annotrect.Bottom, Annotrect.Right, Annotrect.Bottom, Annotrect.Left, Annotrect.Top, Annotrect.Right, Annotrect.Top };
                float[] quad = { Annotrect.Left, Annotrect.Top, Annotrect.Right, Annotrect.Top, Annotrect.Left, Annotrect.Bottom, Annotrect.Right, Annotrect.Bottom };
                PdfAnnotation HighlightAnnotation = PdfAnnotation.CreateMarkup(pdfstamper.Writer, Annotrect, "Text Overlap", PdfAnnotation.MARKUP_HIGHLIGHT, quad);
                HighlightAnnotation.Title = "Overlap Text Highlighter\n" + System.DateTime.Now.ToString();
                HighlightAnnotation.Color = iTextSharp.text.BaseColor.GREEN;
                pdfstamper.AddAnnotation(HighlightAnnotation, pdfPg);
            }
        }

    }
}

1 个答案:

答案 0 :(得分:2)

您正在测试2个矩形是否相交,但您没有使用交叉点。拿取注释的交集。交叉点是重叠的部分。

RectangleF intersection = RectangleF.Intersect(MasterRect, NewRect);

使用此静态方法GNU make guide

public static RectangleF Intersect(
    RectangleF a,
    RectangleF b
)

无需创建ChildRect的副本。 IntersectsWith仅测试交叉点但不创建交叉点并且不更改矩形。由于RectangleF是一个结构,因此是一个值类型,因此无论如何都会将它的副本传递给该方法。如果它是一个类,即一个引用类型,该方法理论上可以改变它的字段和属性。

如果没有交叉点,

Intersect会返回一个空矩形。因此,您也可以先创建交集,然后测试它是否为空,而不是使用IntersectsWith

RectangleF intersection = RectangleF.Intersect(MasterRect, ChildRect);
if (!intersection.IsEmpty)
{
    // create the annotation with `intersection`
}

而不是写

if (j == DotNetRect.Count - 1)
{
    break;
}

将循环条件更改为(添加- 1):

for(int j = 0; j < DotNetRect.Count - 1; j++)
相关问题