1 31 package org.pdfbox.examples.pdmodel; 32 33 import java.awt.Color ; 34 import java.io.IOException ; 35 36 import org.pdfbox.exceptions.COSVisitorException; 37 38 import org.pdfbox.pdmodel.PDDocument; 39 import org.pdfbox.pdmodel.PDPage; 40 41 import org.pdfbox.pdmodel.edit.PDPageContentStream; 42 43 51 public class ShowColorBoxes 52 { 53 56 public ShowColorBoxes() 57 { 58 super(); 59 } 60 61 69 public void doIt( String file) throws IOException , COSVisitorException 70 { 71 PDDocument doc = null; 73 try 74 { 75 doc = new PDDocument(); 76 77 PDPage page = new PDPage(); 78 doc.addPage( page ); 79 80 PDPageContentStream contentStream = new PDPageContentStream(doc, page); 81 contentStream.setNonStrokingColor( Color.CYAN ); 83 contentStream.fillRect( 0,0, page.findMediaBox().getWidth(), page.findMediaBox().getHeight() ); 84 85 contentStream.setNonStrokingColor( Color.RED ); 87 contentStream.fillRect( 10, 10, 100, 100 ); 88 89 contentStream.close(); 90 doc.save( file ); 91 } 92 finally 93 { 94 if( doc != null ) 95 { 96 doc.close(); 97 } 98 } 99 } 100 101 108 public static void main(String [] args) 109 { 110 ShowColorBoxes app = new ShowColorBoxes(); 111 try 112 { 113 if( args.length != 1 ) 114 { 115 app.usage(); 116 } 117 else 118 { 119 app.doIt( args[0] ); 120 } 121 } 122 catch (Exception e) 123 { 124 e.printStackTrace(); 125 } 126 } 127 128 131 private void usage() 132 { 133 System.err.println( "usage: " + this.getClass().getName() + " <output-file>" ); 134 } 135 } | Popular Tags |