我已经在 Visual Studio 2022 中成功编译了 PoDoFo 0.10.3。 现在我想使用这个库从 PDF 文档中提取文本,但我在 API 上遇到了困难。 即使我找不到任何示例如何做到这一点...
void parseOneFile(const string_view& filename)
{
PdfMemDocument document;
document.Load(filename);
// iterate over all pages of the whole pdf document
for (int pn = 0; pn < document.GetPageCount(); ++pn)
{
PoDoFo::PdfPage* page = document.GetPage(pn);
// todo: ectract the text from the page
}
不幸的是上面的代码示例不起作用...... (PoDoFo::PdfMemDocument 类没有成员 GetPageCount)
有人知道如何做到这一点吗? 我只想提取文本并将其保存在像
std::vector<std::string>
这样的容器中以供进一步处理。
谢谢!
阅读 API 后,我能够编写以下代码行:
PdfMemDocument document;
document.Load(filename);
PoDoFo::PdfPageCollection& pagetree = document.GetPages();
for (int pn = 0; pn < pagetree.GetCount(); ++pn)
{
PdfPage& curPdfPage = pagetree.GetPageAt(pn);
PdfContents* pdfContent = curPdfPage.GetContents();
PdfObject oneObject = pdfContent->GetObject();
if (oneObject.IsArray())
{
PdfArray& array = oneObject.GetArray();
for (auto& element : array)
{
std::cout << element.ToString() << std::endl;
}
}
else if (oneObject.HasStream())
{
PdfObjectStream* stream = oneObject.GetStream();
}
else if (oneObject.IsDictionary())
{
PdfDictionary& dict = oneObject.GetDictionary();
}
但我不确定我是否走在正确的道路上...... 我仍然没有数据/文本(std::string 类型)。