Esempio n. 1
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;
}
Esempio n. 2
0
/* Image specific methods */
static void
res_image_init(fz_context *ctx, pdf_document *doc, pdf_res_table *table)
{
	int len, k;
	pdf_obj *obj;
	pdf_obj *type;
	pdf_obj *res = NULL;
	fz_image *image = NULL;
	unsigned char digest[16];

	fz_var(obj);
	fz_var(image);
	fz_var(res);

	fz_try(ctx)
	{
		table->hash = fz_new_hash_table(ctx, 4096, 16, -1);
		len = pdf_count_objects(ctx, doc);
		for (k = 1; k < len; k++)
		{
			obj = pdf_load_object(ctx, doc, k, 0);
			type = pdf_dict_get(ctx, obj, PDF_NAME_Subtype);
			if (pdf_name_eq(ctx, type, PDF_NAME_Image))
			{
				image = pdf_load_image(ctx, doc, obj);
				res_image_get_md5(ctx, image, digest);
				fz_drop_image(ctx, image);
				image = NULL;

				/* Don't allow overwrites. */
				if (fz_hash_find(ctx, table->hash, digest) == NULL)
					fz_hash_insert(ctx, table->hash, digest, obj);
			}
			else
			{
				pdf_drop_obj(ctx, obj);
			}
			obj = NULL;
		}
	}
	fz_always(ctx)
	{
		fz_drop_image(ctx, image);
		pdf_drop_obj(ctx, obj);
	}
	fz_catch(ctx)
	{
		res_table_free(ctx, table);
		fz_rethrow(ctx);
	}
}