srikrishnan has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I want to generate line numbers for each line on the right side margin + of the PDF. something like: The sample Text 1 The sample Text 2 The sample Text 3 likewise. Is it possible to do in perl? Regards, Srikrishnan

Replies are listed 'Best First'.
Re: line number in PDF
by ELISHEVA (Prior) on Mar 30, 2009 at 12:50 UTC
    Almost anything is possible in Perl, provided somebody has written a module to do it. You can get a listing of many of the available PDF modules by searching CPAN - there are currently 526 modules available - one is sure to get the job done.

    Best, beth

      ELISHEVA,

      as correct as your answer might be, looking at srikrishnan's writeups it seems that the OP is well aware of various CPAN modules that deal with PDFs.

      That said I think it would have been appropriate for srikrishnan to show what he tried so far.

        Thanks for your reply. Actually, i want to read where all the lines in a page, exact vertical + position of each line and put the line number straight opposite to t +he lines on right side margin of the PDF. Is it possible to read the line vertical position and make separate la +yer equal to the maintext layer on the right side and insert incremen +tal numbers? Regards, Srikrishnan
Re: line number in PDF
by CountZero (Bishop) on Mar 30, 2009 at 16:49 UTC
    Laying-out PDF documents is a big pain in the a**. Interesting as PDF::API2, it still requires you to do a lot of low level placement of various bits and pieces, so I have basically given up on it and now use LateX to lay-out my PDF documents.

    The LaTeX lineno package can add such linenumbers automatically.

    And as LateX files are pure text files, I make them with Template::Toolkit! (see, there was a Perl-link)

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks for your reply. But the original PDFs has been already created using QUARK or InDesign + or 3B2. Now the requirement is only to add line numbers straight opp +osite to each line on the right side margin.
Re: line number in PDF
by grantm (Parson) on Mar 31, 2009 at 05:08 UTC

    It may be possible - it depends on how the PDF was generated. If the option was used to convert text to curves then you're probably out of luck.

    Also the PDF file format has no concept of 'lines'. Characters in a particular font family, weight, size (etc) are placed at X/Y coordinates on the page. They can be placed in any order (ie: not just left-right, top to bottom).

    So if it were possible, one approach would be to find the y coordinate of every piece of text on the page; reduce that to a unique set; sort them and assign line numbers; go down and add the line numbers as text elements at the same y coordinate on the right hand side of the page.

    For the first part, you might find that the CAM::PDF module has some useful tools (eg: it can render just the text elements of a PDF page). Overlaying new text elements is the easy part. I tend to use PDF::Reuse but I'm sure that PDF::API2 could be used too.

      Hi, Thanks for your valuable suggestions. Regards, Srikrishnan