Two PDF 'shading' experiments

Alan Fry <ajf-n9G92GgND9yX/[email protected]> Wed, 28 Jan 2009 19:54:45 +0000
Newsgroups gmane.comp.lang.perl.modules.pdfapi2
Message-ID <[email protected]>
Radial shading

#!/usr/bin/perl

use PDF::API2;
use PDF::API2::Basic::PDF::Utils;

my $pdf=PDF::API2->new;
my $page = $pdf->page;
my $gfx=$page->gfx;
$page->mediabox(595,842);

my $sh=$pdf->shading;
$sh->{ShadingType}=PDFNum(3);
$sh->{ColorSpace}=PDFName('DeviceRGB');
$sh->{Background}=PDFArray(PDFNum(1),PDFNum(0),PDFNum(0));
$sh->{Coords}=PDFArray(
      
PDFNum(0.5),PDFNum(0.71),PDFNum(1),PDFNum(0.71),PDFNum(0.9),PDFNum(0.02)
);
$sh->{Function}=PDFDict();
$sh->{Function}->{FunctionType}=PDFNum(2);
$sh->{Function}->{Domain}=PDFArray(PDFNum(0),PDFNum(1));
$sh->{Function}->{C0}=PDFArray(PDFNum(0),PDFNum(1),PDFNum(0));
$sh->{Function}->{C1}=PDFArray(PDFNum(0),PDFNum(0),PDFNum(1));
$sh->{Function}->{N}=PDFNum(3);

my $pt=$pdf->pattern();
$pt->{Type}=PDFName('Pattern');
$pt->{PatternType}=PDFNum(2);
$pt->{Matrix}=PDFArray(PDFNum(595),PDFNum(0),PDFNum(0),PDFNum
(595),PDFNum(0),PDFNum(0));
$pt->{Shading}=$sh;

$gfx->fillcolor($pt);
$gfx->circle(298, 421, 250);
$gfx->fill;

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


Text shading

#!/usr/bin/perl -w

use PDF::API2;
use PDF::API2::Basic::PDF::Utils;

my $pdf = PDF::API2->new or die "$!\n";
my $fnt = $pdf->corefont('Times-Roman');
my $page = $pdf->page;
my $gfx = $page->gfx;

$page->mediabox(595, 842);

$sh=$pdf->shading;
$sh->{ShadingType}=PDFNum(2);
$sh->{ColorSpace}=PDFName('DeviceRGB');
$sh->{Background}=PDFArray(PDFNum(0),PDFNum(1),PDFNum(0));
$sh->{Coords}=PDFArray(PDFNum(0.119),PDFNum(0),PDFNum(0.594),PDFNum(0));
$sh->{Function}=PDFDict();
$sh->{Function}->{FunctionType}=PDFNum(2);
$sh->{Function}->{Domain}=PDFArray(PDFNum(0),PDFNum(1));
$sh->{Function}->{C0}=PDFArray(PDFNum(0),PDFNum(0),PDFNum(1));
$sh->{Function}->{C1}=PDFArray(PDFNum(1),PDFNum(1),PDFNum(1));
$sh->{Function}->{N}=PDFNum(1);

$pt=$pdf->pattern();
$pt->{Type}=PDFName('Pattern');
$pt->{PatternType}=PDFNum(2);
$pt->{Matrix}=PDFArray(
     PDFNum(842),PDFNum(0),PDFNum(0),PDFNum(842),PDFNum(0),PDFNum(0)
);
$pt->{Shading}=$sh;
$gfx->fillcolor($pt);

$gfx->rect(10,650,575,50);
$gfx->fill;

$sh2=$pdf->shading;
$sh2->{ShadingType}=PDFNum(2);
$sh2->{ColorSpace}=PDFName('DeviceRGB');
$sh2->{Background}=PDFArray(PDFNum(0),PDFNum(1),PDFNum(0));
$sh2- 
 >{Coords}=PDFArray(PDFNum(0.119),PDFNum(0),PDFNum(0.594),PDFNum(0));
$sh2->{Function}=PDFDict();
$sh2->{Function}->{FunctionType}=PDFNum(2);
$sh2->{Function}->{Domain}=PDFArray(PDFNum(0),PDFNum(1));
$sh2->{Function}->{C0}=PDFArray(PDFNum(1),PDFNum(1),PDFNum(1));
$sh2->{Function}->{C1}=PDFArray(PDFNum(1),PDFNum(0),PDFNum(0));
$sh2->{Function}->{N}=PDFNum(1);

$pt2=$pdf->pattern();
$pt2->{Type}=PDFName('Pattern');
$pt2->{PatternType}=PDFNum(2);
$pt2->{Matrix}=PDFArray(
     PDFNum(842),PDFNum(0),PDFNum(0),PDFNum(842),PDFNum(0),PDFNum(0)
);
$pt2->{Shading}=$sh2;

$gfx->fillcolor('black');
$gfx->textlabel(301, 657, $fnt, 54, 'HELLO WORLD', -align => 'center');

$gfx->fillcolor($pt2);
$gfx->textlabel(300, 658, $fnt, 54, 'HELLO WORLD', -align => 'center');

$gfx->rect(10, 600, 575, 100);
$gfx->stroke;

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