KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > edit > PDPageContentStream


1 /**
2  * Copyright (c) 2004-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.edit;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.color.ColorSpace JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.OutputStream JavaDoc;
38
39 import java.text.NumberFormat JavaDoc;
40
41 import java.util.ArrayList JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Locale JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.HashMap JavaDoc;
46
47 import org.pdfbox.pdmodel.PDDocument;
48 import org.pdfbox.pdmodel.PDPage;
49 import org.pdfbox.pdmodel.PDResources;
50
51 import org.pdfbox.pdmodel.common.COSStreamArray;
52 import org.pdfbox.pdmodel.common.PDStream;
53
54 import org.pdfbox.pdmodel.font.PDFont;
55 import org.pdfbox.pdmodel.graphics.color.PDColorSpace;
56 import org.pdfbox.pdmodel.graphics.color.PDDeviceCMYK;
57 import org.pdfbox.pdmodel.graphics.color.PDDeviceGray;
58 import org.pdfbox.pdmodel.graphics.color.PDDeviceN;
59 import org.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
60 import org.pdfbox.pdmodel.graphics.color.PDICCBased;
61 import org.pdfbox.pdmodel.graphics.color.PDPattern;
62 import org.pdfbox.pdmodel.graphics.color.PDSeparation;
63 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
64 import org.pdfbox.util.MapUtil;
65
66 import org.pdfbox.cos.COSArray;
67 import org.pdfbox.cos.COSDictionary;
68 import org.pdfbox.cos.COSName;
69 import org.pdfbox.cos.COSString;
70
71
72 /**
73  * This class will is a convenience for creating page content streams. You MUST
74  * call close() when you are finished with this object.
75  *
76  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
77  * @version $Revision: 1.18 $
78  */

79 public class PDPageContentStream
80 {
81     private PDPage page;
82     private OutputStream JavaDoc output;
83     private boolean inTextMode = false;
84     private Map JavaDoc fontMappings = new HashMap JavaDoc();
85     private Map JavaDoc imageMappings = new HashMap JavaDoc();
86     private PDResources resources;
87     private Map JavaDoc fonts;
88     private Map JavaDoc images;
89     
90     private PDColorSpace currentStrokingColorSpace = new PDDeviceGray();
91     private PDColorSpace currentNonStrokingColorSpace = new PDDeviceGray();
92     
93     //cached storage component for getting color values
94
private float[] colorComponents = new float[4];
95     
96     private NumberFormat JavaDoc formatDecimal = NumberFormat.getNumberInstance( Locale.US );
97     
98     private static final String JavaDoc BEGIN_TEXT = "BT\n";
99     private static final String JavaDoc END_TEXT = "ET\n";
100     private static final String JavaDoc SET_FONT = "Tf\n";
101     private static final String JavaDoc MOVE_TEXT_POSITION = "Td\n";
102     private static final String JavaDoc SHOW_TEXT = "Tj\n";
103     
104     private static final String JavaDoc SAVE_GRAPHICS_STATE = "q\n";
105     private static final String JavaDoc RESTORE_GRAPHICS_STATE = "Q\n";
106     private static final String JavaDoc CONCATENATE_MATRIX = "cm\n";
107     private static final String JavaDoc XOBJECT_DO = "Do\n";
108     private static final String JavaDoc RG_STROKING = "RG\n";
109     private static final String JavaDoc RG_NON_STROKING = "rg\n";
110     private static final String JavaDoc K_STROKING = "K\n";
111     private static final String JavaDoc K_NON_STROKING = "k\n";
112     private static final String JavaDoc G_STROKING = "G\n";
113     private static final String JavaDoc G_NON_STROKING = "g\n";
114     private static final String JavaDoc APPEND_RECTANGLE = "re\n";
115     private static final String JavaDoc FILL = "f\n";
116     
117     private static final String JavaDoc SET_STROKING_COLORSPACE = "CS\n";
118     private static final String JavaDoc SET_NON_STROKING_COLORSPACE = "cs\n";
119     
120     private static final String JavaDoc SET_STROKING_COLOR_SIMPLE="SC\n";
121     private static final String JavaDoc SET_STROKING_COLOR_COMPLEX="SCN\n";
122     private static final String JavaDoc SET_NON_STROKING_COLOR_SIMPLE="sc\n";
123     private static final String JavaDoc SET_NON_STROKING_COLOR_COMPLEX="scn\n";
124     
125     
126     
127     private static final int SPACE = 32;
128     
129     
130     /**
131      * Create a new PDPage content stream.
132      *
133      * @param document The document the page is part of.
134      * @param sourcePage The page to write the contents to.
135      * @throws IOException If there is an error writing to the page contents.
136      */

137     public PDPageContentStream( PDDocument document, PDPage sourcePage ) throws IOException JavaDoc
138     {
139         this(document,sourcePage,false,true);
140     }
141     
142     /**
143      * Create a new PDPage content stream.
144      *
145      * @param document The document the page is part of.
146      * @param sourcePage The page to write the contents to.
147      * @param appendContent Indicates whether content will be overwritten. If false all previous content is deleted.
148      * @param compress Tell if the content stream should compress the page contents.
149      * @throws IOException If there is an error writing to the page contents.
150      */

151     public PDPageContentStream( PDDocument document, PDPage sourcePage, boolean appendContent, boolean compress )
152         throws IOException JavaDoc
153     {
154         page = sourcePage;
155         resources = page.getResources();
156         if( resources == null )
157         {
158             resources = new PDResources();
159             page.setResources( resources );
160         }
161         fonts = resources.getFonts();
162         images = resources.getImages();
163         // If request specifies the need to append to the document
164
if(appendContent)
165         {
166             // Get the pdstream from the source page instead of creating a new one
167
PDStream contents = sourcePage.getContents();
168             
169             // Create a pdstream to append new content
170
PDStream contentsToAppend = new PDStream( document );
171             
172             // This will be the resulting COSStreamArray after existing and new streams are merged
173
COSStreamArray compoundStream = null;
174             
175             // If contents is already an array, a new stream is simply appended to it
176
if(contents.getStream() instanceof COSStreamArray)
177             {
178                 compoundStream = (COSStreamArray)contents.getStream();
179                 compoundStream.appendStream( contentsToAppend.getStream());
180             }
181             else
182             {
183                 // Creates the COSStreamArray and adds the current stream plus a new one to it
184
COSArray newArray = new COSArray();
185                 newArray.add(contents.getCOSObject());
186                 newArray.add(contentsToAppend.getCOSObject());
187                 compoundStream = new COSStreamArray(newArray);
188             }
189             
190             if( compress )
191             {
192                 List JavaDoc filters = new ArrayList JavaDoc();
193                 filters.add( COSName.FLATE_DECODE );
194                 contentsToAppend.setFilters( filters );
195             }
196             
197             // Sets the compoundStream as page contents
198
sourcePage.setContents( new PDStream(compoundStream) );
199             output = contentsToAppend.createOutputStream();
200         }
201         else
202         {
203             PDStream contents = new PDStream( document );
204             if( compress )
205             {
206                 List JavaDoc filters = new ArrayList JavaDoc();
207                 filters.add( COSName.FLATE_DECODE );
208                 contents.setFilters( filters );
209             }
210             sourcePage.setContents( contents );
211             output = contents.createOutputStream();
212         }
213         formatDecimal.setMaximumFractionDigits( 10 );
214         formatDecimal.setGroupingUsed( false );
215     }
216     
217     /**
218      * Begin some text operations.
219      *
220      * @throws IOException If there is an error writing to the stream or if you attempt to
221      * nest beginText calls.
222      */

223     public void beginText() throws IOException JavaDoc
224     {
225         if( inTextMode )
226         {
227             throw new IOException JavaDoc( "Error: Nested beginText() calls are not allowed." );
228         }
229         appendRawCommands( BEGIN_TEXT );
230         inTextMode = true;
231     }
232     
233     /**
234      * End some text operations.
235      *
236      * @throws IOException If there is an error writing to the stream or if you attempt to
237      * nest endText calls.
238      */

239     public void endText() throws IOException JavaDoc
240     {
241         if( !inTextMode )
242         {
243             throw new IOException JavaDoc( "Error: You must call beginText() before calling endText." );
244         }
245         appendRawCommands( END_TEXT );
246         inTextMode = false;
247     }
248     
249     /**
250      * Set the font to draw text with.
251      *
252      * @param font The font to use.
253      * @param fontSize The font size to draw the text.
254      * @throws IOException If there is an error writing the font information.
255      */

256     public void setFont( PDFont font, float fontSize ) throws IOException JavaDoc
257     {
258         String JavaDoc fontMapping = (String JavaDoc)fontMappings.get( font );
259         if( fontMapping == null )
260         {
261             fontMapping = MapUtil.getNextUniqueKey( fonts, "F" );
262             fontMappings.put( font, fontMapping );
263             fonts.put( fontMapping, font );
264         }
265         appendRawCommands( "/");
266         appendRawCommands( fontMapping );
267         appendRawCommands( SPACE );
268         appendRawCommands( formatDecimal.format( fontSize ) );
269         appendRawCommands( SPACE );
270         appendRawCommands( SET_FONT );
271     }
272     
273     /**
274      * Draw an image at the x,y coordinates, with the default size of the image.
275      *
276      * @param image The image to draw.
277      * @param x The x-coordinate to draw the image.
278      * @param y The y-coordinate to draw the image.
279      *
280      * @throws IOException If there is an error writing to the stream.
281      */

282     public void drawImage( PDXObjectImage image, float x, float y ) throws IOException JavaDoc
283     {
284         drawImage( image, x, y, image.getWidth(), image.getHeight() );
285     }
286     
287     /**
288      * Draw an image at the x,y coordinates and a certain width and height.
289      *
290      * @param image The image to draw.
291      * @param x The x-coordinate to draw the image.
292      * @param y The y-coordinate to draw the image.
293      * @param width The width of the image to draw.
294      * @param height The height of the image to draw.
295      *
296      * @throws IOException If there is an error writing to the stream.
297      */

298     public void drawImage( PDXObjectImage image, float x, float y, float width, float height ) throws IOException JavaDoc
299     {
300         String JavaDoc imageMapping = (String JavaDoc)imageMappings.get( image );
301         if( imageMapping == null )
302         {
303             imageMapping = MapUtil.getNextUniqueKey( images, "Im" );
304             imageMappings.put( image, imageMapping );
305             images.put( imageMapping, image );
306         }
307         appendRawCommands( SAVE_GRAPHICS_STATE );
308         appendRawCommands( formatDecimal.format( width ) );
309         appendRawCommands( SPACE );
310         appendRawCommands( formatDecimal.format( 0 ) );
311         appendRawCommands( SPACE );
312         appendRawCommands( formatDecimal.format( 0 ) );
313         appendRawCommands( SPACE );
314         appendRawCommands( formatDecimal.format( height ) );
315         appendRawCommands( SPACE );
316         appendRawCommands( formatDecimal.format( x ) );
317         appendRawCommands( SPACE );
318         appendRawCommands( formatDecimal.format( y ) );
319         appendRawCommands( SPACE );
320         appendRawCommands( CONCATENATE_MATRIX );
321         appendRawCommands( SPACE );
322         appendRawCommands( "/" );
323         appendRawCommands( imageMapping );
324         appendRawCommands( SPACE );
325         appendRawCommands( XOBJECT_DO );
326         appendRawCommands( SPACE );
327         appendRawCommands( RESTORE_GRAPHICS_STATE );
328     }
329     
330     /**
331      * The Td operator.
332      * @param x The x coordinate.
333      * @param y The y coordinate.
334      * @throws IOException If there is an error writing to the stream.
335      */

336     public void moveTextPositionByAmount( float x, float y ) throws IOException JavaDoc
337     {
338         if( !inTextMode )
339         {
340             throw new IOException JavaDoc( "Error: must call beginText() before moveTextPositionByAmount");
341         }
342         appendRawCommands( formatDecimal.format( x ) );
343         appendRawCommands( SPACE );
344         appendRawCommands( formatDecimal.format( y ) );
345         appendRawCommands( SPACE );
346         appendRawCommands( MOVE_TEXT_POSITION );
347     }
348     
349     /**
350      * This will draw a string at the current location on the screen.
351      *
352      * @param text The text to draw.
353      * @throws IOException If an io exception occurs.
354      */

355     public void drawString( String JavaDoc text ) throws IOException JavaDoc
356     {
357         if( !inTextMode )
358         {
359             throw new IOException JavaDoc( "Error: must call beginText() before drawString");
360         }
361         COSString string = new COSString( text );
362         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
363         string.writePDF( buffer );
364         appendRawCommands( new String JavaDoc( buffer.toByteArray(), "ISO-8859-1"));
365         appendRawCommands( SPACE );
366         appendRawCommands( SHOW_TEXT );
367     }
368     
369     /**
370      * Set the stroking color space. This will add the colorspace to the PDResources
371      * if necessary.
372      *
373      * @param colorSpace The colorspace to write.
374      * @throws IOException If there is an error writing the colorspace.
375      */

376     public void setStrokingColorSpace( PDColorSpace colorSpace ) throws IOException JavaDoc
377     {
378         writeColorSpace( colorSpace );
379         appendRawCommands( SET_STROKING_COLORSPACE );
380     }
381     
382     /**
383      * Set the stroking color space. This will add the colorspace to the PDResources
384      * if necessary.
385      *
386      * @param colorSpace The colorspace to write.
387      * @throws IOException If there is an error writing the colorspace.
388      */

389     public void setNonStrokingColorSpace( PDColorSpace colorSpace ) throws IOException JavaDoc
390     {
391         writeColorSpace( colorSpace );
392         appendRawCommands( SET_NON_STROKING_COLORSPACE );
393     }
394     
395     private void writeColorSpace( PDColorSpace colorSpace ) throws IOException JavaDoc
396     {
397         COSName key = null;
398         if( colorSpace instanceof PDDeviceGray ||
399             colorSpace instanceof PDDeviceRGB ||
400             colorSpace instanceof PDDeviceCMYK )
401         {
402             key = COSName.getPDFName( colorSpace.getName() );
403         }
404         else
405         {
406             COSDictionary colorSpaces =
407                 (COSDictionary)resources.getCOSDictionary().getDictionaryObject(COSName.COLORSPACE);
408             if( colorSpaces == null )
409             {
410                 colorSpaces = new COSDictionary();
411                 resources.getCOSDictionary().setItem( COSName.COLORSPACE, colorSpaces );
412             }
413             key = colorSpaces.getKeyForValue( colorSpace.getCOSObject() );
414             
415             if( key == null )
416             {
417                 int counter = 0;
418                 String JavaDoc csName = "CS";
419                 while( colorSpaces.containsValue( csName + counter ) )
420                 {
421                     counter++;
422                 }
423                 key = COSName.getPDFName( csName + counter );
424                 colorSpaces.setItem( key, colorSpace );
425             }
426         }
427         key.writePDF( output );
428         appendRawCommands( SPACE );
429     }
430     
431     /**
432      * Set the color components of current stroking colorspace.
433      *
434      * @param components The components to set for the current color.
435      * @throws IOException If there is an error while writing to the stream.
436      */

437     public void setStrokingColor( float[] components ) throws IOException JavaDoc
438     {
439         for( int i=0; i< components.length; i++ )
440         {
441             appendRawCommands( formatDecimal.format( components[i] ) );
442             appendRawCommands( SPACE );
443         }
444         if( currentStrokingColorSpace instanceof PDSeparation ||
445             currentStrokingColorSpace instanceof PDPattern ||
446             currentStrokingColorSpace instanceof PDDeviceN ||
447             currentStrokingColorSpace instanceof PDICCBased )
448         {
449             appendRawCommands( SET_STROKING_COLOR_COMPLEX );
450         }
451         else
452         {
453             appendRawCommands( SET_STROKING_COLOR_SIMPLE );
454         }
455     }
456     
457     /**
458      * Set the stroking color, specified as RGB.
459      *
460      * @param color The color to set.
461      * @throws IOException If an IO error occurs while writing to the stream.
462      */

463     public void setStrokingColor( Color JavaDoc color ) throws IOException JavaDoc
464     {
465         ColorSpace JavaDoc colorSpace = color.getColorSpace();
466         if( colorSpace.getType() == ColorSpace.TYPE_RGB )
467         {
468             setStrokingColor( color.getRed(), color.getGreen(), color.getBlue() );
469         }
470         else if( colorSpace.getType() == ColorSpace.TYPE_GRAY )
471         {
472             color.getColorComponents( colorComponents );
473             setStrokingColor( colorComponents[0] );
474         }
475         else if( colorSpace.getType() == ColorSpace.TYPE_CMYK )
476         {
477             color.getColorComponents( colorComponents );
478             setStrokingColor( colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3] );
479         }
480         else
481         {
482             throw new IOException JavaDoc( "Error: unknown colorspace:" + colorSpace );
483         }
484     }
485     
486     /**
487      * Set the non stroking color, specified as RGB.
488      *
489      * @param color The color to set.
490      * @throws IOException If an IO error occurs while writing to the stream.
491      */

492     public void setNonStrokingColor( Color JavaDoc color ) throws IOException JavaDoc
493     {
494         ColorSpace JavaDoc colorSpace = color.getColorSpace();
495         if( colorSpace.getType() == ColorSpace.TYPE_RGB )
496         {
497             setNonStrokingColor( color.getRed(), color.getGreen(), color.getBlue() );
498         }
499         else if( colorSpace.getType() == ColorSpace.TYPE_GRAY )
500         {
501             color.getColorComponents( colorComponents );
502             setNonStrokingColor( colorComponents[0] );
503         }
504         else if( colorSpace.getType() == ColorSpace.TYPE_CMYK )
505         {
506             color.getColorComponents( colorComponents );
507             setNonStrokingColor( colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3] );
508         }
509         else
510         {
511             throw new IOException JavaDoc( "Error: unknown colorspace:" + colorSpace );
512         }
513     }
514     
515     /**
516      * Set the stroking color, specified as RGB, 0-255.
517      *
518      * @param r The red value.
519      * @param g The green value.
520      * @param b The blue value.
521      * @throws IOException If an IO error occurs while writing to the stream.
522      */

523     public void setStrokingColor( int r, int g, int b ) throws IOException JavaDoc
524     {
525         appendRawCommands( formatDecimal.format( r/255d ) );
526         appendRawCommands( SPACE );
527         appendRawCommands( formatDecimal.format( g/255d ) );
528         appendRawCommands( SPACE );
529         appendRawCommands( formatDecimal.format( b/255d ) );
530         appendRawCommands( SPACE );
531         appendRawCommands( RG_STROKING );
532     }
533     
534     /**
535      * Set the stroking color, specified as CMYK, 0-255.
536      *
537      * @param c The cyan value.
538      * @param m The magenta value.
539      * @param y The yellow value.
540      * @param k The black value.
541      * @throws IOException If an IO error occurs while writing to the stream.
542      */

543     public void setStrokingColor( int c, int m, int y, int k) throws IOException JavaDoc
544     {
545         appendRawCommands( formatDecimal.format( c/255d ) );
546         appendRawCommands( SPACE );
547         appendRawCommands( formatDecimal.format( m/255d ) );
548         appendRawCommands( SPACE );
549         appendRawCommands( formatDecimal.format( y/255d ) );
550         appendRawCommands( SPACE );
551         appendRawCommands( formatDecimal.format( k/255d ) );
552         appendRawCommands( SPACE );
553         appendRawCommands( K_STROKING );
554     }
555     
556     /**
557      * Set the stroking color, specified as CMYK, 0.0-1.0.
558      *
559      * @param c The cyan value.
560      * @param m The magenta value.
561      * @param y The yellow value.
562      * @param k The black value.
563      * @throws IOException If an IO error occurs while writing to the stream.
564      */

565     public void setStrokingColor( double c, double m, double y, double k) throws IOException JavaDoc
566     {
567         appendRawCommands( formatDecimal.format( c ) );
568         appendRawCommands( SPACE );
569         appendRawCommands( formatDecimal.format( m ) );
570         appendRawCommands( SPACE );
571         appendRawCommands( formatDecimal.format( y ) );
572         appendRawCommands( SPACE );
573         appendRawCommands( formatDecimal.format( k ) );
574         appendRawCommands( SPACE );
575         appendRawCommands( K_STROKING );
576     }
577     
578     /**
579      * Set the stroking color, specified as grayscale, 0-255.
580      *
581      * @param g The gray value.
582      * @throws IOException If an IO error occurs while writing to the stream.
583      */

584     public void setStrokingColor( int g ) throws IOException JavaDoc
585     {
586         appendRawCommands( formatDecimal.format( g/255d ) );
587         appendRawCommands( SPACE );
588         appendRawCommands( G_STROKING );
589     }
590     
591     /**
592      * Set the stroking color, specified as Grayscale 0.0-1.0.
593      *
594      * @param g The gray value.
595      * @throws IOException If an IO error occurs while writing to the stream.
596      */

597     public void setStrokingColor( double g ) throws IOException JavaDoc
598     {
599         appendRawCommands( formatDecimal.format( g ) );
600         appendRawCommands( SPACE );
601         appendRawCommands( G_STROKING );
602     }
603     
604     /**
605      * Set the color components of current non stroking colorspace.
606      *
607      * @param components The components to set for the current color.
608      * @throws IOException If there is an error while writing to the stream.
609      */

610     public void setNonStrokingColor( float[] components ) throws IOException JavaDoc
611     {
612         for( int i=0; i< components.length; i++ )
613         {
614             appendRawCommands( formatDecimal.format( components[i] ) );
615             appendRawCommands( SPACE );
616         }
617         if( currentNonStrokingColorSpace instanceof PDSeparation ||
618             currentNonStrokingColorSpace instanceof PDPattern ||
619             currentNonStrokingColorSpace instanceof PDDeviceN ||
620             currentNonStrokingColorSpace instanceof PDICCBased )
621         {
622             appendRawCommands( SET_NON_STROKING_COLOR_COMPLEX );
623         }
624         else
625         {
626             appendRawCommands( SET_NON_STROKING_COLOR_SIMPLE );
627         }
628     }
629     
630     /**
631      * Set the non stroking color, specified as RGB, 0-255.
632      *
633      * @param r The red value.
634      * @param g The green value.
635      * @param b The blue value.
636      * @throws IOException If an IO error occurs while writing to the stream.
637      */

638     public void setNonStrokingColor( int r, int g, int b ) throws IOException JavaDoc
639     {
640         appendRawCommands( formatDecimal.format( r/255d ) );
641         appendRawCommands( SPACE );
642         appendRawCommands( formatDecimal.format( g/255d ) );
643         appendRawCommands( SPACE );
644         appendRawCommands( formatDecimal.format( b/255d ) );
645         appendRawCommands( SPACE );
646         appendRawCommands( RG_NON_STROKING );
647     }
648     
649     /**
650      * Set the non stroking color, specified as CMYK, 0-255.
651      *
652      * @param c The cyan value.
653      * @param m The magenta value.
654      * @param y The yellow value.
655      * @param k The black value.
656      * @throws IOException If an IO error occurs while writing to the stream.
657      */

658     public void setNonStrokingColor( int c, int m, int y, int k) throws IOException JavaDoc
659     {
660         appendRawCommands( formatDecimal.format( c/255d ) );
661         appendRawCommands( SPACE );
662         appendRawCommands( formatDecimal.format( m/255d ) );
663         appendRawCommands( SPACE );
664         appendRawCommands( formatDecimal.format( y/255d ) );
665         appendRawCommands( SPACE );
666         appendRawCommands( formatDecimal.format( k/255d ) );
667         appendRawCommands( SPACE );
668         appendRawCommands( K_NON_STROKING );
669     }
670     
671     /**
672      * Set the non stroking color, specified as CMYK, 0.0-1.0.
673      *
674      * @param c The cyan value.
675      * @param m The magenta value.
676      * @param y The yellow value.
677      * @param k The black value.
678      * @throws IOException If an IO error occurs while writing to the stream.
679      */

680     public void setNonStrokingColor( double c, double m, double y, double k) throws IOException JavaDoc
681     {
682         appendRawCommands( formatDecimal.format( c ) );
683         appendRawCommands( SPACE );
684         appendRawCommands( formatDecimal.format( m ) );
685         appendRawCommands( SPACE );
686         appendRawCommands( formatDecimal.format( y ) );
687         appendRawCommands( SPACE );
688         appendRawCommands( formatDecimal.format( k ) );
689         appendRawCommands( SPACE );
690         appendRawCommands( K_NON_STROKING );
691     }
692     
693     /**
694      * Set the non stroking color, specified as grayscale, 0-255.
695      *
696      * @param g The gray value.
697      * @throws IOException If an IO error occurs while writing to the stream.
698      */

699     public void setNonStrokingColor( int g ) throws IOException JavaDoc
700     {
701         appendRawCommands( formatDecimal.format( g/255d ) );
702         appendRawCommands( SPACE );
703         appendRawCommands( G_NON_STROKING );
704     }
705     
706     /**
707      * Set the non stroking color, specified as Grayscale 0.0-1.0.
708      *
709      * @param g The gray value.
710      * @throws IOException If an IO error occurs while writing to the stream.
711      */

712     public void setNonStrokingColor( double g ) throws IOException JavaDoc
713     {
714         appendRawCommands( formatDecimal.format( g ) );
715         appendRawCommands( SPACE );
716         appendRawCommands( G_NON_STROKING );
717     }
718     
719     /**
720      * Draw a rectangle on the page using the current non stroking color.
721      *
722      * @param x The lower left x coordinate.
723      * @param y The lower left y coordinate.
724      * @param width The width of the rectangle.
725      * @param height The height of the rectangle.
726      * @throws IOException If there is an error while drawing on the screen.
727      */

728     public void fillRect( float x, float y, float width, float height ) throws IOException JavaDoc
729     {
730         appendRawCommands( formatDecimal.format( x ) );
731         appendRawCommands( SPACE );
732         appendRawCommands( formatDecimal.format( y ) );
733         appendRawCommands( SPACE );
734         appendRawCommands( formatDecimal.format( width ) );
735         appendRawCommands( SPACE );
736         appendRawCommands( formatDecimal.format( height ) );
737         appendRawCommands( SPACE );
738         appendRawCommands( APPEND_RECTANGLE );
739         appendRawCommands( FILL );
740     }
741     
742     
743     /**
744      * This will append raw commands to the content stream.
745      *
746      * @param commands The commands to append to the stream.
747      * @throws IOException If an error occurs while writing to the stream.
748      */

749     public void appendRawCommands( String JavaDoc commands ) throws IOException JavaDoc
750     {
751         appendRawCommands( commands.getBytes( "ISO-8859-1" ) );
752     }
753     
754     /**
755      * This will append raw commands to the content stream.
756      *
757      * @param commands The commands to append to the stream.
758      * @throws IOException If an error occurs while writing to the stream.
759      */

760     public void appendRawCommands( byte[] commands ) throws IOException JavaDoc
761     {
762         output.write( commands );
763     }
764     
765     /**
766      * This will append raw commands to the content stream.
767      *
768      * @param data Append a raw byte to the stream.
769      *
770      * @throws IOException If an error occurs while writing to the stream.
771      */

772     public void appendRawCommands( int data ) throws IOException JavaDoc
773     {
774         output.write( data );
775     }
776     
777     /**
778      * Close the content stream. This must be called when you are done with this
779      * object.
780      * @throws IOException If the underlying stream has a problem being written to.
781      */

782     public void close() throws IOException JavaDoc
783     {
784         output.close();
785     }
786 }
Popular Tags