Hi Mark,

Thanks for your help!
You have got me on the right track.
There are just a few points which I am having problems with when  
moving the example to a real application.

1 - I'm trying to implement this using Graphics2D, as I have some  
quite complex geometry stored as Java2D GeneralPath objects.
2 - There will also be multiple transparent gradients on top of each  
other.

I have worked up another short test using Java2D & multiple  
transparent gradients here:
http://al.chemy.org/misc/TransparentGradient2.java
This is the correct Java2D output as a PNG:
http://al.chemy.org/misc/TransparentGradient2DesiredOutput.png
And here is what my code using itext renders:
http://al.chemy.org/misc/TransparentGradient2CurrentOutput.pdf

There are several issues with this example:
1 - The clipping paths/groups do not seem to be ordered correctly,  
they seem to be nestled. This is most likely a simple error on my part  
somewhere? But it is causing the following error when I try to open  
the file in Adobe Illustrator "An unknown shading type was  
encountered." It also gets rendered incorrectly (the same in Adobe  
Reader): http://al.chemy.org/misc/TransparentGradient2Illustrator.png
2 - The ordering of the blue triangle gradient seems to be inverted -  
something to do with the y axis being flipped in Java2D? But when I  
invert the gradient colours even the white area of the triangle goes  
red - this could be related to the clipping path/group problem.

Thanks again for your help.

Best Regards,

Karl


On 2009/04/10, at 3:09, Mark Storer wrote:

> I'm not sure how to /fix/ it, but I can see what the problem is.
>
> You want a consistent color with varying alpha, composited with the  
> background.  What you have is consistent alpha with varying color,  
> written over the background.
>
> Your shading is just used for color.  Not sure why.  And you want a  
> range [0] to [1] alpha, not [1 0 0] (red) to [1 1 1] (white) RGB.
>
> Only that's not right.  If it were written entirely over the  
> background, you wouldn't see the blue rect behind at all... but the  
> white area IS stomping it entirely.  So I'm confused to.
>
> Ah!  You're using Luminosity.  White is opaque, red less so.  That  
> makes sense.
>
> So how does one specify alpha?  First, you'll need to use an Alpha  
> group, not a Luminosity one...
>
> Aha!
>
> Your alpha comes from the graphics state's SMask entry, for which  
> you're using your shading XObject.  Okay.  But use "Alpha" instead  
> of luminosity, and I think you can get away with DeviceRGB [0] to [1].
>
> Your red box is also coming from that shading, which is part of the  
> issue...  Lets see if I can hack on your current output to get what  
> you want...
>
> Bingo.  I stuck with DeviceRGB to make my hackage a bit easier.   
> Also, the Adobe version's color ranges are from 0.12-ish to 1,  
> rather than 0 to 1, as in the attached file.
>
> Change your code thusly:
>
> Drop:
> // Create a gradient to use as the mask
> PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), 0, 0,  
> 0, 100, Color.RED, Color.WHITE);
> template.paintShading(shading);
> //Place template
> cb.addTemplate(template, 0, 0);
>
> Replace:
> // Create a gradient to use as the mask
> PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), 0, 0,  
> 0, 100, Color.BLACK, Color.WHITE);
> template.paintShading(shading);
>
> cb.setColorFill( Color.RED );
> cb.rectangle( 0, 0, 100, 100 );
> cb.fill();
>
>
> That'll get you in the right ballpark.  You might need to mess with  
> the shading's axis, or swap BLACK & WHITE to get the direction right.
>
> --Mark Storer
>  Senior Software Engineer
>  Cardiff.com
>
> #include <disclaimer>
> typedef std::Disclaimer<Cardiff> DisCard;
>
>
>
>> -----Original Message-----
>> From: Alchemy [mailto:alch...@al.chemy.org]
>> Sent: Thursday, April 09, 2009 6:44 AM
>> To: Post all your questions about iText here
>> Subject: Re: [iText-questions] GradientPaint & Transparency
>>
>>
>> Hi Again,
>>
>> I have been playing with this today and found a possible way to
>> implement a transparent gradient working from the example here:
>> http://1t3xt.info/examples/browse/?page=example&id=414
>>
>> This is the output I am getting with following code:
>> http://al.chemy.org/misc/TransparentGradientCurrentOutput.pdf
>> http://al.chemy.org/misc/TransparentGradient.java
>>
>> And this is what I am aiming to get:
>> http://al.chemy.org/misc/TransparentGradientDesiredOutput.pdf
>>
>> As you can see there is quite a difference.
>> I am not sure if this is the right way to go about it, or even if a
>> gradient can be used as an SOFT MASK?
>>
>> Thanks in advance,
>>
>> Karl
>>
>>
>>
>> On 2009/04/09, at 15:07, Alchemy wrote:
>>
>>>>>>> When drawing to Java Graphics2D It seems like iText does not
>>> support
>>>>>>> GradientPaint gradients with transparency?
>>>>>> You're right, I don't think that's supported.
>>>>>
>>>>> I had a look at the relevant iText code and lucky for me I
>>> managed to
>>>>> get it going by only changing one line :)
>>>>> I'm using only RGB at this point so I changed
>>>>> com.lowagie.text.pdf.PdfShading line 145 to also return
>> the alpha
>>> value.
>>>>> The full function is below and the new and improved PDF output
>>> from my
>>>>> previous example is here:
>>>>> http://al.chemy.org/misc/graphics2D-2.pdf
>>>>> Beautiful isn't it!
>>>>
>>>> As far as I can see, the change breaks other examples...
>>> Yes you are right.
>>> It worked straight away in Apple's default PDF reader Preview, but
>>> unfortunately I didn't test it fully on Windows etc...
>>> Different PDF readers seem to react differently.
>>> The worst case scenario is that the files will not even open,
>>> otherwise only the transparency may be lost and the colour
>> maintained.
>>>
>>> I am still interested in implementing this, and have mocked up a
>>> transparent gradient in Adobe Illustrator and checked the
>> pdf output.
>>> In general the output is very bloated compared to iText, but it
>>> looks like a shape is drawn with a transparent mask on top
>> of it (as
>>> opposed to using the straight ARGB colours I plugged in last time).
>>> The relevant tags in the pdf go something like this:
>>> <</I false/K false/CS/DeviceRGB/S/Transparency>>
>>> ..
>>> <</OPM 1/BM/Normal/CA 1.0/OP false/SMask 18 0 R/ca 1.0/AIS
>> false/op
>>> false/Type/ExtGState/SA true>>
>>> ..
>>> <</G 17 0 R/S/Luminosity/Type/Mask/BC 19 0 R>>
>>> ..
>>> <</Subtype/Form/Length 67/Matrix[1.0 0.0 0.0 1.0 0.0
>> 0.0]/Group 8 0
>>> R/Resources<</XObject<</Fm0 16 0 R>>/ExtGState<</GS0 21 0 R>>>>/
>>> BBox[455.781 456.219 579.219 332.781]>>stream
>>>
>>> The file itself is here (a 123x123 box at x456 y456):
>>> http://al.chemy.org/misc/GradientTransparencyAiTest.pdf
>>>
>>> So my question is... what do you think is the best way to
>> implement
>>> this functionality using Graphics2D?
>>> Either with a workaround in the application or by extending iText
>>> itself.
>>>
>>> Thank you again for your time!
>>> I really appreciate it.
>>>
>>> Best,
>>> Karl
>>>
>>> -
>>> Alchemy
>>> An open drawing project
>>> http://al.chemy.org
>>> -
>>>
>>>
>>>
>>
>>
>> --------------------------------------------------------------
>> ----------------
>> This SF.net email is sponsored by:
>> High Quality Requirements in a Collaborative Environment.
>> Download a free trial of Rational Requirements Composer Now!
>> http://p.sf.net/sfu/www-ibm-com
>> _______________________________________________
>> iText-questions mailing list
>> iText-questions@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/itext-questions
>>
>> Buy the iText book: http://www.1t3xt.com/docs/book.php
>>
>>
> <TransparentGradientCurrentOutputTWEAK.pdf>


------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to