hello
- User
hello
- Assistant
Hello! How can I assist you today?
- User
C++ 使用mupdf检索关键字位置示例代码
- Assistant
Certainly! Here's an example code in C++ that demonstrates searching for a keyword and retrieving its position using the MuPDF library:
#include <mupdf/fitz.h> #include <mupdf/pdf.h> int main(int argc, char **argv) { const char* filename = "path/to/your/pdf/file.pdf"; const char* keyword = "your_keyword"; fz_context* ctx; fz_document* doc; fz_page* page; fz_rect bounds; fz_try(ctx) { ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); fz_register_document_handlers(ctx); doc = fz_open_document(ctx, filename); int numPages = fz_count_pages(ctx, doc); for (int pageNum = 0; pageNum < numPages; ++pageNum) { page = fz_load_page(ctx, doc, pageNum); fz_text_sheet* textSheet = fz_new_text_sheet_from_page(ctx, page, &bounds, FZ_STEXT_ACCURATE); fz_stext_page* stext = fz_new_stext_page_from_text_sheet(ctx, textSheet); fz_search_order* order = fz_new_search_order(ctx, 1); fz_new_search_item(ctx, order, keyword, 0, 0); fz_search_hit* hits = NULL; int numHits = fz_search_stext(ctx, stext, order, &hits); if (numHits > 0) { // Print the search hits and their positions for (int i = 0; i < numHits; ++i) { fz_search_hit* hit = &hits[i]; printf("Page: %d, Position: %.2f, %.2f\n", pageNum+1, hit->x, bounds.y1 - hit->y); } } fz_free_stext_page(ctx, stext); fz_free_text_sheet(ctx, textSheet); fz_free_page(ctx, page); } fz_free_document(ctx, doc); fz_free_context(ctx); } fz_catch(ctx) { fprintf(stderr, "MuPDF error: %s\n", fz_exception_message(ctx)); return 1; } return 0; } - User
C++ 使用mupdf1.18版本检索关键字位置示例代码
- Assistant
Certainly! Here's an example code in C++ that demonstrates searching for a keyword and retrieving its position using the MuPDF 1.18 version:
#include <mupdf/pdf.h> #include <mupdf/fitz.h> int main(int argc, char **argv) { const char* filename = "path/to/your/pdf/file.pdf"; const char* keyword = "your_keyword"; fz_context* ctx; fz_document* doc; fz_page* page; fz_try(ctx) { ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); fz_register_document_handlers(ctx); doc = fz_open_document(ctx, filename); int numPages = fz_count_pages(ctx, doc); for (int pageNum = 0; pageNum < numPages; ++pageNum) { page = fz_load_page(ctx, doc, pageNum); fz_text_sheet* textSheet = fz_new_text_sheet(ctx); fz_page_boundaries boundaries; fz_bound_page(ctx, page, &boundaries); fz_run_page(ctx, page, textSheet, &boundaries, NULL); fz_rect hit_bbox; fz_search_hit* hits = NULL; int numHits = fz_search_stext_page(ctx, textSheet, keyword, &hits, &hit_bbox); if (numHits > 0) { // Print the search hits and their positions for (int i = 0; i < numHits; ++i) { printf("Page: %d, Position: %.2f, %.2f\n", pageNum+1, hit_bbox.x0, hit_bbox.y1); } } fz_free_text_sheet(ctx, textSheet); fz_free_page(ctx, page); } fz_free_document(ctx, doc); fz_free_context(ctx); } fz_catch(ctx) { fprintf(stderr, "MuPDF error: %s\n", fz_caught_message(ctx)); return 1; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.