#! /usr/bin/perl -w

use strict;

use Text::PDF::File;
use Text::PDF::SFont;
use Text::PDF::Utils;

use constant FONT => 'Helvetica-Bold';
use constant SIZE => '12';

my $pdf = Text::PDF::File->open('yp_super_rollover.pdf', 1) # read-write
  or die $!;
my $root = $pdf->{'Root'}->realise;
my $pgs = $root->{'Pages'}->realise;
my @pglist = proc_pages($pdf, $pgs);

my $max = 0;
foreach my $p (@pglist)
{
    my $dict = $p->find_prop('Resources');
    if (defined $dict && defined $dict->{'Font'})
    {
        foreach my $k (keys %{$dict->{'Font'}})
        {
	  print "$k\n";
            next unless $k =~ m/^ap([0-9]+)/o;
            my $val = $1;
            $max = $val if $val > $max;
        }
    }
    print "-\n";
}
$max++;

my $hb = Text::PDF::SFont->new($pdf, FONT, "ap$max") || die "Can't create font ".FONT;
my $h = Text::PDF::SFont->new($pdf, 'Helvetica', "ap2");
my $size = SIZE;
my $location = '0 0';
my $text = 'AB';

my $stream = PDFDict();
my $s;
$s .= " 0 g -528 649 507 22 re f"; # black out 'Rollover'
$s .= " 1 g BT 1 0 0 1 -522 657 Tm /ap$max 12 Tf (Investment Purchase Form) Tj ET";
$s .= " 1 g -272 399 275 225 re f"; # white out right side of form
# Question 2
$s .= " 0 g BT 1 0 0 1 -269 617 Tm /ap1 8 Tf (2  Please Provide Contact Details) Tj";
$s .= " 1 0 0 1 -260 602 Tm /ap2 8 Tf (Email Address) Tj ET";
$s .= " 1 g 0.5 w -260 579 239 18 re B";
# white out left side of form
$s .= " 1 g -532 -41 300 610 re f";
# dividing line
$s .= " 0 g 1 J 0.5 w -528 569 m -20 569 l S";
# Question 3
$s .= " 0 g BT 1 0 0 1 -529 560 Tm /ap1 8 Tf (3  If you are buying managed fund units enter details below.) Tj";
$s .= " 1 0 0 1 -520 540 Tm (MANAGED FUNDS PURCHASES) '";
# Important information
$s .= " 9 TL /ap1 7 Tf (Important Information: ) '";
$s .= " /ap2 7 Tf ".$h->out_text("The Minimum Cash Balance is \$300 or 0.25\% of the investment value of your account, whichever is greater, plus any accumulated tax").' Tj '.$h->out_text('liability. Your Prosperity will automatically calculate your Minimum Cash Balance ($300 or 0.25%). Do not include this balance in your Investment Purchase Form.')." ' ET";
# Grey Box
$s .= " 0.9 g -520 200 500 320 re f";
# Column Headers
$s .= " 0 g BT 1 0 0 1 -510 505 Tm /ap2 7 Tf (Code) Tj";
$s .= " 1 0 0 1 -425 500 Tm (Fund Name) Tj";
$s .= " 1 0 0 1 -145 500 Tm (Percentage Allocated) Tj";
$s .= " 1 0 0 1  -60 510 Tm (DRP) Tj";
$s .= " 1 0 0 1  -60 500 Tm (Required) Tj";
$s .= " ET";

# Lines of Managed Funds
$s .= " 1 g 0.5 w";
my $t = 515;
foreach (1..10) {
  $t -= 22;
  my $b = $t - 17;
  # Code boxes (12 x 17)
  my $w = (279-140)/11;
  my $l = -510;
  foreach (1..6) {
    $s .= " $l $b $w 17 re B";
    $l += $w;
  }
  $s .= " -425 $b 265 17 re B"; # fund name
  $s .= " -145 $b 70 17 re B";  # $ Amount
  # percentage sign
  my $location = '-84 '.($b+6);
  $s .= " 0 g BT 1 0 0 1 $location Tm /ap2 7 Tf (\%) Tj ET 1 g";
  $s .= " -60 $b $w 17 re B";
}

# Any special instructions
$s .= " 0 g BT 1 0 0 1 -510 265 Tm /ap2 7 Tf (Any Special Instructions) Tj ET 1 g";
$t = 255;
foreach (1..3) {
  my $b = $t - 17;
  $s .= " -510 $b 475 17 re B";
  $t = $b;
}

# dividing line
$s .= " 0 g 1 J 0.5 w -528 195 m -20 195 l S";

# Question 4
$s .= " 0 g BT 1 0 0 1 -528 185 Tm /ap1 8 Tf (4  Declarations) Tj";

# declaration
my $dec1_1 = "I have read and understood the Customer Information Brochure dated 18th April 2001 for Your Prosperity's Superannuation Trust; I agree to acknowledge and";
my $dec1_2 = 'accept the terms and conditions in ';
my $dec2 = 'Section 5';
my $dec3 = ' of the Customer Information Brochure and the declarations in ';
my $dec4 = 'Section 6';
my $dec5_1 = ' of the Customer Information Brochure; and I';
my $dec5_2 = 'have received the current disclosure document for each managed fund to which this transaction instruction relates.';
my $dec6_1 = "I acknowledge that investments in Your Prosperity's Superannuation Trust are not deposits with or other liabilities of National Australia Bank Limited, or other";
my $dec6_2 = "member companies in the National Group of companies and are subject to investment risk including possible delays in repayment and loss of income and capital";
my $dec6_3 = "invested. Neither the repayment of capital or payment of income, nor the investment performance of the investments in Your Prosperity's Superannuation Trust is";
my $dec6_4 = "guaranteed by National Australia Bank Limited, Your Prosperity Ltd, any other company in the National Group of companies, Perpetual Nominees Limited or its";
my $dec6_5 = "respective related entities, the fund managers of the investments or any trustees of those investments.";
my $dec7 = 'Your Prosperity will automatically calculate your Minimum Cash Balance ($300 or 0.25%). Do not include this balance in your Investment Purchase Form.';
$s .= " 12 TL /ap2 7 Tf ";
$s .= $h->out_text($dec1_1);
$s .= " ' ";
$s .= $h->out_text($dec1_2);
$s .= " ' /ap1 7 Tf ";
$s .= $hb->out_text($dec2);
$s .= ' Tj /ap2 7 Tf ';
$s .= $h->out_text($dec3);
$s .= " Tj /ap1 7 Tf ";
$s .= $hb->out_text($dec4);
$s .= ' Tj /ap2 7 Tf ';
$s .= $h->out_text($dec5_1);
$s .= " Tj ";
$s .= $h->out_text($dec5_2);
$s .= " ' ";
$s .= $h->out_text($dec6_1);
$s .= " ' ";
$s .= $h->out_text($dec6_2);
$s .= " ' ";
$s .= $h->out_text($dec6_3);
$s .= " ' ";
$s .= $h->out_text($dec6_4);
$s .= " ' ";
$s .= $h->out_text($dec6_5);
$s .= " ' ";
$s .= $h->out_text($dec7);
$s .= " ' ET";

# signature box
{
  my ($l,$b) = (-528,9); # left, bottom
  my ($t,$m,$r) = ($b+50,$b+17,$l+240);# top, middle, right
  $s .= " 1 g $l $m m $r $m l $r $b l $l $b l $l $t l $r $t l $r $m l S 0 g"; # boxes
  my $loc = "$l ".($t + 5);
  $s .= " BT 1 0 0 1 $loc Tm /ap2 8 Tf ".$h->out_text("Member's Signature")." Tj";
  $loc = ($l+4).' '.($b+3);
  $s .= " 1 0 0 1 $loc Tm ".$h->out_text("Print full name")." Tj";
  $loc = ($l+151).' '.($t-30);
  $s .= " 1 0 0 1 $loc Tm ".$h->out_text("Date          /          /")." Tj ET";
}
#print "$s\n";
$stream->{' stream'} = $s;#"BT 1 0 0 1 $location Tm /ap$max $size Tf " . $font->out_text($text) . " Tj ET";
$pdf->new_obj($stream);

foreach my $p (@pglist) {
  $p->add_font($hb, $pdf);
  $p->add_font($h, $pdf);
  $p->{Contents} = PDFArray($p->{Contents}->elementsof, $stream);
  $pdf->out_obj($p);
}

$pdf->close_file;

sub proc_pages
{
  my ($pdf, $pgs) = @_;
  my ($pg, $pgref, @pglist);
  
  foreach $pgref ($pgs->{'Kids'}->elementsof)
    {
      $pg = $pdf->read_obj($pgref);
      if ($pg->{'Type'}->val =~ m/^Pages$/oi)
        { push(@pglist, proc_pages($pdf, $pg)); }
      else
        {
	  my $pcount;
	  $pgref->{' pnum'} = $pcount++;
	  push (@pglist, $pgref);
        }
    }
  (@pglist);
}
ok(1);
