Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 146793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:40:00+00:00 2026-05-11T08:40:00+00:00

I use the following to read a PDF file and get text strings of

  • 0

I use the following to read a PDF file and get text strings of a page:

my $pdf = CAM::PDF->new($pdf_file); my $pagetree = $pdf->getPageContentTree($page_no);  # Get all text strings of the page # MyRenderer is a separate package which implements getTextBlocks and # renderText methods  my @text = $pagetree->traverse('MyRenderer')->getTextBlocks; 

Now, @text has all the text strings and start x,y of each text string.

How can I get the width (and possibly the height) of each string?

MyRenderer package is as follows:

package MyRenderer; use base 'CAM::PDF::GS'; sub new {     my ($pkg, @args) = @_;     my $self = $pkg->SUPER::new(@args);     $self->{refs}->{text} = [];     return $self; }  sub getTextBlocks {     my ($self) = @_;     return @{$self->{refs}->{text}}; }  sub renderText {     my ($self, $string, $width) = @_;     my ($x, $y) = $self->textToDevice(0,0);     push @{$self->{refs}->{text}}, {                                     str => $string,                                     left => $x,                                     bottom => $y,                                     right =>$x + $width,                                    };     return; } 

Update 1: There’s a function getStringWidth($fontmetrics, $string) in CAM::PDF. Altough there’s a parameter $fontmetrics in that function, irespective of what I pass to that parameter, the function returns the same value for a given string.

Also, I am not sure of the unit of measure the returned value uses.

Update 2: I changed the renderText function to following:

sub renderText {     my ($self, $string, $width) = @_;     my ($x, $y) = $self->textToDevice(0,0);     push @{$self->{refs}->{text}}, {                                 str => $string,                                 left => $x,                                 bottom => $y,                                 right =>$x + ($width * $self->{Tfs}),                                 font => $self->{Tf},                                 font_size => $self->{Tfs},                                };     return; } 

Note that in addition to getting font and font_size, I multiplied $width with font size to get the real width of the string.

Now, only thing missing is the height.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:40:00+00:00Added an answer on May 11, 2026 at 8:40 am

    getStringWidth() depends heavily on the font metrics you provide. If it can’t find the character widths in that data structure, then it falls back to the following code:

       if ($width == 0)    {       # HACK!!!                                                                          #warn 'Using klugy width!\n';                                                      $width = 0.2 * length $string;    } 

    which may be what you’re seeing. When I wrote that, I thought it was better than returning 0. If your font metrics seem good and you think there’s a bug in CAM::PDF, feel free to post more details and I’ll take a look.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following Javascript to read strings out of a text file
I need to read the XML from the file. I use following code: <ItemGroup>
Actually I use the following sentence read on another question in this page: SELECT
I use the following code to read data. It throws java.nio.charset.MalformedInputException. The file I
I am trying to use following code to read a Google text document. But
I use following line of code to read config.properties file in my spring mvc
I use the following to get a list of project files that need to
I use the following code to compile a cpp file to object file. g++
I Use following code for single file drag and drop. private void FormRegion2_DragEnter_1(object sender,
I need to read a text file when I start my program. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.