0

I'am trying using vips in c++ to read a .PDF and convert to .jpeg files. The problem is that the code save all the pages in a single file .jpeg. How can i save in many .jpeg files?

My Code

    VOption *voptions = new VOption();
    voptions->set("dpi",150);
    voptions->set("page", 0);
    voptions->set("n", -1);

    VImage in = VImage().pdfload("/Users/gui/Desktop/PDF_Reader/files/TEST_DOC_READER.pdf",voptions);

    in.write_to_file("/Users/MyUser/Desktop/PDF_Reader/outputs/*.jpeg");

Guilherme
  • 33
  • 4
  • 2
    Just load the pages one at a time rather than setting `n` to `-1`? – Alan Birtles Jul 13 '22 at 11:10
  • 1
    Maybe not super efficient, but you can `pdfload` 1 page at a time and write it to consecutive file. – pptaszni Jul 13 '22 at 11:15
  • 1
    Unrelated to your problem, but there's no need to allocate `voptions` dynamically. Just create it as a normal non-pointer object, and when a pointer to it is needed use the pointer-to operator `&`. Less pointers means less chances of problems (like memory leaks etc.). – Some programmer dude Jul 13 '22 at 11:18
  • @AlanBirtles I was looking for something more efficient. This way, I need to read .PDF to get pages quantity and read each page one at time again. – Guilherme Jul 13 '22 at 14:38
  • @Someprogrammerdude tks! – Guilherme Jul 13 '22 at 14:41
  • In that case you probably need to use a different library, doesn't look like libvips has any other way to do it – Alan Birtles Jul 13 '22 at 15:04

1 Answers1

0

I found a way to solve this using crop.

    VImage in = VImage().pdfload("/Users/MyUser/Desktop/PDF_Reader/files/TEST_DOC_READER.pdf", voptions);
    pages = in.get_int("n-pages");
    h = in.height()/pages;

    for(int i=0; i<pages; i++){
        in.crop(0,i*h, in.width(), h).jpegsave((outdir+to_string(i)+format).c_str());
    }
Guilherme
  • 33
  • 4