pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eric Inman" <eric-in...@comcast.net>
Subject RE: Gradient paint
Date Tue, 05 Aug 2014 02:56:28 GMT
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 <THaush...@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();




Mime
View raw message