pdf-raku / PDF-API6

Facilitates the creation and modification of PDF files
Artistic License 2.0
6 stars 3 forks source link

Valign not centering text in xforms #17

Open tbrowder opened 8 months ago

tbrowder commented 8 months ago

See results of this program:

#!/bin/env raku

use v6;
use PDF::API6;
use PDF::Page;
use PDF::XObject::Form;
use PDF::Content::Color :rgb;

my PDF::API6 $pdf .= new;
my PDF::Page $page = $pdf.add-page;
$page.media-box = [0, 0, 8.5*72, 11*72];

my $ofile = "xforms.pdf";

# create a new XObject form of size 100x50
my @BBox = [0, 0, 100, 50];
my PDF::XObject::Form $form = $page.xobject-form: :@BBox;

$form.graphics: {
    .Save;
    # color the entire form
    .FillColor = rgb(0, 0, 0); #color Black;
    .Rectangle: |@BBox;
    .paint: :fill, :stroke;
    .FillColor = rgb(1, 1, 1); #color White;
    # add some sample text
    .text: {
        .font = .core-font('Helvetica'), 14;
        .print: "White", :position[50, 25], :align<center>, :valign<center>;
    }
    .Restore;
}

# display the form a couple of times
$page.graphics: {
    .Save;
    .transform: :translate(300, 300);
    .do($form);
    .Restore;
    .Save;
    .transform: :translate(200, 300);
    .do($form);
    .Restore;
}

$pdf.save-as: $ofile;
say "see output file: $ofile";
tbrowder commented 8 months ago

When using most of the same code in a PDF::Lite document I get the same results.

dwarring commented 8 months ago

The text baseline was remaining fixed, so a single line of text wasn't being centered. This has been fixed in PDF::Content 0.7.5 by setting the baseline to match valign.