我已经阅读了所有相关的StackOverflow,并且尚未找到适当的解决方案。我想打开PDF文件,然后进一步获取文本(单词)及其坐标,为其中一些添加便条。c#itextsharp,在页面中找到不是块的词块,用于添加粘滞便笺的位置
似乎是不可能的任务,我被困住了。
这段代码如何正确找到页面中的所有单词(但不是它们的坐标)?
using (PdfReader reader = new PdfReader(path))
{
StringBuilder sb = new StringBuilder();
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
for (int page = 5; page <= 5; page++)
{
string text = PdfTextExtractor.GetTextFromPage(reader, page, strategy);
Console.WriteLine(text);
}
//txt = sb.ToString();
}
但是这一次得到的坐标,但对“块”不能依靠他们在正确的顺序。
PdfReader reader = new PdfReader(path);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
LocationTextExtractionStrategyEx strategy;
for (int i = 5; i <= 5; i++) // reader.NumberOfPages
{
//strategy = parser.ProcessContent(i, new SimpleTextExtractionStrategy());
// new MyLocationTextExtractionStrategy("sample", System.Globalization.CompareOptions.None)
strategy = parser.ProcessContent(i, new LocationTextExtractionStrategyEx("MCU_MOSI", 0));
foreach (LocationTextExtractionStrategyEx.ExtendedTextChunk chunk in strategy.m_DocChunks)
{
if (chunk.m_text.Trim() == "MCU_MOSI")
Console.WriteLine("Bingo"); // <-- NEVER HIT
}
//Console.WriteLine(strategy.m_SearchResultsList.ToString()); // strategy.GetResultantText() +
}
它使用一个类从这个职位(由我小修改) Getting Coordinates of string using ITextExtractionStrategy and LocationTextExtractionStrategy in Itextsharp
但只有找到无用的“豆腐块”。
所以问题是可以与iTextSharp真的在页面中找到单词,以便我可以在附近添加一些便签?谢谢。
块有位置和大小信息。因此,您可以对它们进行排序并根据需要推断空间。 – mkl