Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering if there are some ways to extract title and pagenum of each page in a pdf file? Either by some applications, or by programming in some programming language with some pdf libraries?

The title of each page is supposed to be the first line of the page, for example, in slides/presentation files.

The output is supposed to be a text file, with following format:

title_of_first_page pagenum_of_first_page
title_of_second_page pagenum_of_second_page
...

Thanks and regards!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

The following script will print the first line of each page of the PDF file passed as argument, followed by a space and the line number... It uses tools from Poppler (package poppler-utils on Debian or Ubuntu).

#!/bin/bash
if="$1"
pages=$(pdfinfo "$if" | sed -nre 's/^Pages: +([0-9]+)$/\1/p')
for ((i=1; i<=$pages; i++)) ;do
    printf "%s %d\n" "$(pdftotext -f $i -l $i -layout "$if" - | head -n 1)" $i
done
share|improve this answer
    
Thanks! What does "psuedo" mean? –  Tim Sep 10 '11 at 4:51
    
If a page's title has one or two lines, is that possible to be detectable? Note that the pdf file is generated from presentation formats, such as powerpoint. –  Tim Sep 10 '11 at 5:09
    
Here psuedo doesn't specifically refer to psuedo-code. I used it in a normal (English) sense. I just meant that it's not the actual command-line arg, but it will serve the same purpose... About the multi-line header: I've just now looked at PowerPoint presentation which I converted to a PDF. The first line filtered by the above script is the first line of the first text field of any given page. The pdftotext method doesn't have any way of knowing if the originating field contains a newline. –  Peter.O Sep 10 '11 at 6:51
1  
It's spelled pseudo, and I took the liberty of removing that part anyway as it was confusing even when spelled correctly. –  Gilles Sep 10 '11 at 20:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.