BOOL CTextEditorDoc::loadTxt()
{
if(m_strPDFPath.IsEmpty())
return FALSE;
#ifdef _DEBUG
DWORD dwTick = GetTickCount();
CString strLog;
#endif
CString strFile;
fz_context *ctx;
fz_document* doc;
fz_matrix ctm;
fz_page *page;
fz_device *dev;
fz_text_page *text;
fz_text_sheet *sheet;
int i,line,rotation,pagecount;
if(!gb2312toutf8(m_strPDFPath,strFile))
return FALSE;
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
fz_try(ctx){
doc = fz_open_document(ctx, strFile.GetBuffer(0));
}fz_catch(ctx){
fz_free_context(ctx);
return FALSE;
}
line = 0;
rotation = 0;
pagecount = 0;
pagecount = fz_count_pages(doc);
fz_rotate(&ctm, rotation);
fz_pre_scale(&ctm,1.0f,1.0f);
sheet = fz_new_text_sheet(ctx);
for(i=0;i<pagecount;i++){
page = fz_load_page(doc,i);
text = fz_new_text_page(ctx);
dev = fz_new_text_device(ctx, sheet, text);
#ifdef _DEBUG
dwTick = GetTickCount();
#endif
fz_run_page(doc, page, dev, &ctm, NULL);
#ifdef _DEBUG
strLog.Format("run page:%d ms\n",GetTickCount() - dwTick);
OutputDebugString(strLog);
dwTick = GetTickCount();
#endif
//m_linesInfoVector.push_back(line);
print_text_page(ctx,m_strContent,text,line);
#ifdef _DEBUG
strLog.Format("print text:%d ms\n",GetTickCount() - dwTick);
OutputDebugString(strLog);
dwTick = GetTickCount();
#endif
fz_free_device(dev);
fz_free_text_page(ctx,text);
fz_free_page(doc, page);
}
fz_free_text_sheet(ctx,sheet);
fz_close_document(doc);
fz_free_context(ctx);
return TRUE;
}
该代码可以提取PDF的所有文本,但可能太慢了。如何改进它? 大部分时间都花在函数
fz_run_page
中。也许只是从PDF中提取文本,我不需要执行
fz_run_page
?快速浏览您的代码看起来不错。
没有当前的用户维修方法来提高此速度。我们可能可以使用设备提示避免在以后的版本中读取图像等。我将对此思考并与其他开发人员进行讨论。但是现在你在做正确的事情。
Hth.
不,需要fz_run_page调用。您需要解释文档的页面以拔出文本,这就是fz_run_page所做的。
可能会创建一个更简单的文本设备,以避免跟踪角色位置,但我怀疑这会对性能产生真正的影响。
warning:这是一个非常旧的线程,不再可以以这种方式提取文本(例如,MUPDF不再导出)。我浪费了整整一个早晨,因为副驾驶员似乎在推荐具有相同性质的代码。
我正在尝试找到“现代”做到这一点(使用