Re: Broken PDFs when opened in Acrobat when using egstate

Alfred Reibenschuh <[email protected]> Mon, 10 Mar 2008 22:42:15 +0100
Newsgroups gmane.comp.lang.perl.modules.pdfapi2
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

hi!

actually egstate (and hence transparency) only works in graphics mode,
but not text mode.

so the following example i've just checked into cvs
shows how it is done:

#!/usr/bin/perl
#=======================================================================
#    ____  ____  _____              _    ____ ___   ____
#   |  _ \|  _ \|  ___|  _   _     / \  |  _ \_ _| |___ \
#   | |_) | | | | |_    (_) (_)   / _ \ | |_) | |    __) |
#   |  __/| |_| |  _|    _   _   / ___ \|  __/| |   / __/
#   |_|   |____/|_|     (_) (_) /_/   \_\_|  |___| |_____|
#
#   A Perl Module Chain to faciliate the Creation and Modification
#   of High-Quality "Portable Document Format (PDF)" Files.
#
#   Copyright 1999-2004 Alfred Reibenschuh <[email protected]>.
#
#=======================================================================
#
#   PERMISSION TO USE, COPY, MODIFY, AND DISTRIBUTE THIS FILE FOR
#   ANY PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT
#   THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL
#   COPIES.
#
#   THIS FILE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS FILE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
#
#   $Id: 060_transparency,v 1.1 2008/03/10 21:38:43 areibens Exp $
#
#=======================================================================
use lib '../lib/';
use PDF::API2;
use PDF::API2::Util;
use POSIX;


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

my $TRANSPARENT = $pdf->egstate; # Called just once
$TRANSPARENT->transparency(0.9);

$fnt=$pdf->corefont('Verdana-Bold');

$page = $pdf->page;
$gfx=$page->gfx;

$gfx->textlabel(30,750,$fnt,100,'100% Opaque',-color=>'#F00');

$gfx->save;
$gfx->egstate( $TRANSPARENT );
$gfx->textlabel(30,720,$fnt,100,'90% Transparent',-color=>'#000');
$gfx->restore;

$page = $pdf->page;
$text=$page->text;

$text->save;
$text->font( $fnt, 100 );
$text->fillcolor( 'red' );
$text->translate( 570, 750 );
$text->text_right( 'Opaque 100%' );
$text->fill;
$text->restore;

$text->textend;
$text->save;
$text->egstate( $TRANSPARENT );
$text->textstart;
$text->font( $fnt, 100 );
$text->fillcolor( 'black' );
$text->translate( 570, 720 );
$text->text_right( 'Transparent 90%' );
$text->fill;
$text->textend;
$text->restore;
$text->textstart;

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

exit;

__END__


cheers,

- --
fredo


Rick Measham wrote:
| I'm getting some really annoying behaviour and I'm hoping someone can
| work it out for me.
|
| (using cpan://A/AR/AREIBENS/PDF-API2-0.69.tar.gz)
|
| I want to overlay an almost-transparent number in a number of places on
| each page. I'm doing it using the following code:
|
|     my $TRANSPARENT = $pdf->egstate; # Called just once
|     $TRANSPARENT->transparency(0.9);
|
|     ...
|
|     $text->save;
|     $text->egstate( $TRANSPARENT );
|     $text->font( $FONT_NUMBERS_FACE, $FONT_NUMBERS_SIZE );
|     $text->fillcolor( 'black' );
|     $text->translate( $PAGE_WIDTH - $right_margin, 0 );
|     $text->text_right( $photo_number );
|     $text->fill;
|     $text->restore;
|
| However:
| * in kpdf, that is causing all subsequent text on that page to still use
| the egstate -- as if the restore wasn't there.
| * in Acrobat 8, that doesn't cause transparency at all, just a light
| grey that sits on top of everything.
| http://rick.measham.id.au/paste/whacked.pdf
|
| If I take out the egstate line, then it works just fine in both (but of
| course isn't transparent)
| http://rick.measham.id.au/paste/fine.pdf
|
| If I change all references to $gfx rather than $text, then it works just
| fine in kpdf, but Acrobat complains that the PDF has errors and I need
| to talk to whoever gave me the PDF.
| http://rick.measham.id.au/paste/broken.pdf
|
| Is there an option that I've missed that will let me set the numbers to
| almost-transparent that works in all (or at least Adobe's) PDF readers?
|
| Cheers!
| Rick Measham
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFH1as3jKJMaHhpyr4RCEmcAJ0V9P1xie8FSl6ofW8AUBq9RYrDPACgmryF
OK+7eEz+YcFdyv+yD5rD+wk=
=VFYr
-----END PGP SIGNATURE-----