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 pdfinfo_main(int argc, char **argv) { enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state; char *filename = ""; char *password = ""; int show = ALL; int c; while ((c = fz_getopt(argc, argv, "mfispxd:")) != -1) { switch (c) { case 'm': if (show == ALL) show = DIMENSIONS; else show |= DIMENSIONS; break; case 'f': if (show == ALL) show = FONTS; else show |= FONTS; break; case 'i': if (show == ALL) show = IMAGES; else show |= IMAGES; break; case 's': if (show == ALL) show = SHADINGS; else show |= SHADINGS; break; case 'p': if (show == ALL) show = PATTERNS; else show |= PATTERNS; break; case 'x': if (show == ALL) show = XOBJS; else show |= XOBJS; break; case 'd': 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); } state = NO_FILE_OPENED; while (fz_optind < argc) { if (state == NO_FILE_OPENED || !arg_is_page_range(argv[fz_optind])) { if (state == NO_INFO_GATHERED) { showinfo(filename, show, "1-"); } closexref(); filename = argv[fz_optind]; printf("%s:\n", filename); doc = pdf_open_document_no_run(ctx, filename); if (pdf_needs_password(doc)) if (!pdf_authenticate_password(doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename); pagecount = pdf_count_pages(doc); showglobalinfo(); state = NO_INFO_GATHERED; } else { showinfo(filename, show, argv[fz_optind]); state = INFO_SHOWN; } fz_optind++; } if (state == NO_INFO_GATHERED) showinfo(filename, show, "1-"); closexref(); fz_free_context(ctx); return 0; }