1 package it.stefanochizzolini.clown.samples; 2 3 import it.stefanochizzolini.clown.bytes.Buffer; 4 import it.stefanochizzolini.clown.bytes.FileInputStream; 5 import it.stefanochizzolini.clown.documents.Document; 6 import it.stefanochizzolini.clown.documents.Page; 7 import it.stefanochizzolini.clown.documents.Pages; 8 import it.stefanochizzolini.clown.documents.contents.ContentStream; 9 import it.stefanochizzolini.clown.documents.contents.ContentStreams; 10 import it.stefanochizzolini.clown.documents.contents.Fonts; 11 import it.stefanochizzolini.clown.documents.contents.Resources; 12 import it.stefanochizzolini.clown.documents.contents.XObjects; 13 import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor; 14 import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum; 15 import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum; 16 import it.stefanochizzolini.clown.documents.contents.composition.ContentBuilder; 17 import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font; 18 import it.stefanochizzolini.clown.documents.contents.xObjects.FormXObject; 19 import it.stefanochizzolini.clown.files.File; 20 import it.stefanochizzolini.clown.objects.PdfName; 21 import it.stefanochizzolini.clown.tokens.FileFormatException; 22 23 import java.awt.geom.Point2D ; 24 import java.io.RandomAccessFile ; 25 26 34 public class WatermarkSample 35 implements ISample 36 { 37 public void run( 38 PDFClownSampleLoader loader 39 ) 40 { 41 47 48 String filePath = loader.getPdfFileChoice("Please select a PDF file"); 50 51 File file; 53 try 54 { 55 file = new File( 57 new FileInputStream( 58 new RandomAccessFile (filePath,"r") 59 ) 60 ); 61 } 62 catch(FileFormatException e) 63 {throw new RuntimeException (filePath + " file has a bad file format.",e);} 64 catch(Exception e) 65 {throw new RuntimeException (filePath + " file access error.",e);} 66 67 Document document = file.getDocument(); 69 Pages pages = document.getPages(); 71 72 FormXObject watermark = new FormXObject(document); 74 { 75 double height = document.getPageHeight(); 78 double width = document.getPageWidth(); 79 if(height == 0) 81 { 82 Page page = pages.get(0); 84 height = page.getHeight(); 85 width = page.getWidth(); 86 } 87 watermark.setHeight(height); 88 watermark.setWidth(width); 89 90 Resources resources = new Resources(document); 93 watermark.setResources(resources); 94 Fonts fonts = new Fonts(document); 96 resources.setFonts(fonts); 97 PdfName fontName = new PdfName("it.stefanochizzolini.clown.samples:Times"); 99 fonts.put( 100 fontName, 101 new StandardType1Font( 102 document, 103 StandardType1Font.FamilyNameEnum.Times, 104 true, 105 false 106 ) 107 ); 108 ContentStream content = watermark.getContent(); 110 ContentBuilder contentBuilder = new ContentBuilder(content,new Buffer()); 111 contentBuilder.beginLocalState(); 112 contentBuilder.setFont(fontName,120); 113 contentBuilder.beginText(); 115 contentBuilder.setFillColor( 116 new DeviceRGBColor(115f/255, 164f/255, 232f/255) 117 ); 118 contentBuilder.showText( 119 "PDFClown", 120 new Point2D.Double (width*.2d,height*.65d), 121 AlignmentXEnum.Left, 122 AlignmentYEnum.Top, 123 45 124 ); 125 contentBuilder.endText(); 127 contentBuilder.endLocalState(); 128 content.append(contentBuilder.getBuffer()); 130 } 131 132 PdfName watermarkName = new PdfName("it.stefanochizzolini.clown.samples:watermark"); 133 ContentStream backgroundContent = new ContentStream(document); 135 136 XObjects commonXObjects = null; 137 for(Page page : pages) 138 { 139 Resources resources = page.getResources(); 141 XObjects xObjects = resources.getXObjects(); 142 if(xObjects == null) 143 { 144 if(commonXObjects == null) 145 {commonXObjects = new XObjects(document);} 146 147 xObjects = commonXObjects; 148 resources.setXObjects(xObjects); 149 resources.update(); } 151 if(!xObjects.containsKey(watermarkName)) 152 { 153 xObjects.put(watermarkName,watermark); 154 xObjects.update(); } 156 157 ContentStreams contents = page.getContents(); 160 contents.add(0,backgroundContent); 162 contents.update(); } 164 ContentBuilder contentBuilder = new ContentBuilder(backgroundContent,new Buffer()); 166 contentBuilder.showXObject(watermarkName); 167 backgroundContent.append(contentBuilder.getBuffer()); 168 169 loader.serialize(file,this.getClass().getSimpleName()); 171 } 172 } | Popular Tags |