Programatically How do I reset colors in PDF
I am creating a PDF file and I would like to generate several rectangles on the page However once I fill a rectangle in color I cannot reset the fill color (non-stroking color) to clear, white, transparent or whatever is needed so that no color is in the rectangle
I use t开发者_运维百科his to set the stroke and non-stroke to red
1.0 0.0 0.0 rg
1.0 0.0 0.0 RG
I set the line width
1 w
I draw a rectangle and it appears as a red rectangle (outlined and filled in red)
0046.8 0633.6 0237.6 0100.8 re
B
Now I set the stroke to black
0.0 0.0 0.0 RG
I attempt to create another rectangle (I only want black outline)
0072.0 0576.0 0288.0 0144.0 re
B
But the second rectangle appears with a black outline but is filled in red? How do I get red of the red fill?
Your problem is your use of B
. If you interchange the two drawing operations you'll see that (what was) the second rectangle is now filled with black, because black is the default color. You should use S
for the second rectangle, so that that path is stroked but not filled.
By the way, you're missing some really easy ways to reduce the size of your generated PDF:
- Use
g
/G
instead ofrg
/RG
when all three numbers are the same. 1 w
is unnecessary (unless returning to that setting from a different one), 1 is the default value for stroke width.- Don't print trailing
.0
or leading zeroes on your numbers.
Putting all which together, here's what your generated page stream should look like:
1 0 0 rg 1 0 0 RG
46.8 633.6 237.6 100.8 re B
0 G
72 576 288 144 re S
精彩评论