Моя цель - нарисовать прямоугольник над искомым текстом.
Я уже реализовал класс LocationTextExtractionStrategy, который соединяет текстовые блоки в предложения (по одному на каждую строку) и возвращает начальное местоположение - X и Y.
Я использовал решение из: Получение координат строки с использованием ITextExtractionStrategy и LocationTextExtractionStrategy в Itextsharp , и вот что я получил до сих пор (ниже приведен код для организации чанков)
public override void RenderText(TextRenderInfo renderInfo)
{
LineSegment segment = renderInfo.GetBaseline();
if (renderInfo.GetRise() != 0)
{ // remove the rise from the baseline - we do this because the text from a super/subscript render operations should probably be considered as part of the baseline of the text the super/sub is relative to
Matrix riseOffsetTransform = new Matrix(0, -renderInfo.GetRise());
segment = segment.TransformBy(riseOffsetTransform);
}
TextChunk tc = new TextChunk(renderInfo.GetText(), tclStrat.CreateLocation(renderInfo, segment));
locationalResult.Add(tc);
}
public IList<TextLocation> GetLocations()
{
var filteredTextChunks = filterTextChunks(locationalResult, null);
filteredTextChunks.Sort();
TextChunk lastChunk = null;
var textLocations = new List<TextLocation>();
foreach (var chunk in filteredTextChunks)
{
if (lastChunk == null)
{
//initial
textLocations.Add(new TextLocation
{
Text = chunk.Text,
X = chunk.Location.StartLocation[0],
Y = chunk.Location.StartLocation[1]
});
}
else
{
if (chunk.SameLine(lastChunk))
{
var text = "";
// we only insert a blank space if the trailing character of the previous string wasn't a space, and the leading character of the current string isn't a space
if (IsChunkAtWordBoundary(chunk, lastChunk) && !StartsWithSpace(chunk.Text) && !EndsWithSpace(lastChunk.Text))
text += ' ';
text += chunk.Text;
textLocations[textLocations.Count - 1].Text += text;
}
else
{
textLocations.Add(new TextLocation
{
Text = chunk.Text,
X = chunk.Location.StartLocation[0],
Y = chunk.Location.StartLocation[1]
});
}
}
lastChunk = chunk;
}
//now find the location(s) with the given texts
return textLocations;
}
Когда я пытаюсь нарисовать прямоугольник в веревках текста, он даже близко не подходит к нему. Я рисую прямоугольник так:
PdfContentByte content = pdfStamper.GetOverContent(pageNumber);
iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(leftLowerX, leftLowerY, upperRightX, upperRightY);//pdfReader.GetPageSizeWithRotation(x);
rectangle.BackgroundColor = color;
content.Rectangle(rectangle);