void closexref(void) { int i; if (doc) { pdf_close_document(doc); doc = NULL; } if (dim) { for (i = 0; i < dims; i++) fz_free(ctx, dim[i].u.dim.bbox); fz_free(ctx, dim); dim = NULL; dims = 0; } if (font) { fz_free(ctx, font); font = NULL; fonts = 0; } if (image) { fz_free(ctx, image); image = NULL; images = 0; } if (shading) { fz_free(ctx, shading); shading = NULL; shadings = 0; } if (pattern) { fz_free(ctx, pattern); pattern = NULL; patterns = 0; } if (form) { fz_free(ctx, form); form = NULL; forms = 0; } if (psobj) { fz_free(ctx, psobj); psobj = NULL; psobjs = 0; } }
void pdf_clean_file(fz_context *ctx, char *infile, char *outfile, char *password, fz_write_options *opts, char *argv[], int argc) { globals glo = { 0 }; glo.ctx = ctx; fz_try(ctx) { glo.doc = pdf_open_document_no_run(ctx, infile); if (pdf_needs_password(ctx, glo.doc)) if (!pdf_authenticate_password(ctx, glo.doc, password)) fz_throw(glo.ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); /* Only retain the specified subset of the pages */ if (argc) retainpages(ctx, &glo, argc, argv); pdf_write_document(ctx, glo.doc, outfile, opts); } fz_always(ctx) { pdf_close_document(ctx, glo.doc); } fz_catch(ctx) { if (opts && opts->errors) *opts->errors = *opts->errors+1; } }
Document *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(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_close_document(raw_pdf_document); } fz_free_context(context); return nullptr; } PDFDocument *document = new PDFDocument(page_cache_size); document->_fz_context = context; document->_pdf_document = raw_pdf_document; return document; }
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; }
/* ** 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); }
JNIEXPORT void JNICALL Java_com_artifex_mupdf_MuPDFCore_destroying(JNIEnv * env, jobject thiz) { fz_free_display_list(ctx, currentPageList); currentPageList = NULL; pdf_close_document(xref); xref = NULL; }
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; }
static int pdfpages_pages(fz_context *ctx, fz_output *out, char *filename, char *password, char *argv[], int argc) { enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state; int argidx = 0; pdf_document *doc = NULL; int ret = 0; state = NO_FILE_OPENED; while (argidx < argc) { if (state == NO_FILE_OPENED || !arg_is_page_range(argv[argidx])) { if (state == NO_INFO_GATHERED) { showpages(ctx, doc, out, "1-"); } pdf_close_document(ctx, doc); filename = argv[argidx]; fz_printf(ctx, out, "%s:\n", filename); doc = pdf_open_document(ctx, filename); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename); state = NO_INFO_GATHERED; } else { ret |= showpages(ctx, doc, out, argv[argidx]); state = INFO_SHOWN; } argidx++; } if (state == NO_INFO_GATHERED) showpages(ctx, doc, out, "1-"); pdf_close_document(ctx, doc); return ret; }
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; }
PDFDocument::~PDFDocument() { // Must destroy page cache explicitly first, since destroying cached pages // involves clearing MuPDF state, which requires document structures // (_pdf_document, _fz_context) to still exist. _page_cache.reset(); pdf_close_document(_pdf_document); fz_free_context(_fz_context); }
JNIEXPORT int JNICALL Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jfilename) { const char *filename; int pages = 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; } xref = NULL; fz_try(ctx) { colorspace = fz_device_rgb; LOGE("Opening document..."); fz_try(ctx) { xref = pdf_open_document(ctx, filename); } fz_catch(ctx) { fz_throw(ctx, "Cannot open document: '%s'\n", filename); } pages = pdf_count_pages(xref); LOGE("Done! %d pages", pages); } fz_catch(ctx) { LOGE("Failed: %s", ctx->error->message); pdf_close_document(xref); xref = NULL; fz_free_context(ctx); ctx = NULL; } return pages; }
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); }
ErrorCode juggler_put_page_contents(juggler_t *src, const char *filename) { pdf_document *new_doc = test_creation(src); struct pos_info *pos = calloc(src->pagecount, sizeof(struct pos_info)); int i = 0; for(; i < src->pagecount; i++) { //, 695 * i, 0, 695, 942, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; pos[i].scale = 1.0; pos[i].x = 595.27 * (i % 2); pos[i].y = 0; pos[i].width = 595.27; pos[i].height = 842; pos[i].outer_clip_x = pos[i].x; pos[i].outer_clip_y = pos[i].y; pos[i].outer_clip_width = pos[i].width; pos[i].outer_clip_height = pos[i].height; pos[i].sheet_pagenum = i / 2; pos[i].src_pagenum = i; } put_pages_on_new_sheet(src->ctx, new_doc, src->ctx, src->pdf, pos, src->pagecount); /* write the new document to disk */ fz_write_options write_options; write_options.do_incremental = 0; write_options.do_ascii = 0; write_options.do_expand = 0; write_options.do_garbage = 1; write_options.do_linear = 0; write_options.do_clean = 0; write_options.continue_on_error = 0; pdf_write_document(src->ctx, new_doc, filename, &write_options); /* and close it */ pdf_close_document(src->ctx, new_doc); return(NoError); }
pdf_document *openxref(fz_context *ctx, char *filename) { pdf_document *xref = pdf_open_document(ctx, filename); fz_try(ctx) { if (pdf_needs_password(xref)) { logbench("Warning: password protected document\n"); fz_throw(ctx, "document requires password"); } pdf_count_pages(xref); } fz_catch(ctx) { pdf_close_document(xref); fz_rethrow(ctx); } return xref; }
/* ** Reconstruct PDF file per the information in pageinfo. ** use_forms==0: Old-style reconstruction where the pages are not turned into XObject Forms. ** use_forms==1: New-style where pages are turned into XObject forms. */ int wmupdf_remake_pdf(char *infile,char *outfile,WPDFPAGEINFO *pageinfo,int use_forms,FILE *out) { pdf_document *xref; fz_context *ctx; fz_write_options fzopts; char *password=""; int status; int write_failed; fzopts.do_garbage=1; /* 2 and 3 don't work for this. */ fzopts.do_expand=0; fzopts.do_ascii=0; fzopts.do_linear=0; write_failed=0; wpdfpageinfo_sort(pageinfo); xref=NULL; /* New context */ ctx = fz_new_context(NULL,NULL,FZ_STORE_UNLIMITED); if (!ctx) { nprintf(out,"wmupdf_remake_pdf: Cannot initialize context.\n"); return(-1); } fz_try(ctx) { xref=pdf_open_document_no_run(ctx,infile); if (!xref) { fz_free_context(ctx); nprintf(out,"wmupdf_remake_pdf: Cannot open PDF file %s.\n",infile); return(-2); } if (pdf_needs_password(xref) && !pdf_authenticate_password(xref,password)) { pdf_close_document(xref); fz_free_context(ctx); nprintf(out,"wmupdf_remake_pdf: Cannot authenticate PDF file %s.\n",infile); return(-3); } status=wmupdf_pdfdoc_newpages(xref,ctx,pageinfo,use_forms,out); if (status<0) { pdf_close_document(xref); fz_free_context(ctx); nprintf(out,"wmupdf_remake_pdf: Error re-paginating PDF file %s.\n",infile); return(status); } info_update(ctx,xref,pageinfo->producer); /* Write output */ pdf_write_document(xref,outfile,&fzopts); } fz_always(ctx) { pdf_close_document(xref); } fz_catch(ctx) { write_failed=1; } fz_free_context(ctx); if (write_failed) { nprintf(out,"wmupdf_remake_pdf: Error writing output PDF file %s.\n",outfile); return(-10); } return(0); }
int pdfclean_main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; char *password = ""; int c; int subset; fz_write_options opts; int write_failed = 0; opts.do_garbage = 0; opts.do_expand = 0; opts.do_ascii = 0; opts.do_linear = 0; while ((c = fz_getopt(argc, argv, "adfgilp:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'g': opts.do_garbage ++; break; case 'd': opts.do_expand ^= fz_expand_all; break; case 'f': opts.do_expand ^= fz_expand_fonts; break; case 'i': opts.do_expand ^= fz_expand_images; break; case 'l': opts.do_linear ++; break; case 'a': opts.do_ascii ++; 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++]; } subset = 0; if (argc - fz_optind > 0) subset = 1; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } fz_try(ctx) { 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 */ if (subset) retainpages(argc, argv); pdf_write_document(xref, outfile, &opts); } fz_always(ctx) { pdf_close_document(xref); } fz_catch(ctx) { write_failed = 1; } fz_free_context(ctx); return write_failed ? 1 : 0; }
int main(int argc, char **argv) #endif { char *infile; char *outfile = "out.pdf"; char *password = ""; int c, num; int subset; while ((c = fz_getopt(argc, argv, "adfgip:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'g': dogarbage ++; break; case 'd': doexpand ^= expand_all; break; case 'f': doexpand ^= expand_fonts; break; case 'i': doexpand ^= expand_images; break; case 'a': doascii ++; 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++]; } subset = 0; if (argc - fz_optind > 0) subset = 1; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } xref = pdf_open_document(ctx, infile); if (pdf_needs_password(xref)) if (!pdf_authenticate_password(xref, password)) fz_throw(ctx, "cannot authenticate password: %s\n", infile); out = fopen(outfile, "wb"); if (!out) fz_throw(ctx, "cannot open output file '%s'", outfile); fprintf(out, "%%PDF-%d.%d\n", xref->version / 10, xref->version % 10); fprintf(out, "%%\316\274\341\277\246\n\n"); uselist = fz_malloc_array(ctx, xref->len + 1, sizeof(char)); ofslist = fz_malloc_array(ctx, xref->len + 1, sizeof(int)); genlist = fz_malloc_array(ctx, xref->len + 1, sizeof(int)); renumbermap = fz_malloc_array(ctx, xref->len + 1, sizeof(int)); for (num = 0; num < xref->len; num++) { uselist[num] = 0; ofslist[num] = 0; genlist[num] = 0; renumbermap[num] = num; } /* Make sure any objects hidden in compressed streams have been loaded */ preloadobjstms(); /* Only retain the specified subset of the pages */ if (subset) retainpages(argc, argv); /* Sweep & mark objects from the trailer */ if (dogarbage >= 1) sweepobj(xref->trailer); /* Coalesce and renumber duplicate objects */ if (dogarbage >= 3) removeduplicateobjs(); /* Compact xref by renumbering and removing unused objects */ if (dogarbage >= 2) compactxref(); /* Make renumbering affect all indirect references and update xref */ /* Do not renumber objects if encryption is in use, as the object * numbers are baked into the streams/strings, and we can't currently * cope with moving them. See bug 692627. */ if (dogarbage >= 2 && !xref->crypt) renumberobjs(); writepdf(); if (fclose(out)) fz_throw(ctx, "cannot close output file '%s'", outfile); fz_free(xref->ctx, uselist); fz_free(xref->ctx, ofslist); fz_free(xref->ctx, genlist); fz_free(xref->ctx, renumbermap); pdf_close_document(xref); fz_free_context(ctx); return 0; }
PdfCreator::~PdfCreator() { pdf_close_document(doc); fz_free_context(ctx); }
Document::~Document () { pdf_close_document (MuDoc_); }