我已經閱讀了所有相關的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