Pdf::Pdf() { if (references == 0) ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); // 256MB cache ++references; doc = 0; }
PDFDocument* PDFDocument::Open(const std::string& path, int page_cache_size) { fz_context* context = fz_new_context(nullptr, nullptr, FZ_STORE_DEFAULT); pdf_document* raw_pdf_document = nullptr; fz_try(context) { raw_pdf_document = pdf_open_document(context, path.c_str()); if ((raw_pdf_document == nullptr) || (!pdf_count_pages(context, raw_pdf_document))) { fz_throw( context, FZ_ERROR_GENERIC, const_cast<char*>("Cannot open document \"%s\""), path.c_str()); } } fz_catch(context) { if (raw_pdf_document != nullptr) { pdf_drop_document(context, raw_pdf_document); } fz_drop_context(context); return nullptr; } PDFDocument* document = new PDFDocument(page_cache_size); document->_fz_context = context; document->_pdf_document = raw_pdf_document; return document; }
int main(int argc, char *argv[]) { int saved = 0; if (argc != 3) { printf("Usage: extr file.pdf pageno\n"); exit(1); } char *filename = strdup(argv[1]); char *dir = dirname(strdup(filename)); int pageno = atoi(argv[2]); fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "extr: cannot create context\n"); exit(1); } fz_var(doc); fz_try(ctx) { doc = pdf_open_document(ctx, filename); saved = save_attachments(pageno, dir); } fz_catch(ctx) { } printf("%d\n", saved); return 0; }
int main(int argc, char **argv) { /* get filename */ char *filename = NULL; if (argc != 2) { printf("usage: %s filename.pdf\n", argv[0]); return 1; } if (strcmp(argv[1], "--help") == 0) { printf("usage: %s filename.pdf\n", argv[0]); return 0; } filename = argv[1]; /* open document */ fz_context *context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); fz_document *document = fz_open_document(context, filename); /* print page count */ printf("page count: %d\n", fz_count_pages(document)); fz_close_document(document); fz_free_context(context); return 0; }
int pdfextract_main(int argc, char **argv) { char *infile; char *password = ""; int c, o; while ((c = fz_getopt(argc, argv, "p:r")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'r': dorgb++; break; default: usage(); break; } } if (fz_optind == argc) usage(); infile = argv[fz_optind++]; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } doc = pdf_open_document(ctx, infile); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); if (fz_optind == argc) { int len = pdf_count_objects(ctx, doc); for (o = 1; o < len; o++) showobject(o); } else { while (fz_optind < argc) { showobject(atoi(argv[fz_optind])); fz_optind++; } } pdf_close_document(ctx, doc); fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int pdfsign_main(int argc, char **argv) { fz_context *ctx; pdf_document *doc; char *password = ""; int i, n, c; pdf_page *page = NULL; while ((c = fz_getopt(argc, argv, "p:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; default: usage(); break; } } if (argc - fz_optind < 1) usage(); filename = argv[fz_optind++]; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialize context\n"); exit(1); } fz_var(page); doc = pdf_open_document(ctx, filename); fz_try(ctx) { if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_warn(ctx, "cannot authenticate password: %s", filename); n = pdf_count_pages(ctx, doc); for (i = 0; i < n; ++i) { page = pdf_load_page(ctx, doc, i); verify_page(ctx, doc, i, page); fz_drop_page(ctx, (fz_page*)page); page = NULL; } } fz_always(ctx) pdf_drop_document(ctx, doc); fz_catch(ctx) { fz_drop_page(ctx, (fz_page*)page); fprintf(stderr, "error verify signatures: %s\n", fz_caught_message(ctx)); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int pdfposter_main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; char *password = ""; int c; fz_write_options opts = { 0 }; pdf_document *doc; fz_context *ctx; opts.do_incremental = 0; opts.do_garbage = 0; opts.do_expand = 0; opts.do_ascii = 0; opts.do_linear = 0; while ((c = fz_getopt(argc, argv, "x:y:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'x': x_factor = atoi(fz_optarg); break; case 'y': y_factor = atoi(fz_optarg); break; default: usage(); break; } } if (argc - fz_optind < 1) usage(); infile = argv[fz_optind++]; if (argc - fz_optind > 0 && (strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF"))) { outfile = argv[fz_optind++]; } ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } doc = pdf_open_document(ctx, infile); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); decimatepages(ctx, doc); pdf_write_document(ctx, doc, outfile, &opts); pdf_close_document(ctx, doc); fz_drop_context(ctx); return 0; }
Pdf::Pdf() { if (references == 0) { ctx = fz_new_context(&fz_alloc_default, 256 << 20); // 256MB cache cache = fz_new_glyph_cache(ctx); } ++references; xref = 0; }
int pdfposter_main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; char *password = ""; int c; fz_write_options opts; pdf_document *xref; fz_context *ctx; opts.do_garbage = 0; opts.do_expand = 0; opts.do_ascii = 0; while ((c = fz_getopt(argc, argv, "x:y:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'x': x_factor = atoi(fz_optarg); break; case 'y': y_factor = atoi(fz_optarg); break; default: usage(); break; } } if (argc - fz_optind < 1) usage(); infile = argv[fz_optind++]; if (argc - fz_optind > 0 && (strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF"))) { outfile = argv[fz_optind++]; } ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } xref = pdf_open_document_no_run(ctx, infile); if (pdf_needs_password(xref)) if (!pdf_authenticate_password(xref, password)) fz_throw(ctx, "cannot authenticate password: %s", infile); /* Only retain the specified subset of the pages */ decimatepages(xref); pdf_write_document(xref, outfile, &opts); pdf_close_document(xref); fz_free_context(ctx); return 0; }
bool Viewer::init() { ctx = fz_new_context(nullptr, nullptr, FZ_STORE_UNLIMITED); if (ctx) { fz_register_document_handlers(ctx); return true; } else { return false; } }
void DrPDFExtractor::Initialize() { if (m_ctx) { fz_free_context(m_ctx); } m_ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (m_doc) { fz_close_document(m_doc); m_doc = NULL; } }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int argc; LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); char argv0[256]; MSG msg; int fd; int code; fz_context *ctx; ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } pdfapp_init(ctx, &gapp); GetModuleFileNameA(NULL, argv0, sizeof argv0); install_app(argv0); winopen(); if (argc == 2) { wcscpy(wbuf, argv[1]); } else { if (!winfilename(wbuf, nelem(wbuf))) exit(0); } fd = _wopen(wbuf, O_BINARY | O_RDONLY, 0666); if (fd < 0) winerror(&gapp, "cannot open file"); code = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, filename, sizeof filename, NULL, NULL); if (code == 0) winerror(&gapp, "cannot convert filename to utf-8"); pdfapp_open(&gapp, filename, fd, 0); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } pdfapp_close(&gapp); return 0; }
PdfCreator::PdfCreator() { ctx = fz_new_context(nullptr, nullptr, FZ_STORE_DEFAULT); if (!ctx) return; fz_try(ctx) { doc = pdf_create_document(ctx); } fz_catch(ctx) { doc = nullptr; } }
JNIEXPORT int JNICALL Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jfilename) { const char *filename; int result = 0; filename = (*env)->GetStringUTFChars(env, jfilename, NULL); if (filename == NULL) { LOGE("Failed to get filename"); return 0; } /* 128 MB store for low memory devices. Tweak as necessary. */ ctx = fz_new_context(NULL, NULL, 128 << 20); if (!ctx) { LOGE("Failed to initialise context"); return 0; } doc = NULL; fz_try(ctx) { colorspace = fz_device_rgb; LOGE("Opening document..."); fz_try(ctx) { doc = fz_open_document(ctx, (char *)filename); } fz_catch(ctx) { fz_throw(ctx, "Cannot open document: '%s'\n", filename); } LOGE("Done!"); result = 1; } fz_catch(ctx) { LOGE("Failed: %s", ctx->error->message); fz_close_document(doc); doc = NULL; fz_free_context(ctx); ctx = NULL; } (*env)->ReleaseStringUTFChars(env, jfilename, filename); return result; }
void Mpdf::loadPdf(QString path) { currentPath = removeBackRef(path); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); doc = fz_open_document(ctx, currentPath.toStdString().c_str()); pageCount = fz_count_pages(doc); currentPage = 1; currentZoom = 60; enablePageNumber(); showPage(); hideTop(); }
int pdfpages_main(int argc, char **argv) { char *filename = ""; char *password = ""; int c; fz_output *out = NULL; int ret; fz_context *ctx; while ((c = fz_getopt(argc, argv, "p:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; default: infousage(); break; } } if (fz_optind == argc) infousage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } fz_var(out); ret = 0; fz_try(ctx) { out = fz_stdout(ctx); ret = pdfpages_pages(ctx, out, filename, password, &argv[fz_optind], argc-fz_optind); } fz_catch(ctx) { ret = 1; } fz_drop_output(ctx, out); fz_drop_context(ctx); return ret; }
HRESULT MuPDFDoc::InitContext() { InitializeCriticalSectionEx(&m_critSec, 0, 0); locks.user = &m_critSec; locks.lock = lock_mutex; locks.unlock = unlock_mutex; m_context = fz_new_context(nullptr, &locks, FZ_STORE_DEFAULT); if (!m_context) { return E_OUTOFMEMORY; } else { m_cts = (fz_cookie*)CoTaskMemAlloc(sizeof(fz_cookie)); return S_OK; } }
/* ** Get /Info strings, e.g. wmupdf_info_field("myfile.pdf","Author",buf,255); ** Info labels: ** Title Document title ** Author Name of the person who created the doc. ** Subject Subject of the doc ** Keywords Keywords associated with the document. ** Creator If doc was converted to PDF from another format, the name ** of the product that created the original document. ** Producer If doc was converted to PDF from another format, the name ** of the product that converted it to PDF. ** CreationDate Date/Time document was created. ** ModDate Date/Time of most recent mod. */ int wmupdf_info_field(char *infile,char *label,char *buf,int maxlen) { pdf_document *xref; fz_context *ctx; pdf_obj *info,*obj; char *password=""; xref=NULL; buf[0]='\0'; ctx = fz_new_context(NULL,NULL,FZ_STORE_UNLIMITED); if (!ctx) return(-1); fz_try(ctx) { xref=pdf_open_document_no_run(ctx,infile); if (!xref) { fz_free_context(ctx); return(-2); } if (pdf_needs_password(xref) && !pdf_authenticate_password(xref,password)) { pdf_close_document(xref); fz_free_context(ctx); return(-3); } if (xref->trailer!=NULL && (info=pdf_dict_gets(xref->trailer,"Info"))!=NULL && (obj=pdf_dict_gets(info,label))!=NULL && pdf_is_string(obj)) { strncpy(buf,pdf_to_str_buf(obj),maxlen-1); buf[maxlen-1]='\0'; } } fz_always(ctx) { pdf_close_document(xref); } fz_catch(ctx) { } fz_free_context(ctx); return(0); }
void benchfile(char *pdffilename, int loadonly, int pageNo) { pdf_document *xref = NULL; mstimer timer; int page_count; int curpage; fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { logbench("Error: fz_new_context() failed\n"); return; } logbench("Starting: %s\n", pdffilename); timerstart(&timer); fz_var(xref); fz_try(ctx) { xref = openxref(ctx, pdffilename); } fz_catch(ctx) { goto Exit; } timerstop(&timer); logbench("load: %.2f ms\n", timeinms(&timer)); page_count = pdf_count_pages(xref); logbench("page count: %d\n", page_count); if (loadonly) goto Exit; for (curpage = 1; curpage <= page_count; curpage++) { pdf_page *page; if ((-1 != pageNo) && (pageNo != curpage)) continue; page = benchloadpage(ctx, xref, curpage); if (page) { benchrenderpage(ctx, xref, page, curpage); pdf_free_page(xref, page); } } Exit: logbench("Finished: %s\n", pdffilename); pdf_close_document(xref); fz_free_context(ctx); }
void Init_mupdf_ext() { to_s = rb_intern ("to_s"); FZ_CONTEXT = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); fz_register_document_handlers(FZ_CONTEXT); /* MuPDF Module Holds the stuffs */ rb_MuPDF = rb_define_module( "MuPDF" ); Init_Document(); }
pdf::pdf(const char *filepath) { _filepath = filepath; _good = true; _image_name = nullptr; _needs_password = false; _size = 0; // Create a context to hold the exception stack and various caches. _ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!_ctx) { _good = false; return; } fz_try(_ctx) { // Register the default file types. fz_register_document_handlers(_ctx); // Open the PDF, XPS or CBZ document. _doc = fz_open_document(_ctx, filepath); if (fz_needs_password(_ctx, _doc)) { _needs_password = true; _good = false; } else { // Retrieve the number of pages (not used in this example). _size = fz_count_pages(_ctx, _doc); } } fz_catch(_ctx) { _doc = 0; _good = false; } }
ViewerWidget::ViewerWidget(QWidget *parent) : QWidget(parent) { scales << 0.0625 << 0.125 << 0.25 << 0.5 << 1.0 << 2.0 << 4.0 << 8.0 << 16.0; scene = new QGraphicsScene(this); view = new QGraphicsView(scene, this); view->setFocusPolicy(Qt::NoFocus); view->setFrameShape(QFrame::NoFrame); view->setDragMode(QGraphicsView::ScrollHandDrag); view->viewport()->setAutoFillBackground(false); view->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate); QVBoxLayout *vbox = new QVBoxLayout(this); vbox->setContentsMargins(0, 0, 0, 0); vbox->addWidget(view); setLayout(vbox); context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); }
/* Set up the context, mutex and cookie */ status_t muctx::InitializeContext() { int i; /* Get the mutexes set up */ for (i = 0; i < FZ_LOCK_MAX; i++) InitializeCriticalSectionEx(&mu_criticalsec[i], 0, 0); mu_locks.user = &mu_criticalsec[0]; mu_locks.lock = lock_mutex; mu_locks.unlock = unlock_mutex; /* Allocate the context */ this->mu_ctx = fz_new_context(NULL, &mu_locks, FZ_STORE_DEFAULT); if (this->mu_ctx == NULL) { return E_OUTOFMEM; } else { return S_ISOK; } }
JNIFUNC(jlong, MuContext, nNew)(JNIEnv *env, jclass clazz) { ContextGlue* glue = new ContextGlue; glue->ctx = fz_new_context(nullptr, nullptr, FZ_STORE_UNLIMITED); fz_register_document_handlers(glue->ctx); glue->RectF = env->FindClass("android/graphics/RectF"); glue->RectF_left = env->GetFieldID(glue->RectF, "left", "F"); glue->RectF_top = env->GetFieldID(glue->RectF, "top", "F"); glue->RectF_right = env->GetFieldID(glue->RectF, "right", "F"); glue->RectF_bottom = env->GetFieldID(glue->RectF, "bottom", "F"); glue->Bitmap = env->FindClass("android/graphics/Bitmap"); glue->Bitmap_mWidth = env->GetFieldID(glue->Bitmap, "mWidth", "I"); glue->Bitmap_mHeight = env->GetFieldID(glue->Bitmap, "mHeight", "I"); glue->MuDocument = env->FindClass("com/github/jiboo/dwiinaar/mupdf/MuDocument"); glue->MuDocument_dPageCount = env->GetFieldID(glue->MuDocument, "dPageCount", "I"); glue->MuDocument_dNeedsPassword = env->GetFieldID(glue->MuDocument, "dNeedsPassword", "Z"); glue->MuPDFException = env->FindClass("com/github/jiboo/dwiinaar/mupdf/MuPDFException"); return reinterpret_cast<jlong>(glue); }
int wmupdf_numpages(char *filename) { fz_context *ctx; fz_document *doc; int np; doc=NULL; ctx = fz_new_context(NULL,NULL,FZ_STORE_DEFAULT); if (!ctx) return(-1); fz_try(ctx) { doc=fz_open_document(ctx,filename); } fz_catch(ctx) { fz_free_context(ctx); return(-2); } np=fz_count_pages(doc); fz_close_document(doc); fz_flush_warnings(ctx); fz_free_context(ctx); return(np); }
bool MupdfBackend::load(const QString& filePath) { Q_ASSERT(m_doc == 0); Q_ASSERT(m_ctx == 0); m_ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); // TODO: locks if (!m_ctx) { return false; } fz_register_document_handlers(m_ctx); fz_try(m_ctx) { m_doc = fz_open_document(m_ctx, filePath.toUtf8()); } fz_catch(m_ctx) { fz_drop_context(m_ctx); m_ctx = 0; return false; } return true; }
MuError MuOfficeLib_create(MuOfficeLib **pMu) { MuOfficeLib *mu; fz_locks_context *locks; if (pMu == NULL) return MuOfficeDocErrorType_IllegalArgument; mu = Pal_Mem_calloc(1, sizeof(MuOfficeLib)); if (mu == NULL) return MuOfficeDocErrorType_OutOfMemory; locks = init_muoffice_locks(mu); if (locks == NULL) goto Fail; mu->ctx = fz_new_context(&muoffice_alloc, locks, FZ_STORE_DEFAULT); if (mu->ctx == NULL) goto Fail; fz_try(mu->ctx) fz_register_document_handlers(mu->ctx); fz_catch(mu->ctx) goto Fail; *pMu = mu; return MuOfficeDocErrorType_NoError; Fail: if (mu) { fin_muoffice_locks(mu); Pal_Mem_free(mu); } return MuOfficeDocErrorType_OutOfMemory; }
void convertFile(AcString pdfName, int page, BOOL bDwg) { fz_context *ctx; ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { return; } pdfapp_t gapp; pdfapp_init(ctx, &gapp); gapp.strFileName = pdfName; gapp.pageno = page; gapp.bOutputDwg = bDwg; //½øÐÐת»» char utf8Name[PATH_MAX]; pdfapp_AcString2Utf8Char(gapp.strFileName, utf8Name); pdfapp_open(&gapp, utf8Name, 0); pdfapp_close(&gapp); fz_free_context(ctx); }
int main(int argc, char **argv) { char *filename = argv[1]; pthread_t *thread = NULL; fz_locks_context locks; pthread_mutex_t mutex[FZ_LOCK_MAX]; int i; // Initialize FZ_LOCK_MAX number of non-recursive mutexes. for (i = 0; i < FZ_LOCK_MAX; i++) { if (pthread_mutex_init(&mutex[i], NULL) < 0) fail("pthread_mutex_init()"); } // Initialize the locking structure with function pointers to // the locking functions and to the user data. In this case // the user data is a pointer to the array of mutexes so the // locking functions can find the relevant lock to change when // they are called. This way we avoid global variables. locks.user = mutex; locks.lock = lock_mutex; locks.unlock = unlock_mutex; // This is the main threads context function, so supply the // locking structure. This context will be used to parse all // the pages from the document. fz_context *ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); // Open the PDF, XPS or CBZ document. fz_document *doc = fz_open_document(ctx, filename); // Retrieve the number of pages, which translates to the // number of threads used for rendering pages. int threads = fz_count_pages(doc); fprintf(stderr, "spawning %d threads, one per page...\n", threads); thread = malloc(threads * sizeof (pthread_t)); for (i = 0; i < threads; i++) { // Load the relevant page for each thread. fz_page *page = fz_load_page(doc, i); // Compute the bounding box for each page. fz_rect rect = fz_bound_page(doc, page); fz_bbox bbox = fz_round_rect(rect); // Create a display list that will hold the drawing // commands for the page. fz_display_list *list = fz_new_display_list(ctx); // Run the loaded page through a display list device // to populate the page's display list. fz_device *dev = fz_new_list_device(ctx, list); fz_run_page(doc, page, dev, fz_identity, NULL); fz_free_device(dev); // The page is no longer needed, all drawing commands // are now in the display list. fz_free_page(doc, page); // Create a white pixmap using the correct dimensions. fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb, bbox); fz_clear_pixmap_with_value(ctx, pix, 0xff); // Populate the data structure to be sent to the // rendering thread for this page. struct data *data = malloc(sizeof (struct data)); data->pagenumber = i + 1; data->ctx = ctx; data->list = list; data->bbox = bbox; data->pix = pix; // Create the thread and pass it the data structure. if (pthread_create(&thread[i], NULL, renderer, data) < 0) fail("pthread_create()"); } // Now each thread is rendering pages, so wait for each thread // to complete its rendering. fprintf(stderr, "joining %d threads...\n", threads); for (i = threads - 1; i >= 0; i--) { char filename[42]; struct data *data; if (pthread_join(thread[i], (void **) &data) < 0) fail("pthread_join"); sprintf(filename, "out%04d.png", i); fprintf(stderr, "\tSaving %s...\n", filename); // Write the rendered image to a PNG file fz_write_png(ctx, data->pix, filename, 0); // Free the thread's pixmap and display list since // they were allocated by the main thread above. fz_drop_pixmap(ctx, data->pix); fz_free_display_list(ctx, data->list); // Free the data structured passed back and forth // between the main thread and rendering thread. free(data); } fprintf(stderr, "finally!\n"); fflush(NULL); free(thread); // Finally the document is closed and the main thread's // context is freed. fz_close_document(doc); fz_free_context(ctx); return 0; }
void render(char *filename, int pagenumber, int zoom, int rotation) { // Create a context to hold the exception stack and various caches. fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); // Open the PDF, XPS or CBZ document. fz_document *doc = fz_open_document(ctx, filename); // Retrieve the number of pages (not used in this example). int pagecount = fz_count_pages(doc); // Load the page we want. Page numbering starts from zero. fz_page *page = fz_load_page(doc, pagenumber - 1); // Calculate a transform to use when rendering. This transform // contains the scale and rotation. Convert zoom percentage to a // scaling factor. Without scaling the resolution is 72 dpi. fz_matrix transform; fz_rotate(&transform, rotation); fz_pre_scale(&transform, zoom / 100.0f, zoom / 100.0f); // Take the page bounds and transform them by the same matrix that // we will use to render the page. fz_rect bounds; fz_bound_page(doc, page, &bounds); fz_transform_rect(&bounds, &transform); // Create a blank pixmap to hold the result of rendering. The // pixmap bounds used here are the same as the transformed page // bounds, so it will contain the entire page. The page coordinate // space has the origin at the top left corner and the x axis // extends to the right and the y axis extends down. fz_irect bbox; fz_round_rect(&bbox, &bounds); fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox); fz_clear_pixmap_with_value(ctx, pix, 0xff); // A page consists of a series of objects (text, line art, images, // gradients). These objects are passed to a device when the // interpreter runs the page. There are several devices, used for // different purposes: // // draw device -- renders objects to a target pixmap. // // text device -- extracts the text in reading order with styling // information. This text can be used to provide text search. // // list device -- records the graphic objects in a list that can // be played back through another device. This is useful if you // need to run the same page through multiple devices, without // the overhead of parsing the page each time. // Create a draw device with the pixmap as its target. // Run the page with the transform. fz_device *dev = fz_new_draw_device(ctx, pix); fz_run_page(doc, page, dev, &transform, NULL); fz_free_device(dev); // Save the pixmap to a file. fz_write_png(ctx, pix, "out.png", 0); // Clean up. fz_drop_pixmap(ctx, pix); fz_free_page(doc, page); fz_close_document(doc); fz_free_context(ctx); }