How to print table of contents of a pdf?












2














I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?










share|improve this question



























    2














    I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?










    share|improve this question

























      2












      2








      2







      I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?










      share|improve this question













      I have a pdf kinda-book file which has a table of contents as metadata in file but they are not listed on any page of the document. I want to print the file with table of contents, or print the table of contents separately. How can I do that?







      pdf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 7 hours ago









      CrabManCrabMan

      19017




      19017






















          1 Answer
          1






          active

          oldest

          votes


















          5














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }





          share|improve this answer























          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
            – ctrl-alt-delor
            6 hours ago










          • @ctrl-alt-delor code added
            – derobert
            6 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493799%2fhow-to-print-table-of-contents-of-a-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }





          share|improve this answer























          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
            – ctrl-alt-delor
            6 hours ago










          • @ctrl-alt-delor code added
            – derobert
            6 hours ago
















          5














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }





          share|improve this answer























          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
            – ctrl-alt-delor
            6 hours ago










          • @ctrl-alt-delor code added
            – derobert
            6 hours ago














          5












          5








          5






          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }





          share|improve this answer














          pdftk can dump out the "bookmarks" with, e.g., pdftk file.pdf dump_data_utf8; you'll get a bunch of Bookmark* entries buried in the rest of the metadata. grep can give just them:



          $ pdftk whatever.pdf dump_data_utf8 | grep ^Bookmark
          BookmarkBegin
          BookmarkTitle: Cover
          BookmarkLevel: 1
          BookmarkPageNumber: 1
          BookmarkBegin
          BookmarkTitle: Agenda
          BookmarkLevel: 1
          BookmarkPageNumber: 2


          The "level" is the indentation level (so a level 2 is indented from a level 1). You can format that into whatever format you want for printing.



          Here is a Perl script to print it in LaTeX format, which can then be fed to e.g., pdflatex to get a PDF file (which you could even use pdftk to prepend to your original PDF). Note this is also available at https://gitlab.com/derobert/random-toys/blob/master/pdf/pdftoc-to-latex (which is a good place to send pull requests if you want to improve it):



          #!/usr/bin/perl
          use 5.024;
          use strict;
          use warnings qw(all);
          use IPC::Run3;
          use LaTeX::Encode;
          use Encode qw(decode);

          my @levels
          = qw(chapter section subsection subsubsection paragraph subparagraph);
          my @counters;

          my ($data_enc, $data);
          run3 ['pdftk', $ARGV[0], 'dump_data_utf8'], undef, $data_enc;
          $data = decode('UTF-8', $data_enc, Encode::FB_CROAK);

          my @latex_bm;

          my $bm;
          foreach (split(/n/, $data)) {
          /^Bookmark/ or next;
          if (/^BookmarkBegin$/) {
          add_latex_bm($bm) if $bm;
          $bm = {};
          } elsif (/^BookmarkLevel: (d+)$/a) {
          ++$counters[$1 - 1];
          $#counters = $1 - 1;
          $bm->{number} = join(q{.}, @counters);
          $bm->{level} = $1 - 1;
          } elsif (/^BookmarkTitle: (.+)$/) {
          $bm->{title} = latex_encode($1);
          } elsif (/^BookmarkPageNumber: (d+)$/a) {
          $bm->{page} = $1;
          } else {
          die "Unknown Bookmark tag in $_n";
          }
          }
          add_latex_bm($bm) if $bm;

          print <<LATEX;
          \documentclass{report}
          \begin{document}
          ${ join('', @latex_bm) }
          \end{document}
          LATEX

          exit 0;

          sub add_latex_bm {
          my $bm = shift;
          my $level = $levels[$bm->{level}];
          my $number = $bm->{number};
          my $title = $bm->{title};
          my $page = $bm->{page};

          push @latex_bm, <<LINE;
          \contentsline {$level}{\numberline {$number}$title}{$page}%
          LINE
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 5 hours ago

























          answered 6 hours ago









          derobertderobert

          72.5k8153210




          72.5k8153210












          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
            – ctrl-alt-delor
            6 hours ago










          • @ctrl-alt-delor code added
            – derobert
            6 hours ago


















          • Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
            – ctrl-alt-delor
            6 hours ago










          • @ctrl-alt-delor code added
            – derobert
            6 hours ago
















          Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
          – ctrl-alt-delor
          6 hours ago




          Any you can use LaTex to take this data, and turn it into a nicely formatted, printable pdf. +1 for anyone that adds a complementary answer, on how to do this.
          – ctrl-alt-delor
          6 hours ago












          @ctrl-alt-delor code added
          – derobert
          6 hours ago




          @ctrl-alt-delor code added
          – derobert
          6 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493799%2fhow-to-print-table-of-contents-of-a-pdf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks