2

My app on iOS extracts embedded files from PDF. Now, i try to make Android app with same functionality using MuPDF.

On iOS, I can use Quartz2d to extract embedded files:

  1. Access to root PDF dictionary (CGPDFDocumentGetCatalog)
  2. Get files array (Names > EmbeddedFiles > Names) and itarate through it
  3. Copy file stream contents from file dictionary (EF > F) to NSData and save it.

Is there any way to do this with MuPDF?

4

1 回答 1

1

基于 pdfextact.c 的解决方案看起来像蛮力,但它有效:

  1. 遍历所有 pdf 对象 ( pdf_load_object)
  2. 确定对象是否为嵌入文件 ( isembed)
  3. 如果是 - 访问它的流并保存文件(saveembed

在大多数测试用例中,嵌入文件存储在文件末尾,因此,reverce 迭代是有意义的。

static int isembed(pdf_obj *obj) {
    pdf_obj *type = pdf_dict_gets(obj, "Type");
    return pdf_is_name(type) && !strcmp(pdf_to_name(type), "Filespec");
}


static void saveembed(pdf_obj *dict) {
    char *filename;

    pdf_obj *obj = pdf_dict_gets(dict, "F");
    if (obj) filename = pdf_to_str_buf(obj);

    obj = pdf_dict_gets(dict, "EF");
    if (!obj) return;

    pdf_obj *stream = pdf_dict_gets(obj, "F");
    if (!stream) return;

    FILE *f;
    fz_buffer *buf;
    int n, len;
    unsigned char *data;

    buf = pdf_load_stream(doc, pdf_to_num(stream), pdf_to_gen(stream));

    printf("extracting embedded file %s\n", filename);

    f = fopen(filename, "wb");

    len = fz_buffer_storage(ctx, buf, &data);
    n = fwrite(data, 1, len, f);

    fclose(f);
    fz_drop_buffer(ctx, buf);
}
于 2013-01-24T15:44:17.517 回答