You can use the advancewidth(
string ) method of the text object to tell you the exact width of a line. It will tell you the width of whatever string of characters you give it as an argument using the current font attributes of the text object.
It's important to note that it doesn't matter at all what you've already added to the PDF with that text object. It just measures the string you give it.
Here are some bits and pieces of code:
my $page = $pdf->page;
my $text = $page->text;
my $font_size = 10;
my $font = $pdf->corefont( "Times-Roman" );
my $bold = $pdf->corefont( "Times-Bold" );
my $italic = $pdf->corefont( "Times-Italic" );
$text->font( $bold, $font_size );
# ...
sub text {
my $text = $_[0];
my $x = $_[1];
my $y = $_[2];
my $alignment = $_[3];
my $string = $_[4];
$text->translate( $x, $y );
if ( $alignment eq "right" ) {
$text->text_right( $string );
} elsif ( $alignment eq "center" ) {
$text->text_center( $string );
} elsif ( $alignment eq "justified" ) {
$text->text_justified( $string );
} else {
$text->text( $string );
}
return $text->advancewidth( $string );
} # End text subroutine
Are you trying to make paragraphs that fit the page, or a section of the page? If you post some of your code, I could offer some ideas.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.