qpdf icon indicating copy to clipboard operation
qpdf copied to clipboard

Remove PDF pages while retaining the bookmarks

Open danielvartan opened this issue 2 years ago • 2 comments

Hi,

Awesome package!

Is there a way to remove PDF pages using qpdf::pdf_subset() without losing all the bookmarks?

danielvartan avatar Sep 26 '23 03:09 danielvartan

Not the way the function cpp_pdf_split used by qpdf::pdf_subset is written. cpp_pdf_split copies the pages requested to a new empty pdf file, which looses all bookmarks, links, etc. To preserve bookmarks, cpp_pdf_split would need to remove any pages not requested from the input file. This would be a relatively trivial to implement.

m-holger avatar Apr 09 '24 20:04 m-holger

Sample implementation:

Rcpp::CharacterVector cpp_pdf_select(char const* infile, char const* outfile,
                                     Rcpp::IntegerVector which, char const* password){
  QPDF inpdf;
  read_pdf_with_password(infile, password, &inpdf);
  QPDFPageDocumentHelper in_pdh(inpdf);
  std::vector<QPDFPageObjectHelper> pages =  in_pdh.getAllPages();
  for (auto const& page :pages) {
    in_pdh.removePage(page);
  }
  for (int i = 0; i < which.size(); i++) {
    int index = which.at(i) -1; //zero index
    in_pdh.addPage(pages.at(index), false);
  }
  QPDFWriter outpdfw(inpdf, outfile);
  outpdfw.setStaticID(true); // for testing only
  outpdfw.setStreamDataMode(qpdf_s_preserve);
  outpdfw.write();
  return outfile;
}

m-holger avatar Apr 13 '24 11:04 m-holger