C++:PDF解析-->提取文本--> podofo-0.10.3

o8x7eapl  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(165)

我已经在Visual Studio 2022中成功编译了PoDoFo 0.10.3。现在我想使用这个库从PDF文档中提取文本,但我正在努力使用API。甚至我也找不到任何示例如何做到这一点。

  1. void parseOneFile(const string_view& filename)
  2. {
  3. PdfMemDocument document;
  4. document.Load(filename);
  5. // iterate over all pages of the whole pdf document
  6. for (int pn = 0; pn < document.GetPageCount(); ++pn)
  7. {
  8. PoDoFo::PdfPage* page = document.GetPage(pn);
  9. // todo: ectract the text from the page
  10. }

字符串
不幸的是,上面的代码示例不工作.(类PoDoFo::PdfMemDocument没有成员GetPageCount)
有人知道怎么做吗?我只想提取文本并保存到一个像std::vector<std::string>这样的容器中,以便进一步处理。
谢谢你,谢谢

z2acfund

z2acfund1#

在阅读了API之后,我能够编写以下代码行:

  1. PdfMemDocument document;
  2. document.Load(filename);
  3. PoDoFo::PdfPageCollection& pagetree = document.GetPages();
  4. for (int pn = 0; pn < pagetree.GetCount(); ++pn)
  5. {
  6. PdfPage& curPdfPage = pagetree.GetPageAt(pn);
  7. PdfContents* pdfContent = curPdfPage.GetContents();
  8. PdfObject oneObject = pdfContent->GetObject();
  9. if (oneObject.IsArray())
  10. {
  11. PdfArray& array = oneObject.GetArray();
  12. for (auto& element : array)
  13. {
  14. std::cout << element.ToString() << std::endl;
  15. }
  16. }
  17. else if (oneObject.HasStream())
  18. {
  19. PdfObjectStream* stream = oneObject.GetStream();
  20. }
  21. else if (oneObject.IsDictionary())
  22. {
  23. PdfDictionary& dict = oneObject.GetDictionary();
  24. }

字符串
但我不确定我是否走对了路.我仍然没有数据/文本(类型为std::string)。

展开查看全部

相关问题