PhilterPaper / Perl-PDF-Builder

Extended version of the popular PDF::API2 Perl-based PDF library for creating, reading, and modifying PDF documents
https://www.catskilltech.com/FreeSW/product/PDF%2DBuilder/title/PDF%3A%3ABuilder/freeSW_full
Other
6 stars 7 forks source link

CTS 31 - How to find limits of textlabel? #122

Closed PhilterPaper closed 3 years ago

PhilterPaper commented 3 years ago

Hello,

sorry for the beginners question:

When I use the textlabel command:

$text->textlabel(50, 100, $font, 10, "Hello World", -hscale=>80 );

How can I find out where on the paper the right end of this text will be? I would like to know, so that other elements of the page do not overlap or collide with the text.

greetings Kurt

PhilterPaper commented 3 years ago

Reply #1 by Phil

The textlabel() call does not appear to have been intended for anything more than one-off labels. I will take a look at it to see if I can do anything about its leaving the "next write" position defined (@where = $text->textpos();). In the meantime, you can either

  1. use the text width returned by textlabel() and add it to your "x" coordinate

  2. use $text->text("Hello World"); instead, which leaves the "next write" position defined

An example of both:

use warnings;
use strict;
use PDF::Builder;

my $pdf = PDF::Builder->new();
my $page = $pdf->page();
my $text = $page->text();
my $font = $pdf->corefont("times-roman");

# output via textlabel()
my $w = $text->textlabel(50,100, $font,10, "Hello World (textlabel)", -hscale=>80);
$text->textlabel(50+$w,100, $font,10, "Next write here", -hscale=>80);

# output via text() or text_left()
$text->font($font, 10);
$text->translate(50,200);
$text->hscale(80);
$text->text("Hello World (text)");
$text->text("Next write here");

$pdf->saveas("where.pdf");

There is also $text->advancewidth("text") to tell you the width of a slug of text, in the current font and size.

I will move this to the bugs section soon, as either the code or the documentation should be updated.

PhilterPaper commented 3 years ago

stueber commented

Dear Phil,

Thanks a lot for your elaborate answer. I tested the program snippet you provided and it works fine for me :)

Greetings Kurt

PhilterPaper commented 3 years ago

I don't see a clean way to change the behavior of textlabel() to make it play nicely with other text operations, so I have updated the documentation to clarify that it is a standalone operation.

Closing.