You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by kl...@lhsystems.com on 2014/10/14 17:39:30 UTC

Shading of an elliptic form

Hello,
I'm validating the use of pdfbox 1.8.6 for the creation of a pdf file for an editor.

Therefore I need to create a shading within an ellipse, which looks like the following:

[cid:image002.png@01CFE7D4.3CB52060]

I wrote a test program, which creates a circular shading:


      public static void main(String[] args) {

            testLinearGradient();
            testCircularGradient();

      }
      public static void testCircularGradient() {
        try {
            float width = 600.0f;
            float height = 600.0f;
            PDRectangle sizePDF = new PDRectangle(width, height);

           PDDocument document = new PDDocument();
            PDPage page = new PDPage(sizePDF);
            document.addPage(page);

            // shading attributes
             COSDictionary dict = new COSDictionary();
             dict.setInt(COSName.SHADING_TYPE, 3);
             dict.setName(COSName.COLORSPACE, "DeviceRGB");
             COSArray coords = new COSArray();
             coords.add(COSInteger.get(300));    //x1
             coords.add(COSInteger.get(300));    //y1
             coords.add(COSInteger.get(300));    //x2
             coords.add(COSInteger.get(300));    //y2
             int radius = 300;
             coords.add(COSInteger.get(radius)); //r1
             coords.add(COSInteger.get(0));      //r2
             dict.setItem(COSName.COORDS, coords);
             // function with attributes
             COSDictionary fdict = new COSDictionary();
             fdict.setInt(COSName.FUNCTION_TYPE, 2);
             COSArray domain = new COSArray();
             domain.add(COSInteger.get(0));
             domain.add(COSInteger.get(1));
             COSArray c0 = new COSArray();
             c0.add(COSFloat.get("1"));       //color 1 red
             c0.add(COSFloat.get("0"));
             c0.add(COSFloat.get("0"));
             COSArray c1 = new COSArray();
             c1.add(COSFloat.get("0"));      // color 2 blue
             c1.add(COSFloat.get("0"));
             c1.add(COSFloat.get("1"));
             fdict.setItem(COSName.DOMAIN, domain);
             fdict.setItem(COSName.C0, c0);
             fdict.setItem(COSName.C1, c1);
             fdict.setFloat(COSName.N, 1.0f);
             dict.setItem(COSName.FUNCTION, fdict);

             PDShadingType3 shading = new PDShadingType3(dict);
             // create and add to shading resources
             page.setResources(new PDResources());
             Map shadings = new HashMap();
             shadings.put("sh1", (PDShadingResources) shading);
             page.getResources().setShadings(shadings);
             // invoke shading from content stream
             PDPageContentStream contentStream = new
             PDPageContentStream(document, page, true, false);

             contentStream.appendRawCommands("/sh1 sh");

             contentStream.close();
             document.save("c:\\pdfCircularGradient.pdf");
             document.close();

        } catch (Exception e) {
            System.out.println(" error creating pdf file." + e.toString());
        }
      }


Does anyone has an idea, how I can change the program, so it will create an elliptic shading.
Is an elliptic shading supported by the pdf specification?


Thanks
Klaus Graaf


 
Sitz der Gesellschaft / Corporate Headquarters: Lufthansa Systems AS GmbH, Norderstedt, Registereintragung / Registration: Amtsgericht Norderstedt 3688NO
Geschaeftsfuehrung / Management Board: Bernd Appel



AW: Shading of an elliptic form

Posted by kl...@lhsystems.com.
Dear Tilman,

thank very much for the hint with the transformation matrix. This way I can create the elliptic shading!

Klaus



 
Sitz der Gesellschaft / Corporate Headquarters: Lufthansa Systems AS GmbH, Norderstedt, Registereintragung / Registration: Amtsgericht Norderstedt 3688NO
Geschaeftsfuehrung / Management Board: Bernd Appel

-----Ursprüngliche Nachricht-----

Von: Tilman Hausherr [mailto:THausherr@t-online.de] 
Gesendet: Dienstag, 14. Oktober 2014 18:17
An: users@pdfbox.apache.org
Betreff: Re: Shading of an elliptic form

You can do this by using a transformation matrix with different x and y scales.

See PDFBOX-2217, it has a sample PDF.

Note that correct rendering is not yet supported.


Tilman

Am 14.10.2014 um 17:39 schrieb klaus.graaf@lhsystems.com:
>
> Hello,
>
> I'm validating the use of pdfbox 1.8.6 for the creation of a pdf file 
> for an editor.
>
> Therefore I need to create a shading within an ellipse, which looks 
> like the following:
>
> I wrote a test program, which creates a *circular* shading:
>
> *public**static**void*main(String[] args) {
>
> /testLinearGradient/();
>
> /testCircularGradient/();
>
> }
>
> *public**static**void*testCircularGradient() {
>
> *try*{
>
> *float*width = 600.0f;
>
> *float*height = 600.0f;
>
> PDRectangle sizePDF = *new*PDRectangle(width, height);
>
>            PDDocument document = *new*PDDocument();
>
>             PDPage page = *new*PDPage(sizePDF);
>
> document.addPage(page);
>
> // shading attributes
>
> COSDictionary dict = *new*COSDictionary();
>
> dict.setInt(COSName./SHADING_TYPE/, 3);
>
> dict.setName(COSName./COLORSPACE/, "DeviceRGB");
>
>              COSArray coords = *new*COSArray();
>
> coords.add(COSInteger./get/(300)); //x1
>
> coords.add(COSInteger./get/(300)); //y1
>
> coords.add(COSInteger./get/(300)); //x2
>
> coords.add(COSInteger./get/(300)); //y2
>
> *int*radius = 300;
>
> coords.add(COSInteger./get/(radius)); //r1
>
> coords.add(COSInteger./get/(0)); //r2
>
> dict.setItem(COSName./COORDS/, coords);
>
> // function with attributes
>
> COSDictionary fdict = *new*COSDictionary();
>
> fdict.setInt(COSName./FUNCTION_TYPE/, 2);
>
>              COSArray domain = *new*COSArray();
>
> domain.add(COSInteger./get/(0));
>
> domain.add(COSInteger./get/(1));
>
>              COSArray c0 = *new*COSArray();
>
> c0.add(COSFloat./get/("1"));  //color 1 red
>
> c0.add(COSFloat./get/("0"));
>
> c0.add(COSFloat./get/("0"));
>
>              COSArray c1 = *new*COSArray();
>
> c1.add(COSFloat./get/("0")); // _color_ 2 _blue_
>
> c1.add(COSFloat./get/("0"));
>
> c1.add(COSFloat./get/("1"));
>
> fdict.setItem(COSName./DOMAIN/, domain);
>
> fdict.setItem(COSName./C0/, c0);
>
> fdict.setItem(COSName./C1/, c1);
>
> fdict.setFloat(COSName./N/, 1.0f);
>
> dict.setItem(COSName./FUNCTION/, fdict);
>
>              PDShadingType3 shading = *new*PDShadingType3(dict);
>
> // create and add to shading resources
>
> page.setResources(*new*PDResources());
>
> _Map_ shadings = *new*_HashMap_();
>
> _shadings.put(__"sh1"__, (PDShadingResources) shading)_;
>
> page.getResources().setShadings(_shadings_);
>
> // invoke shading from content stream
>
> PDPageContentStream contentStream = *new*
>
>              PDPageContentStream(document, page, *true*, *false*);
>
>              contentStream.appendRawCommands("/sh1 sh");
>
>              contentStream.close();
>
>            document.save("c:\\pdfCircularGradient.pdf");
>
>           document.close();
>
>         } *catch*(Exception e) {
>
>             System./out/.println(" error creating pdf file."+ 
> e.toString());
>
>         }
>
>       }
>
> Does anyone has an idea, how I can change the program, so it will 
> create an elliptic shading.
>
> Is an elliptic shading supported by the pdf specification?
>
> Thanks
>
> Klaus Graaf
>
> Sitz der Gesellschaft / Corporate Headquarters: Lufthansa Systems AS 
> GmbH, Norderstedt, Registereintragung / Registration: Amtsgericht 
> Norderstedt 3688NO Geschaeftsfuehrung / Management Board: Bernd Appel
>
>


Re: Shading of an elliptic form

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,

My answer was a bit short. Next thing for you to do would be to have a 
look at the file from PDFBOX-2217 with the PDFDebugger command line app, 
and/or make tests with the "cm" operator.

If you still don't know what to do, just tell and I'll invest more time  
to help you.

Tilman

Am 14.10.2014 um 18:16 schrieb Tilman Hausherr:
> You can do this by using a transformation matrix with different x and 
> y scales.
>
> See PDFBOX-2217, it has a sample PDF.
>
> Note that correct rendering is not yet supported.
>
>
> Tilman
>
> Am 14.10.2014 um 17:39 schrieb klaus.graaf@lhsystems.com:
>>
>> Hello,
>>
>> I'm validating the use of pdfbox 1.8.6 for the creation of a pdf file 
>> for an editor.
>>
>> Therefore I need to create a shading within an ellipse, which looks 
>> like the following:
>>
>> I wrote a test program, which creates a *circular* shading:
>>
>> *public**static**void*main(String[] args) {
>>
>> /testLinearGradient/();
>>
>> /testCircularGradient/();
>>
>> }
>>
>> *public**static**void*testCircularGradient() {
>>
>> *try*{
>>
>> *float*width = 600.0f;
>>
>> *float*height = 600.0f;
>>
>> PDRectangle sizePDF = *new*PDRectangle(width, height);
>>
>>            PDDocument document = *new*PDDocument();
>>
>>             PDPage page = *new*PDPage(sizePDF);
>>
>> document.addPage(page);
>>
>> // shading attributes
>>
>> COSDictionary dict = *new*COSDictionary();
>>
>> dict.setInt(COSName./SHADING_TYPE/, 3);
>>
>> dict.setName(COSName./COLORSPACE/, "DeviceRGB");
>>
>>              COSArray coords = *new*COSArray();
>>
>> coords.add(COSInteger./get/(300)); //x1
>>
>> coords.add(COSInteger./get/(300)); //y1
>>
>> coords.add(COSInteger./get/(300)); //x2
>>
>> coords.add(COSInteger./get/(300)); //y2
>>
>> *int*radius = 300;
>>
>> coords.add(COSInteger./get/(radius)); //r1
>>
>> coords.add(COSInteger./get/(0)); //r2
>>
>> dict.setItem(COSName./COORDS/, coords);
>>
>> // function with attributes
>>
>> COSDictionary fdict = *new*COSDictionary();
>>
>> fdict.setInt(COSName./FUNCTION_TYPE/, 2);
>>
>>              COSArray domain = *new*COSArray();
>>
>> domain.add(COSInteger./get/(0));
>>
>> domain.add(COSInteger./get/(1));
>>
>>              COSArray c0 = *new*COSArray();
>>
>> c0.add(COSFloat./get/("1"));  //color 1 red
>>
>> c0.add(COSFloat./get/("0"));
>>
>> c0.add(COSFloat./get/("0"));
>>
>>              COSArray c1 = *new*COSArray();
>>
>> c1.add(COSFloat./get/("0")); // _color_ 2 _blue_
>>
>> c1.add(COSFloat./get/("0"));
>>
>> c1.add(COSFloat./get/("1"));
>>
>> fdict.setItem(COSName./DOMAIN/, domain);
>>
>> fdict.setItem(COSName./C0/, c0);
>>
>> fdict.setItem(COSName./C1/, c1);
>>
>> fdict.setFloat(COSName./N/, 1.0f);
>>
>> dict.setItem(COSName./FUNCTION/, fdict);
>>
>>              PDShadingType3 shading = *new*PDShadingType3(dict);
>>
>> // create and add to shading resources
>>
>> page.setResources(*new*PDResources());
>>
>> _Map_ shadings = *new*_HashMap_();
>>
>> _shadings.put(__"sh1"__, (PDShadingResources) shading)_;
>>
>> page.getResources().setShadings(_shadings_);
>>
>> // invoke shading from content stream
>>
>> PDPageContentStream contentStream = *new*
>>
>>              PDPageContentStream(document, page, *true*, *false*);
>>
>>              contentStream.appendRawCommands("/sh1 sh");
>>
>>              contentStream.close();
>>
>>            document.save("c:\\pdfCircularGradient.pdf");
>>
>>           document.close();
>>
>>         } *catch*(Exception e) {
>>
>>             System./out/.println(" error creating pdf file."+ 
>> e.toString());
>>
>>         }
>>
>>       }
>>
>> Does anyone has an idea, how I can change the program, so it will 
>> create an elliptic shading.
>>
>> Is an elliptic shading supported by the pdf specification?
>>
>> Thanks
>>
>> Klaus Graaf
>>
>> Sitz der Gesellschaft / Corporate Headquarters: Lufthansa Systems AS 
>> GmbH, Norderstedt, Registereintragung / Registration: Amtsgericht 
>> Norderstedt 3688NO
>> Geschaeftsfuehrung / Management Board: Bernd Appel
>>
>>
>
>


Re: Shading of an elliptic form

Posted by Tilman Hausherr <TH...@t-online.de>.
You can do this by using a transformation matrix with different x and y 
scales.

See PDFBOX-2217, it has a sample PDF.

Note that correct rendering is not yet supported.


Tilman

Am 14.10.2014 um 17:39 schrieb klaus.graaf@lhsystems.com:
>
> Hello,
>
> I'm validating the use of pdfbox 1.8.6 for the creation of a pdf file 
> for an editor.
>
> Therefore I need to create a shading within an ellipse, which looks 
> like the following:
>
> I wrote a test program, which creates a *circular* shading:
>
> *public**static**void*main(String[] args) {
>
> /testLinearGradient/();
>
> /testCircularGradient/();
>
> }
>
> *public**static**void*testCircularGradient() {
>
> *try*{
>
> *float*width = 600.0f;
>
> *float*height = 600.0f;
>
> PDRectangle sizePDF = *new*PDRectangle(width, height);
>
>            PDDocument document = *new*PDDocument();
>
>             PDPage page = *new*PDPage(sizePDF);
>
> document.addPage(page);
>
> // shading attributes
>
> COSDictionary dict = *new*COSDictionary();
>
> dict.setInt(COSName./SHADING_TYPE/, 3);
>
> dict.setName(COSName./COLORSPACE/, "DeviceRGB");
>
>              COSArray coords = *new*COSArray();
>
> coords.add(COSInteger./get/(300)); //x1
>
> coords.add(COSInteger./get/(300)); //y1
>
> coords.add(COSInteger./get/(300)); //x2
>
> coords.add(COSInteger./get/(300)); //y2
>
> *int*radius = 300;
>
> coords.add(COSInteger./get/(radius)); //r1
>
> coords.add(COSInteger./get/(0)); //r2
>
> dict.setItem(COSName./COORDS/, coords);
>
> // function with attributes
>
> COSDictionary fdict = *new*COSDictionary();
>
> fdict.setInt(COSName./FUNCTION_TYPE/, 2);
>
>              COSArray domain = *new*COSArray();
>
> domain.add(COSInteger./get/(0));
>
> domain.add(COSInteger./get/(1));
>
>              COSArray c0 = *new*COSArray();
>
> c0.add(COSFloat./get/("1"));  //color 1 red
>
> c0.add(COSFloat./get/("0"));
>
> c0.add(COSFloat./get/("0"));
>
>              COSArray c1 = *new*COSArray();
>
> c1.add(COSFloat./get/("0")); // _color_ 2 _blue_
>
> c1.add(COSFloat./get/("0"));
>
> c1.add(COSFloat./get/("1"));
>
> fdict.setItem(COSName./DOMAIN/, domain);
>
> fdict.setItem(COSName./C0/, c0);
>
> fdict.setItem(COSName./C1/, c1);
>
> fdict.setFloat(COSName./N/, 1.0f);
>
> dict.setItem(COSName./FUNCTION/, fdict);
>
>              PDShadingType3 shading = *new*PDShadingType3(dict);
>
> // create and add to shading resources
>
> page.setResources(*new*PDResources());
>
> _Map_ shadings = *new*_HashMap_();
>
> _shadings.put(__"sh1"__, (PDShadingResources) shading)_;
>
> page.getResources().setShadings(_shadings_);
>
> // invoke shading from content stream
>
> PDPageContentStream contentStream = *new*
>
>              PDPageContentStream(document, page, *true*, *false*);
>
>              contentStream.appendRawCommands("/sh1 sh");
>
>              contentStream.close();
>
>            document.save("c:\\pdfCircularGradient.pdf");
>
>           document.close();
>
>         } *catch*(Exception e) {
>
>             System./out/.println(" error creating pdf file."+ 
> e.toString());
>
>         }
>
>       }
>
> Does anyone has an idea, how I can change the program, so it will 
> create an elliptic shading.
>
> Is an elliptic shading supported by the pdf specification?
>
> Thanks
>
> Klaus Graaf
>
> Sitz der Gesellschaft / Corporate Headquarters: Lufthansa Systems AS 
> GmbH, Norderstedt, Registereintragung / Registration: Amtsgericht 
> Norderstedt 3688NO
> Geschaeftsfuehrung / Management Board: Bernd Appel
>
>