我正在使用iTextSharp从特定矩形内的pdf中获取数据
在高度情况下获取的数据工作正常,但在宽度情况下,返回的是整行而不是矩形中的单词。
我使用的代码如下:
PdfReader reader = new PdfReader(Home.currentInstance.Get_PDF_URL());
iTextSharp.text.Rectangle pageRectangle = reader.GetPageSize(currentPage);
float selection_x = ((float)(selectionRectangle.RenderTransform.Value.OffsetX) / (float)canvas.Width) * pageRectangle.Width;
float selection_y = pageRectangle.Height - (((float)(selectionRectangle.RenderTransform.Value.OffsetY) / (float)canvas.Height) * pageRectangle.Height);
float selection_height = ((float)(selectionRectangle.Height) / (float)canvas.Height) * pageRectangle.Height;
float selection_width = ((float)(selectionRectangle.Width) / (float)canvas.Width) * pageRectangle.Width;
selection_y -= selection_height;
RectangleJ rect = new RectangleJ(selection_x,selection_y,selection_width,selection_height);
RenderFilter[] filter = { new RegionTextRenderFilter(rect) };
ITextExtractionStrategy strategy;
strategy = new FilteredTextRenderListener(
new LocationTextExtractionStrategy(), filter
);
String pageText = PdfTextExtractor.GetTextFromPage(reader, currentPage, strategy);
任何帮助将不胜感激。
提前致谢
最佳答案
最后,我能够解决问题
我创建了以下课程
public class LimitedTextStrategy : iTextSharp.text.pdf.parser.ITextExtractionStrategy
{
public readonly ITextExtractionStrategy textextractionstrategy;
public LimitedTextStrategy(ITextExtractionStrategy strategy)
{
this.textextractionstrategy = strategy;
}
public void RenderText(iTextSharp.text.pdf.parser.TextRenderInfo renderInfo)
{
foreach (TextRenderInfo info in renderInfo.GetCharacterRenderInfos())
{
this.textextractionstrategy.RenderText(info);
}
}
public string GetResultantText()
{
return this.textextractionstrategy.GetResultantText();
}
public void BeginTextBlock() {
this.textextractionstrategy.BeginTextBlock();
}
public void EndTextBlock() {
this.textextractionstrategy.EndTextBlock();
}
public void RenderImage(ImageRenderInfo renderInfo) {
this.textextractionstrategy.RenderImage(renderInfo);
}
}
然后将提取线更改为
String pageText = PdfTextExtractor.GetTextFromPage(reader, currentPage, new LimitedTextStrategy(strategy));
现在它工作正常。我希望它也能帮助其他人
关于c# - iTextSharp RegionTextRenderFilter在宽度情况下不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22110703/