There's no "Helvetica-Roman" in core, your code dies on me in both question and solution. The latter, while moves in right direction, produces white text below the black rectangle, are you sure it's what you want?

Because, each invocation of either "text" or "gfx" methods of page object appends a chunk of content to content array, and you still add text to 0-th content array element, regardless of this being in the end of your program.

To original problem: the PDF spec says, that inside "text objects" (fragments of content stream bracketed between BT and ET keywords) you can only use either text-related or general graphics state related operators. I.e., drawing graphics is not allowed while inside a "text object". Some viewers/interpreters may be more forgiving, but such PDF is still broken.

Just, add text to content object, returned by "text" method of a page. And add graphics to content object returned by "gfx" method of a page. Then the correct BT-ET frames will be handled for you automatically.

use strict; use warnings; use PDF::API2; my $pdf = PDF::API2-> new; my $page = $pdf-> page; my $font = $pdf-> corefont( 'Helvetica' ); my $content = $page-> text; $content-> font( $font, 16 ); $content-> fillcolor( '#000000' ); $content-> translate( 50, 620 ); $content-> text( 'Print some text' ); $content = $page-> gfx; $content-> rect( 50, 560, 400, 18 ); $content-> fillcolor( '#000000' ); $content-> fill; $content = $page-> text; $content-> font( $font, 16 ); $content-> fillcolor( '#ffffff' ); $content-> translate( 62, 565 ); $content-> text( 'Print text in rect' ); $pdf-> saveas( 'test.pdf' );

In reply to Re: PDF API2 rect fill not displayed in android pdf viewer by vr
in thread PDF API2 rect fill not displayed in android pdf viewer by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.