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

This code generates a letter fetching details from database and print database field values and the static text on a PDF.

I am trying to right align the amounts fields (code lines 444 through 479), which is not working because I think I am not able to convert string length to pixels to print them on PDF.

I tried calculating the start position (x coordinate) of the Amount Delayed1 field by substracting length of Amount Delayed1 field from total length (length of Item Amount + start position of Item Amount), i.e (140 + 7)(start position + len of item amount) - 5 (len of amount delayed1) but this logic doesn't position the amount delayed as right aligned.

use strict; use warnings; use PDF::API2; use PDF::Create; use DB; use DBI; use Time::Format;# qw(%time %strftime %manip); use PDF::API2::Annotation; use PDF::Table; use constant mm => 25.4 / 72; use constant in => 1 / 72; use constant pt => 1; # Connect to Oracle Database and return data using a query and store + it in a variable my $user = "scott"; my $password = "tiger"; my $db=DBI->connect("dbi:Oracle:HOSTNAME:1521/ORCL", "$user", "$pa +ssword") || die($DBI::errstr . "\n"); $db->{AutoCommit} = 0; $db->{RaiseError} = 1; $db->{ora_check_sql} = 0; $db->{RowCacheSize} = 16; my $SEL = "select acc_num, acc_name, item_amt, amt_delayed1, available_date1,date_deposited, amt_delayed2, available_date2, dt_today, case when substr(acc_num,1,2)='IM' then 'X' else NULL end as chk_X, case when substr(acc_num,1,2)='ST' then 'X' else NULL end as sav_X, hold_reason from ( select 'AI12345678, AI654321' as acc_num, 'First Middle Last' as acc_n +ame, '7000.00' as item_amt, '40.00' as amt_delayed1, to_char(trunc(sy +sdate)+1,'mm/dd/yyyy') as available_date1, to_char(trunc(sysdate)-2,'mm/dd/yyyy') as date_deposited, '3000.00' as + amt_delayed2, to_char(trunc(sysdate)+2,'mm/dd/yyyy') as available_da +te2, to_char(trunc(sysdate),'mm/dd/yyyy') as dt_today, 'Large Deposit,The Check(s) you deposited on this day exceed \$5000' a +s hold_reason from dual union select 'AS555666777, AS65432155' as acc_num, 'First Middle Last1,First + Middle Last2' as acc_name, '2400.00' as item_amt, '1000.00' as amt_d +elayed1, to_char(trunc(sysdate)+1,'mm/dd/yyyy') as available_date1, to_char(trunc(sysdate)-2,'mm/dd/yyyy') as date_deposited, '1400.00' as + amt_delayed2, to_char(trunc(sysdate)+2,'mm/dd/yyyy') as available_da +te2, to_char(trunc(sysdate),'mm/dd/yyyy') as dt_today, 'Large Deposit2,The Check(s) you deposited on this day exceed \$10000' + as hold_reason from dual)"; my $sth = $db->prepare($SEL); $_ = "\t" if !defined($_); my $fileTime = $time{'yyyymmdd.hhmmss.mmm'}; my $picture = 'C:\\Users\\Public\\Pictures\\BOTW 3x1 lockup rbg.jp +g'; my ( $paragraph1, $paragraph2,$paragraph3,$paragraph4) = get_data( +); my ( $endw, $ypos, $paragraph ) = undef; my $ctr = 0; $sth->execute(); while (my @v_data = $sth->fetchrow_array()) { my ($acc_num, $acc_name, $item_amt, $amt_delayed1, $avl_da +te1, $dt_deposited, $amt_delayed2, $avl_date2,$dt_today,$chk_X,$sav_X +,$hold_reas) = @v_data; my $len_item_amt = length $item_amt; my $len_amt_delayed1 = length $amt_delayed1;#$amt_delayed1 +; print "Length of Amount Delayed1: ".$len_amt_delayed1; print $hold_reas; my $pdf = PDF::API2->new( -file => "$0.$ctr.pdf" ); print "$0.$ctr.pdf\n"; my %font = ( Helvetica => { Bold => $pdf->corefont( 'Helvetica-Bold', -encoding = +> 'latin1' ), Roman => $pdf->corefont( 'Helvetica', -encoding = +> 'latin1' ), Italic => $pdf->corefont( 'Helvetica-Oblique', -encoding = +> 'latin1' ), }, Times => { Bold => $pdf->corefont( 'Times-Bold', -encoding => 'la +tin1' ), Roman => $pdf->corefont( 'Times', -encoding => 'la +tin1' ), Italic => $pdf->corefont( 'Times-Italic', -encoding => 'la +tin1' ), }, Arial => { Bold => $pdf->corefont( 'Arial-Bold', -encoding => 'la +tin1' ), Roman => $pdf->corefont( 'Arial', -encoding => 'la +tin1' ), Italic => $pdf->corefont( 'Arial-Italic', -encoding => 'la +tin1' ), }, ); my $page = $pdf->page; $page->mediabox( 215.9 / mm, 279.4 / mm); ##Correct dimension +s of page for a PDF for A4 paper printing## ###########Block to print image on the page########### my $photo = $page->gfx; die("Unable to find image file: $!") unless -e $picture; my $photo_file = $pdf->image_jpeg($picture); #$photo->image( $photo_file, 20, 710.0 , 280, 50); $photo->image( $photo_file, 20, 610.0, 3/mm, 7/mm); my $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 16 / pt ) +; $right_column_text->fillcolor('black'); my ( $endw, $ypos, $paragraph ) = text_block( $right_column_text, 'NOTICE OF HOLD', -x => 425 , -y => 725,#555, -w => 160 , -h => 110 , -lead => 10 / pt, -parspace => 0 / pt, -align => 'right', ); my $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $paragraph1, -x => 30 , -y => 335, -w => 550,#441.5, -h => 110, -lead => 11 / pt, -parspace => 0 / pt, -align => 'justify', ); my ($hold_reas1,$hold_reas2,$hold_reas3,$hold_reas4,$hold_reas +5) = split(',',$hold_reas); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $hold_reas1."\n".$hold_reas2."\n", -x => 30 , -y => 290, -w => 550,#441.5, -h => 110, -lead => 12 / pt, -parspace => 0 / pt, -align => 'justify', ); # $left_column_text = $page->text; # $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); # $left_column_text->fillcolor('black'); # ( $endw, $ypos, $paragraph ) = text_block # ( # $left_column_text, # $hold_reas2, # -x => 30 , # -y => 280, # -w => 550,#441.5, # -h => 110, # -lead => 11 / pt, # -parspace => 0 / pt, # -align => 'justify', # ); ###########Text showing NOTICE block on the letter########### $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $paragraph2, -x => 30 , -y => 206,#306,(This is last perfect position) -w => 550,#441.5, -h => 110, -lead => 11 / pt, -parspace => 0 / pt, -align => 'justify', ); ###########Text showing Phone, URL and FDIC block on the letter### +######## $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $paragraph3, -x => 30 , -y => 106, -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 8 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $paragraph4, -x => 460,#380 , -y => 40,#106, -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); ###########Account and Hold Details labels and data on the letter +- LEFT########### $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, 'Account Number:', -x => 30 , -y => 505,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, 'Name:', -x => 30 , -y => 485,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, 'Amount of Item(s):', -x => 30 , -y => 425,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, 'Amount Delayed:', -x => 30 , -y => 405,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, 'Available Date:', -x => 30 , -y => 385,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, '(Date funds are available for withdrawal)', -x => 30 , -y => 375,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); ###########Account and Hold Details labels on the letter - RIGHT## +######### $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, 'Checking:', -x => 380 , -y => 505,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, 'Today'.''.'s Date:', -x => 380 , -y => 485,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, 'Date Deposited:', -x => 380 , -y => 425,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, 'Amount Delayed:', -x => 380 , -y => 405,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, 'Available Date:', -x => 380 , -y => 385,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, '(Date funds are available for withdrawal)', -x => 380 , -y => 375,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); ###########Account and Hold Details Data on the letter - LEFT##### +###### $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $acc_num, #'Account Number data:', -x => 140 , -y => 505,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); my ($v_name1,$v_name2) = split(',',$acc_name); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $v_name1."\n".$v_name2,#Name data:', -x => 140 , -y => 485,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 12 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $item_amt,#'Amount of Item(s) data:', -x => 140 , -y => 425,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); my $v_pos1 = 140 + $len_item_amt; print $v_pos1."\n\n"; print "len item amt : ".$len_item_amt."\n\n"; print "len amt delayed1: ".$len_amt_delayed1."\n\n"; print "x position: $v_pos1-$len_amt_delayed1"; $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $amt_delayed1,#'Amount Delayed data:', -x => $v_pos1 - $len_amt_delayed1,#140 , -y => 405,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $avl_date2,#'Available Date data:', -x => 140 , -y => 385,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); ###########Account and Hold Details Data on the letter - RIGHT#### +####### $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, 'Savings:', -x => 490 , -y => 505,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, $chk_X,#'Checking X:', -x => 550 , -y => 505,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, $sav_X,#'Savings X:', -x => 550 , -y => 505,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, $dt_today,#'Today'.''.'s Date Data:', -x => 490 , -y => 485,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, $dt_deposited,#'Date Deposited Data:', -x => 490 , -y => 425,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, $amt_delayed2,#'Amount Delayed Data:', -x => 490 , -y => 405,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); $right_column_text = $page->text; $right_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $right_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $right_column_text, $avl_date1,#'Available Date Data:', -x => 490 , -y => 385,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', ); ###########Draw Lines Botom########### my $black_line_bottom = $page->gfx; $black_line_bottom->strokecolor('black'); $black_line_bottom->move( 579, 360 );#( 528, 410 ); $black_line_bottom->line( 30, 360 ); $black_line_bottom->stroke; ###########Draw Lines Top########### my $black_line_top = $page->gfx; $black_line_top->strokecolor('black'); $black_line_top->move( 579, 525 );#( 528, 410 ); $black_line_top->line( 30, 525 ); $black_line_top->stroke; ####################Customer Name And Address Positioning######### +########## $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block( $left_column_text, 'GEORGE WASHINGTON', # 'Customer Name', -x => 90 , -y => 605,#625,#555, -w => 1000 , -h => 1100 , -lead => 7 / pt, -parspace => 0 / pt, -align => 'left' # -hang => "\xB20 " ); $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block( $left_column_text, 'THE WHITE HOUSE', # 'Address', -x => 90 , -y => 590,#610,#555, -w => 1000 , -h => 1110 , -lead => 7 / pt, -parspace => 0 / pt, -align => 'left' # -hang => "\xB20 " ); $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block( $left_column_text, '1600 PENNSYLVANIA AVENUE NW', # 'City', -x => 90 , -y => 575,#595,#555, -w => 1000 , -h => 1110 , -lead => 7 / pt, -parspace => 0 / pt, -align => 'left' # -hang => "\xB20 " ); $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block( $left_column_text, 'WASHINGTON, DC 20500', # 'State and Zip', -x => 90 , -y => 560,#580,#555, -w => 1000 , -h => 1110 , -lead => 7 / pt, -parspace => 0 / pt, -align => 'left' # -hang => "\xB20 " ); $ctr++; $pdf->save; $pdf->end(); } END { $db->disconnect if defined($db); } sub get_data{ qq|The item(s) described above, included in your deposit, is drawn + on another financial institution. We are delaying the availability +of this item(s) because the funds from the check have not been collec +ted. If this time period is greater than 2 days it is becuause:|, qq|Notice: If you did not receive this notice at the time you made + the deposit or cashed a check, and the check you deposited/cashed is + paid, we will return to you any fee for the overdraft or returned ch +ecks that result solely from the additional delay that we are imposin +g. To obtain a refund of such fee, |, qq|400-400-4000 www.google.com XXXXX|, qq|(Disclosure revision: 02/2015)| } sub text_block { #my ( $endw, $ypos, $paragraph ) = undef; #my $text_object = shift; #my $text = shift; my $text_object = shift; my $text = shift; my %arg = @_; # Get the text in paragraphs my @paragraphs = split( /\n/, $text ); # calculate width of all words my $space_width = $text_object->advancewidth(' '); my @words = split( /\s+/, $text ); my %width = (); foreach (@words) { next if exists $width{$_}; $width{$_} = $text_object->advancewidth($_); } $ypos = $arg{'-y'}; my @paragraph = split( / /, shift(@paragraphs) ); my $first_line = 1; my $first_paragraph = 1; # while we can add another line while ( $ypos >= $arg{'-y'} - $arg{'-h'} + $arg{'-lead'} ) { unless (@paragraph) { last unless scalar @paragraphs; @paragraph = split( / /, shift(@paragraphs) ); $ypos -= $arg{'-parspace'} if $arg{'-parspace'}; last unless $ypos >= $arg{'-y'} - $arg{'-h'}; $first_line = 1; $first_paragraph = 0; } my $xpos = $arg{'-x'}; # while there's room on the line, add another word my @line = (); my $line_width = 0; if ( $first_line && exists $arg{'-hang'} ) { my $hang_width = $text_object->advancewidth( $arg{'-hang'} + ); $text_object->translate( $xpos, $ypos ); $text_object->text( $arg{'-hang'} ); $xpos += $hang_width; $line_width += $hang_width; $arg{'-indent'} += $hang_width if $first_paragraph; } elsif ( $first_line && exists $arg{'-flindent'} ) { $xpos += $arg{'-flindent'}; $line_width += $arg{'-flindent'}; } elsif ( $first_paragraph && exists $arg{'-fpindent'} ) { $xpos += $arg{'-fpindent'}; $line_width += $arg{'-fpindent'}; } elsif ( exists $arg{'-indent'} ) { $xpos += $arg{'-indent'}; $line_width += $arg{'-indent'}; } while ( @paragraph and $line_width + ( scalar(@line) * $space_width ) + $width{ $paragraph[0] } < $arg{'-w'} ) { $line_width += $width{ $paragraph[0] }; push( @line, shift(@paragraph) ); } # calculate the space width my ( $wordspace, $align ); if ( $arg{'-align'} eq 'fulljustify' or ( $arg{'-align'} eq 'justify' and @paragraph ) ) { if ( scalar(@line) == 1 ) { @line = split( //, $line[0] ); } $wordspace = ( $arg{'-w'} - $line_width ) / ( scalar(@line +) - 1 ); $align = 'justify'; } else { $align = ( $arg{'-align'} eq 'justify' ) ? 'left' : $arg{' +-align'}; $wordspace = $space_width; } $line_width += $wordspace * ( scalar(@line) - 1 ); if ( $align eq 'justify' ) { foreach my $word (@line) { $text_object->translate( $xpos, $ypos ); $text_object->text($word); $xpos += ( $width{$word} + $wordspace ) if (@line); } $endw = $arg{'-w'}; } else { # calculate the left hand position of the line if ( $align eq 'right' ) { $xpos += $arg{'-w'} - $line_width; } elsif ( $align eq 'center' ) { $xpos += ( $arg{'-w'} / 2 ) - ( $line_width / 2 ); } # render the line $text_object->translate( $xpos, $ypos ); $endw = $text_object->text( join( ' ', @line ) ); } $ypos -= $arg{'-lead'}; $first_line = 0; } unshift( @paragraphs, join( ' ', @paragraph ) ) if scalar(@paragra +ph); return ( $endw, $ypos, join( "\n", @paragraphs ) ) }
Thank you. OSexplorer

Replies are listed 'Best First'.
Re: Dynamic text (amount in decimal) not aligned on PDF
by poj (Abbot) on Nov 15, 2014 at 11:38 UTC
    length gives the number of characters, you need to use $content->advancewidth($text). Here is a demo
    #!perl use strict; use PDF::API2; my $pdf = new PDF::API2(-file => "align.pdf"); $pdf->mediabox(842, 595); #A4 Landscape my $page = $pdf->page(); my $font = $pdf->corefont('helvetica'); my $gfx = $page->gfx(); $gfx->move(600,595);$gfx->vline; $gfx->move(200,595);$gfx->vline; $gfx->stroke; my $content = $page->text(); $content->font($font,24); my $y = 500; for my $text (1,1.2,1.23,12.34,123.45,12345.67,123456.78){ my $len = length $text; my $width = $content->advancewidth($text); $content->translate(200,$y); $content->text("$text ($len)($width) left align"); $y -= 30;; $content->translate(600,$y); $content->text_right("right align $text"); $y -= 30;; } $pdf->saveas();
    poj

      Thank you RonW and poj! I forgot to mention in my post that am new to Perl (and have started loving it!), so please excuse me for stupid questions. I apologize for posting my complete code, but I want to make sure Monks are able to understand what I intend to do.

      RonW I tried using fixed width font but it didn't work. I don't know if I did something wrong.

      POJ, I think I partially understood your demo code, and have a question - the advancewidth method would help me in setting the x position of the data field or the width of it?

      The code below is the amount field which I want to align right and should get placed under the amount field above it. The text_block method already has -align argument (set to justify) and changing this to right, places the entire text to the right of the page.

      The amount fields on my PDF looks like this (a grid)- 400.00 has to right align with 10000.00:
      Item Amount 10000.00 Date Deposit 11/17/2014
      Amount Delayed 400.00 Amount Delayed 10.00

      my $v_pos1 = 140 + $len_item_amt; print $v_pos1."\n\n"; print "len item amt : ".$len_item_amt."\n\n"; print "len amt delayed1: ".$len_amt_delayed1."\n\n"; print "x position: $v_pos1-$len_amt_delayed1"; $left_column_text = $page->text; $left_column_text->font( $font{'Arial'}{'Roman'}, 10 / pt ); $left_column_text->fillcolor('black'); ( $endw, $ypos, $paragraph ) = text_block ( $left_column_text, $amt_delayed1,#'Amount Delayed data:', -x => $v_pos1 - $len_amt_delayed1,#140 , -y => 405,#306,(This is last perfect position) -w => 460,#441.5, -h => 110, -lead => 10 / pt, -parspace => 0 / pt, -align => 'justify', );
      Thank you. OSexplorer
        Try this
        # remove these #my $len_item_amt = length $item_amt; #my $len_amt_delayed1 = length $amt_delayed1;#$amt_delayed1; # replace this line # my $v_pos1 = 140 + $len_item_amt; my $w1 = $left_column_text->advancewidth($item_ant); my $w2 = $left_column_text->advancewidth($amt_delayed1); # replace this line # -x => $v_pos1 - $len_amt_delayed1,#140 , -x => 140 + $w1 - $w2;
        poj

      Monk you gave me this piece of code when I needed the most and I was clueless how to align amount field with another on a PDF. Thank you very much!! You are the perfect and the most patient "mentor" I've ever had..Thanks for helping!!

      Thank you. OSexplorer
Re: Dynamic text (amount in decimal) not aligned on PDF
by RonW (Parson) on Nov 14, 2014 at 22:48 UTC

    Too much code to figure out.

    However, did you try using a fixed width font for the amount column?