pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: Gradient paint
Date Wed, 16 Jul 2014 04:42:47 GMT
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