MuPDF的使用
调用流程
// 获取上下文
fz_context context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
if (!context)
return;
// register the default file types
fz_register_document_handlers(context);
// open document 路径为utf-8的路径
fz_try(context)
{
fz_document document = fz_open_document(context, filePath.toUtf8().data());
}
获取目录
//把fz_output out改下你想要的行式就可以获取了,从中获取想要的标题与数据
static void do_debug_outline(fz_output *out, fz_outline outline, int level)
{
int i;
while (outline)
{
for (i = 0; i < level; i++)
printf(out, “\t”);
fz_printf(out, “%s\t%d\n”, outline->title, outline->dest.kind == FZ_LINK_GOTO ? outline->dest.ld.gotor.page + 1 : 0);
if (outline->down)
do_debug_outline(out, outline->down, level + 1);
outline = outline->next;
}
}
fz_outline outline = fz_load_outline(context, document);
do_debug_outline(out, outline, 0);
搜索关键字
旧的接口调用:
//查找V1的位置,返回所在坐标
static fz_rect search_hit_bbox[5000] = { 0 };
int aa = fz_search_page_number(context, document, 0, “V1”, search_hit_bbox, 5000);
新的接口调用:(参考源码的调用)
//第二个参数代表页数
fz_page page = fz_load_page(doc, 0);
fz_display_list list = NULL;
fz_device* dev = NULL;
fz_text_page* text = NULL;
fz_cookie cookie = {0};
static fz_text_sheet* sheet = NULL;
const fz_matrix fz_identity = {1, 0, 0, 1, 0, 0};
sheet = fz_new_text_sheet(ctx);
text = fz_new_text_page(ctx);
dev = fz_new_text_device(ctx, sheet, text);
fz_run_page(doc, page, dev, &fz_identity, &cookie);
static fz_rect search_hit_bbox[5000] = {0};
int a = fz_search_text_page(ctx, text, “V1”, search_hit_bbox, 5000);
fz_always(ctx)
{
fz_free_device(dev);
dev = NULL;
fz_free_text_page(ctx, text);
}
fz_catch(ctx)
{
fz_drop_display_list(ctx, list);
fz_free_page(doc, page);
fz_rethrow(ctx);
}