Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Dynamic text (amount in decimal) not aligned on PDF

by poj (Abbot)
on Nov 15, 2014 at 11:38 UTC ( [id://1107302]=note: print w/replies, xml ) Need Help??


in reply to Dynamic text (amount in decimal) not aligned on PDF

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

Replies are listed 'Best First'.
Re^2: Dynamic text (amount in decimal) not aligned on PDF
by osexplorer (Novice) on Nov 17, 2014 at 20:05 UTC

    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

        Yaaaaayyyy..it worked!!! Thank you so much poj! I was about to start writing the code to put that complete set of fields into a grid and would have tried aligning the text to the right.

        I am happy I got a drop of Wisdom today..Thank you Perl Monks!

        Thank you. OSexplorer
Re^2: Dynamic text (amount in decimal) not aligned on PDF
by osexplorer (Novice) on Feb 19, 2015 at 21:23 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1107302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found