http://qs321.pair.com?node_id=813011

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

Hi,

In the PDF::API2 module, is there any way to align the paragraph vertically in the given region?

Example

$txt->font($font, $fontsize);<br> $txt->lead($lead);<br> $txt->translate($x, $y);<br> $overflow = $txt->paragraph('long paragraph here ...', $width, $height +, -align => 'center');

-align is for horizontal alignment. I need the paragraph aligned vertically also, like -valign => 'middle'.

Can anybody help?

Replies are listed 'Best First'.
Re: PDF::API2 paragraph vertical align
by mykl (Monk) on Dec 16, 2009 at 16:25 UTC

    I've just had a look at the source code for version 0.73 or PDF::API2, and it looks like the paragraph() method is hard-coded to work downwards from the height that you specify. It works through the provided text from start to end, fitting as much as possible on each line, then repeating with the remaining text.

    However, it does tell you how much of the height it used, when called in a list context:

    ($text_remaining, $height_remaining) = $txt->paragraph('long paragraph + here ...', $width, $height, -align => 'center');
    So if you could call this once in a non-outputting fashion - perhaps to a dummy document? - you could adjust your $height, based on the unused space. I would guess:
    $height -= $height_remaining/2; # untested!
    and then call it again, outputting to your real document. It's a bodge, but it looks like the only alternative is to write versions of paragraph(), text_fill_left(), text_fill_right() etc. that only calculate text placement but do not add it to the PDF document.

    Or is it possible to remove a trial $txt object from the document before re-generating it in the adjusted position? Perhaps some other monk can clarify.

      It worked!!!

      Instead of dummy text, I wrote the original text in white color. The background is white. So the white text is not viewable in the pdf. Is it a bad idea?? Is there any other way instead of putting the original text?

      $text->fillcolor('#ffffff'); $text->translate($x, $y); ($overflow_text, $height_remaining) = $text->paragraph(qq{text here}, +$width, $height);
      $y is re-calculated with respect to $height_remaining.
      $y -= $height_remaining / 2;

      Now I wrote the text again in black font. So now the text is vertically aligned in the given height.

      $text->fillcolor('#000000'); $text->translate($x, $y); $text->paragraph(qq{text here}, $width, $height);
      Thanks again!!!
Re: PDF::API2 paragraph vertical align
by snoopy (Curate) on Dec 17, 2009 at 02:27 UTC
    I've also been digging around in PDF::API2, following on from mykl.

    Another angle of attack is to try to solve the problem by subclassing/overriding.

    For example here's the code from PDF::API2::Content for outputting centered text.

    sub text_center { my ($self,$text,@opts)=@_; my $width=$self->advancewidth($text); return $self->text($text,-indent=>-($width/2),@opts); }
    It seems that the text method is used in all cases to output the text (left, right, centered, justified).

    The following overrides the text method, giving us the ability to disable text output at will, allowing to precompute the height of the paragraph:

    #!/usr/bin/perl package PreflightText; use common::sense; use base 'PDF::API2::Content::Text'; our $PREFLIGHT; sub text { my $self = shift; my $text = shift; return $PREFLIGHT ? $self->advancewidth($text) : $self->SUPER::text($text, @_) } ###################################################################### package main; use common::sense; use PDF::API2; my $para = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut + enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi + ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehe +nderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur +. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui o +fficia deserunt mollit anim id est laborum."; my $pdf = PDF::API2->new(); my $page = $pdf->page(0); my $txt = $page->text; bless $txt, 'PreflightText'; $txt->font($pdf->corefont('Times-Roman') => 10); $txt->transform(-translate=>[0, 0]); $txt->lead(12); my $actual_height = do { # # compute, but don't output # local(${PreflightText::PREFLIGHT}) = 1; my (undef, $height_remaining) = $txt->paragraph($para,'500', '1000 +', -align => 'center'); 1000 - $height_remaining; }; # # now do actual output of the vertically centered text # $txt->transform(-translate=>[300, 500 + $actual_height/2]); my ($text_remaining) = $txt->paragraph($para,'500', '1000', -align => +'center'); my $saveas = "/tmp/center.pdf"; warn "saving as: $saveas\n"; $pdf->saveas($saveas);
    Update July 2012:
    (1) Added saveas()
    (2) Just for correctness, changed text() override method to return advancewidth() in preflight mode - same as what PDF::API2::Content::text() returns.