You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Eric Inman <er...@comcast.net> on 2014/08/05 04:56:28 UTC

RE: Gradient paint

The code below works for me and the gradient appears.

However, if I try to fill a rectangle by following

	contentStream.appendRawCommands("/sh1 sh");

with

        contentStream.setNonStrokingColor(Color.MAGENTA);
        contentStream.fillRect(100,100,100,100);

then the gradient no longer appears nor does the rectangle. If I put the rectangle code before the appendRawCommands call, then both the rectangle and the gradient appear.

In this simple example, switching the order is an acceptable solution, but in the real application that I'm working on, I need to do these operations in either order. Is that possible?

Thanks

-----Original Message-----
From: Tilman Hausherr <TH...@t-online.de>
Sent: Wed, 16 Jul 2014 04:42:47 GMT
To: users@pdfbox.apache.org 
Subject: Re: Gradient paint

For the 1.8 version the code is slightly different:

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

             // shading attributes
             COSDictionary dict = new COSDictionary();
             dict.setInt(COSName.SHADING_TYPE, 2);
             dict.setName(COSName.COLORSPACE, "DeviceRGB");
             COSArray coords = new COSArray();
             coords.add(COSInteger.get(100));
             coords.add(COSInteger.get(400));
             coords.add(COSInteger.get(400));
             coords.add(COSInteger.get(600));
             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"));
             c0.add(COSFloat.get("0"));
             c0.add(COSFloat.get("0"));
             COSArray c1 = new COSArray();
             c1.add(COSFloat.get("0.5"));
             c1.add(COSFloat.get("1"));
             c1.add(COSFloat.get("0.5"));
             fdict.setItem(COSName.DOMAIN, domain);
             fdict.setItem(COSName.C0, c0);
             fdict.setItem(COSName.C1, c1);
             fdict.setInt(COSName.N, 1);
             dict.setItem(COSName.FUNCTION, fdict);

             PDShadingType2 shading = new PDShadingType2(dict);

             // create and add to shading resources
             page.setResources(new PDResources());
             Map<String, PDShadingResources> shadings = new 
HashMap<String, PDShadingResources>();
             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("shtest.pdf");
             document.close();




Re: Gradient paint

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi, please send both pdf files to me directly (tilman at snafu dot de) 
and I'll answer publicly.

Tilman

Am 05.08.2014 04:56, schrieb Eric Inman:
> The code below works for me and the gradient appears.
>
> However, if I try to fill a rectangle by following
>
> 	contentStream.appendRawCommands("/sh1 sh");
>
> with
>
>          contentStream.setNonStrokingColor(Color.MAGENTA);
>          contentStream.fillRect(100,100,100,100);
>
> then the gradient no longer appears nor does the rectangle. If I put the rectangle code before the appendRawCommands call, then both the rectangle and the gradient appear.
>
> In this simple example, switching the order is an acceptable solution, but in the real application that I'm working on, I need to do these operations in either order. Is that possible?
>
> Thanks
>
> -----Original Message-----
> From: Tilman Hausherr <TH...@t-online.de>
> Sent: Wed, 16 Jul 2014 04:42:47 GMT
> To: users@pdfbox.apache.org
> Subject: Re: Gradient paint
>
> For the 1.8 version the code is slightly different:
>
>               PDDocument document = new PDDocument();
>               PDPage page = new PDPage();
>               document.addPage(page);
>
>               // shading attributes
>               COSDictionary dict = new COSDictionary();
>               dict.setInt(COSName.SHADING_TYPE, 2);
>               dict.setName(COSName.COLORSPACE, "DeviceRGB");
>               COSArray coords = new COSArray();
>               coords.add(COSInteger.get(100));
>               coords.add(COSInteger.get(400));
>               coords.add(COSInteger.get(400));
>               coords.add(COSInteger.get(600));
>               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"));
>               c0.add(COSFloat.get("0"));
>               c0.add(COSFloat.get("0"));
>               COSArray c1 = new COSArray();
>               c1.add(COSFloat.get("0.5"));
>               c1.add(COSFloat.get("1"));
>               c1.add(COSFloat.get("0.5"));
>               fdict.setItem(COSName.DOMAIN, domain);
>               fdict.setItem(COSName.C0, c0);
>               fdict.setItem(COSName.C1, c1);
>               fdict.setInt(COSName.N, 1);
>               dict.setItem(COSName.FUNCTION, fdict);
>
>               PDShadingType2 shading = new PDShadingType2(dict);
>
>               // create and add to shading resources
>               page.setResources(new PDResources());
>               Map<String, PDShadingResources> shadings = new
> HashMap<String, PDShadingResources>();
>               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("shtest.pdf");
>               document.close();
>
>
>


RE: Gradient paint

Posted by Eric Inman <er...@comcast.net>.
Thank you, that worked!

Looks like there are some general rules that I need to become familiar with.

-----Original Message-----
From: Tilman Hausherr [mailto:THausherr@t-online.de] 
Sent: Tuesday, August 5, 2014 5:22 PM
To: users@pdfbox.apache.org
Subject: Re: Gradient paint

here's the stream of the correct PDF:

1 0 1 rg
100 100 100 100 re
f
/sh1 sh



incorrect PDF #1:
/sh1 sh1 0 1 rg
100 100 100 100 re
f


incorrect PDF #2:

1 0 1 rg
100 100 100 100 re
f
/sh1 sh0 0 1 rg
100 200 100 100 re
f


===> add a newline or a space after "/sh1 sh"

I'll clarify this in the sample code.

Tilman



Am 05.08.2014 04:56, schrieb Eric Inman:
> The code below works for me and the gradient appears.
>
> However, if I try to fill a rectangle by following
>
> 	contentStream.appendRawCommands("/sh1 sh");
>
> with
>
>          contentStream.setNonStrokingColor(Color.MAGENTA);
>          contentStream.fillRect(100,100,100,100);
>
> then the gradient no longer appears nor does the rectangle. If I put the rectangle code before the appendRawCommands call, then both the rectangle and the gradient appear.
>
> In this simple example, switching the order is an acceptable solution, but in the real application that I'm working on, I need to do these operations in either order. Is that possible?
>
> Thanks
>
> -----Original Message-----
> From: Tilman Hausherr <TH...@t-online.de>
> Sent: Wed, 16 Jul 2014 04:42:47 GMT
> To: users@pdfbox.apache.org
> Subject: Re: Gradient paint
>
> For the 1.8 version the code is slightly different:
>
>               PDDocument document = new PDDocument();
>               PDPage page = new PDPage();
>               document.addPage(page);
>
>               // shading attributes
>               COSDictionary dict = new COSDictionary();
>               dict.setInt(COSName.SHADING_TYPE, 2);
>               dict.setName(COSName.COLORSPACE, "DeviceRGB");
>               COSArray coords = new COSArray();
>               coords.add(COSInteger.get(100));
>               coords.add(COSInteger.get(400));
>               coords.add(COSInteger.get(400));
>               coords.add(COSInteger.get(600));
>               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"));
>               c0.add(COSFloat.get("0"));
>               c0.add(COSFloat.get("0"));
>               COSArray c1 = new COSArray();
>               c1.add(COSFloat.get("0.5"));
>               c1.add(COSFloat.get("1"));
>               c1.add(COSFloat.get("0.5"));
>               fdict.setItem(COSName.DOMAIN, domain);
>               fdict.setItem(COSName.C0, c0);
>               fdict.setItem(COSName.C1, c1);
>               fdict.setInt(COSName.N, 1);
>               dict.setItem(COSName.FUNCTION, fdict);
>
>               PDShadingType2 shading = new PDShadingType2(dict);
>
>               // create and add to shading resources
>               page.setResources(new PDResources());
>               Map<String, PDShadingResources> shadings = new 
> HashMap<String, PDShadingResources>();
>               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("shtest.pdf");
>               document.close();
>
>
>


Re: Gradient paint

Posted by Tilman Hausherr <TH...@t-online.de>.
here's the stream of the correct PDF:

1 0 1 rg
100 100 100 100 re
f
/sh1 sh



incorrect PDF #1:
/sh1 sh1 0 1 rg
100 100 100 100 re
f


incorrect PDF #2:

1 0 1 rg
100 100 100 100 re
f
/sh1 sh0 0 1 rg
100 200 100 100 re
f


===> add a newline or a space after "/sh1 sh"

I'll clarify this in the sample code.

Tilman



Am 05.08.2014 04:56, schrieb Eric Inman:
> The code below works for me and the gradient appears.
>
> However, if I try to fill a rectangle by following
>
> 	contentStream.appendRawCommands("/sh1 sh");
>
> with
>
>          contentStream.setNonStrokingColor(Color.MAGENTA);
>          contentStream.fillRect(100,100,100,100);
>
> then the gradient no longer appears nor does the rectangle. If I put the rectangle code before the appendRawCommands call, then both the rectangle and the gradient appear.
>
> In this simple example, switching the order is an acceptable solution, but in the real application that I'm working on, I need to do these operations in either order. Is that possible?
>
> Thanks
>
> -----Original Message-----
> From: Tilman Hausherr <TH...@t-online.de>
> Sent: Wed, 16 Jul 2014 04:42:47 GMT
> To: users@pdfbox.apache.org
> Subject: Re: Gradient paint
>
> For the 1.8 version the code is slightly different:
>
>               PDDocument document = new PDDocument();
>               PDPage page = new PDPage();
>               document.addPage(page);
>
>               // shading attributes
>               COSDictionary dict = new COSDictionary();
>               dict.setInt(COSName.SHADING_TYPE, 2);
>               dict.setName(COSName.COLORSPACE, "DeviceRGB");
>               COSArray coords = new COSArray();
>               coords.add(COSInteger.get(100));
>               coords.add(COSInteger.get(400));
>               coords.add(COSInteger.get(400));
>               coords.add(COSInteger.get(600));
>               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"));
>               c0.add(COSFloat.get("0"));
>               c0.add(COSFloat.get("0"));
>               COSArray c1 = new COSArray();
>               c1.add(COSFloat.get("0.5"));
>               c1.add(COSFloat.get("1"));
>               c1.add(COSFloat.get("0.5"));
>               fdict.setItem(COSName.DOMAIN, domain);
>               fdict.setItem(COSName.C0, c0);
>               fdict.setItem(COSName.C1, c1);
>               fdict.setInt(COSName.N, 1);
>               dict.setItem(COSName.FUNCTION, fdict);
>
>               PDShadingType2 shading = new PDShadingType2(dict);
>
>               // create and add to shading resources
>               page.setResources(new PDResources());
>               Map<String, PDShadingResources> shadings = new
> HashMap<String, PDShadingResources>();
>               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("shtest.pdf");
>               document.close();
>
>
>