Commit 2c6fe1805a2fa5fae719224dff44f41a823466b8

Authored by Jay Berkenbilt
1 parent df33c368

Support groups of pages in --split-pages (fixes #30)

qpdf/qpdf.cc
@@ -43,7 +43,7 @@ struct Options @@ -43,7 +43,7 @@ struct Options
43 password(0), 43 password(0),
44 linearize(false), 44 linearize(false),
45 decrypt(false), 45 decrypt(false),
46 - split_pages(false), 46 + split_pages(0),
47 copy_encryption(false), 47 copy_encryption(false),
48 encryption_file(0), 48 encryption_file(0),
49 encryption_file_password(0), 49 encryption_file_password(0),
@@ -98,7 +98,7 @@ struct Options @@ -98,7 +98,7 @@ struct Options
98 char const* password; 98 char const* password;
99 bool linearize; 99 bool linearize;
100 bool decrypt; 100 bool decrypt;
101 - bool split_pages; 101 + int split_pages;
102 bool copy_encryption; 102 bool copy_encryption;
103 char const* encryption_file; 103 char const* encryption_file;
104 char const* encryption_file_password; 104 char const* encryption_file_password;
@@ -1338,7 +1338,8 @@ static void parse_options(int argc, char* argv[], Options& o) @@ -1338,7 +1338,8 @@ static void parse_options(int argc, char* argv[], Options& o)
1338 } 1338 }
1339 else if (strcmp(arg, "split-pages") == 0) 1339 else if (strcmp(arg, "split-pages") == 0)
1340 { 1340 {
1341 - o.split_pages = true; // XXX 1341 + int n = ((parameter == 0) ? 1 : atoi(parameter));
  1342 + o.split_pages = n;
1342 } 1343 }
1343 else if (strcmp(arg, "deterministic-id") == 0) 1344 else if (strcmp(arg, "deterministic-id") == 0)
1344 { 1345 {
@@ -2007,16 +2008,28 @@ static void write_outfile(QPDF& pdf, Options& o) @@ -2007,16 +2008,28 @@ static void write_outfile(QPDF& pdf, Options& o)
2007 2008
2008 std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); 2009 std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
2009 int pageno_len = QUtil::int_to_string(pages.size()).length(); 2010 int pageno_len = QUtil::int_to_string(pages.size()).length();
2010 - int pageno = 0;  
2011 - for (std::vector<QPDFObjectHandle>::const_iterator iter = pages.begin();  
2012 - iter != pages.end(); ++iter) 2011 + unsigned int num_pages = pages.size();
  2012 + for (unsigned int i = 0; i < num_pages; i += o.split_pages)
2013 { 2013 {
2014 - QPDFObjectHandle page = *iter;  
2015 - std::string outfile =  
2016 - before + QUtil::int_to_string(++pageno, pageno_len) + after; 2014 + unsigned int first = i + 1;
  2015 + unsigned int last = i + o.split_pages;
  2016 + if (last > num_pages)
  2017 + {
  2018 + last = num_pages;
  2019 + }
2017 QPDF outpdf; 2020 QPDF outpdf;
2018 outpdf.emptyPDF(); 2021 outpdf.emptyPDF();
2019 - outpdf.addPage(page, false); 2022 + for (unsigned int pageno = first; pageno <= last; ++pageno)
  2023 + {
  2024 + QPDFObjectHandle page = pages.at(pageno - 1);
  2025 + outpdf.addPage(page, false);
  2026 + }
  2027 + std::string page_range = QUtil::int_to_string(first, pageno_len);
  2028 + if (o.split_pages > 1)
  2029 + {
  2030 + page_range += "-" + QUtil::int_to_string(last, pageno_len);
  2031 + }
  2032 + std::string outfile = before + page_range + after;
2020 QPDFWriter w(outpdf, outfile.c_str()); 2033 QPDFWriter w(outpdf, outfile.c_str());
2021 set_writer_options(outpdf, o, w); 2034 set_writer_options(outpdf, o, w);
2022 w.write(); 2035 w.write();
qpdf/qtest/qpdf.test
@@ -713,7 +713,7 @@ foreach my $d ( @@ -713,7 +713,7 @@ foreach my $d (
713 } 713 }
714 show_ntests(); 714 show_ntests();
715 # ---------- 715 # ----------
716 -$td->notify("--- Split Pages ---"); # XXXX 716 +$td->notify("--- Split Pages ---");
717 # sp = split-pages 717 # sp = split-pages
718 my @sp_cases = ( 718 my @sp_cases = (
719 [11, '%d at beginning', '', '%d_split-out.zdf'], 719 [11, '%d at beginning', '', '%d_split-out.zdf'],
@@ -722,12 +722,24 @@ my @sp_cases = ( @@ -722,12 +722,24 @@ my @sp_cases = (
722 [11, 'pdf extension', '', 'split-out.Pdf'], 722 [11, 'pdf extension', '', 'split-out.Pdf'],
723 [4, 'fallback', '--pages 11-pages.pdf 1-3 minimal.pdf --', 'split-out'], 723 [4, 'fallback', '--pages 11-pages.pdf 1-3 minimal.pdf --', 'split-out'],
724 ); 724 );
725 -$n_tests += 1; 725 +$n_tests += 5;
726 for (@sp_cases) 726 for (@sp_cases)
727 { 727 {
728 $n_tests += 1 + $_->[0]; 728 $n_tests += 1 + $_->[0];
729 } 729 }
730 730
  731 +$td->runtest("split page group > 1",
  732 + {$td->COMMAND => "qpdf --static-id --split-pages=5 11-pages.pdf" .
  733 + " split-out-group.pdf"},
  734 + {$td->STRING => "", $td->EXIT_STATUS => 0},
  735 + $td->NORMALIZE_NEWLINES);
  736 +foreach my $f ('01-05', '06-10', '11-11')
  737 +{
  738 + $td->runtest("checkout group $f",
  739 + {$td->FILE => "split-out-group-$f.pdf"},
  740 + {$td->FILE => "split-exp-group-$f.pdf"});
  741 +}
  742 +
731 $td->runtest("no split-pages to stdout", 743 $td->runtest("no split-pages to stdout",
732 {$td->COMMAND => "qpdf --split-pages 11-pages.pdf -"}, 744 {$td->COMMAND => "qpdf --split-pages 11-pages.pdf -"},
733 {$td->FILE => "split-pages-stdout.out", $td->EXIT_STATUS => 2}, 745 {$td->FILE => "split-pages-stdout.out", $td->EXIT_STATUS => 2},
qpdf/qtest/qpdf/split-exp-group-01-05.pdf 0 โ†’ 100644
No preview for this file type
qpdf/qtest/qpdf/split-exp-group-06-10.pdf 0 โ†’ 100644
No preview for this file type
qpdf/qtest/qpdf/split-exp-group-11-11.pdf 0 โ†’ 100644
No preview for this file type