先获取poppler库,编译出lib与dll,配置好依赖环境,获取某页所有文本:

QList<QString> PDFkitEngine::GetText(int nPageNum)
 {
     QList<QString> lstText;    Poppler::Page* pPage = NULL;
     pPage = GetPage(nPageNum);
     if (pPage == nullptr)
     {
         return lstText;
     }    QList<Poppler::TextBox* > lstTexts = pPage->textList();
     if (lstTexts.count() == 0)
     {
         return lstText;
     }    for (int i = 0;i < lstTexts.count();i++) 
     {
         lstText.append(lstTexts.at(i)->text());
     }    return lstText;
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

输出到txt文本:

void PDFkitEngine::outputText(const QString& strfileName, QList<QString>& vecText)
 {
     QFile file(strfileName);
     if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
     {
         return;
     }    QTextStream textStream(&file);
     for (auto& _info : vecText)
     {
         textStream << _info << endl;
     }
     
     file.close();
 }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.


测试效果是可以将pdf文本全部读出来,并且成功的输出到文本文件,但是有点瑕疵是,pdf读出来的一行数据内容可能会分成2行或者多行,但是文本获取没有问题