1 31 package org.pdfbox.examples.pdmodel; 32 33 import org.pdfbox.pdmodel.PDDocument; 34 import org.pdfbox.pdmodel.PDPage; 35 import org.pdfbox.pdmodel.common.PDRectangle; 36 import org.pdfbox.pdmodel.edit.PDPageContentStream; 37 import org.pdfbox.pdmodel.font.PDFont; 38 import org.pdfbox.pdmodel.font.PDType1Font; 39 import org.pdfbox.pdmodel.graphics.color.PDGamma; 40 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI; 41 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine; 42 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle; 43 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup; 44 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink; 45 import org.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary; 46 47 48 import java.util.List ; 49 50 56 public class Annotation 57 { 58 private Annotation() 59 { 60 } 62 63 70 public static void main( String [] args ) throws Exception 71 { 72 if( args.length != 1 ) 73 { 74 usage(); 75 } 76 else 77 { 78 PDDocument document = new PDDocument(); 79 80 try 81 { 82 PDPage page = new PDPage(); 83 document.addPage(page); 84 List annotations = page.getAnnotations(); 85 86 89 float inch = 72; 90 PDGamma colourRed = new PDGamma(); 91 colourRed.setR(1); 92 PDGamma colourBlue = new PDGamma(); 93 colourBlue.setB(1); 94 PDGamma colourBlack = new PDGamma(); 95 96 PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); 97 borderThick.setWidth(inch/12); PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary(); 99 borderThin.setWidth(inch/72); PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); 101 borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); 102 borderULine.setWidth(inch/72); 104 105 float pw = page.getMediaBox().getUpperRightX(); 106 float ph = page.getMediaBox().getUpperRightY(); 107 108 109 111 112 PDFont font = PDType1Font.HELVETICA_BOLD; 113 114 PDPageContentStream contentStream = new PDPageContentStream(document, page); 115 contentStream.beginText(); 116 contentStream.setFont( font, 18 ); 117 contentStream.moveTextPositionByAmount( inch, ph-inch-18); 118 contentStream.drawString( "PDFBox" ); 119 contentStream.moveTextPositionByAmount( 0,-(inch/2)); 120 contentStream.drawString( "Click Here" ); 121 contentStream.endText(); 122 123 contentStream.close(); 124 125 PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); 127 txtMark.setColour(colourBlue); 128 txtMark.setConstantOpacity((float)0.2); 130 132 float textWidth = (font.getStringWidth( "PDFBox" )/1000) * 18; 133 PDRectangle position = new PDRectangle(); 134 position.setLowerLeftX(inch); 135 position.setLowerLeftY( ph-inch-18 ); 136 position.setUpperRightX(72 + textWidth); 137 position.setUpperRightY(ph-inch); 138 txtMark.setRectangle(position); 139 140 float[] quads = new float[8]; 145 146 quads[0] = position.getLowerLeftX(); quads[1] = position.getUpperRightY()-2; quads[2] = position.getUpperRightX(); quads[3] = quads[1]; quads[4] = quads[0]; quads[5] = position.getLowerLeftY()-2; quads[6] = quads[2]; quads[7] = quads[5]; 155 txtMark.setQuadPoints(quads); 156 txtMark.setContents("Highlighted since it's important"); 157 158 annotations.add(txtMark); 159 160 PDAnnotationLink txtLink = new PDAnnotationLink(); 162 txtLink.setBorderStyle(borderULine); 163 164 166 textWidth = (font.getStringWidth( "Click Here" )/1000) * 18; 167 position = new PDRectangle(); 168 position.setLowerLeftX(inch); 169 position.setLowerLeftY( ph-(float)(1.5*inch)-20); position.setUpperRightX(72 + textWidth); 171 position.setUpperRightY(ph-(float)(1.5*inch)); 172 txtLink.setRectangle(position); 173 174 PDActionURI action = new PDActionURI(); 176 action.setURI("http://www.pdfbox.org"); 177 txtLink.setAction(action); 178 179 annotations.add(txtLink); 180 181 182 184 PDAnnotationSquareCircle aCircle = 185 new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE); 186 aCircle.setContents("Circle Annotation"); 187 aCircle.setInteriorColour(colourRed); aCircle.setColour(colourBlue); aCircle.setBorderStyle(borderThin); 190 191 194 position = new PDRectangle(); 195 position.setLowerLeftX(inch); 196 position.setLowerLeftY(ph-(3*inch)-inch); position.setUpperRightX(2*inch); position.setUpperRightY(ph-(3*inch)); aCircle.setRectangle(position); 200 201 annotations.add(aCircle); 203 204 206 PDAnnotationSquareCircle aSquare = 207 new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE); 208 aSquare.setContents("Square Annotation"); 209 aSquare.setColour(colourRed); aSquare.setBorderStyle(borderThick); 211 212 215 position = new PDRectangle(); position.setLowerLeftX(pw-(2*inch)); position.setLowerLeftY(ph-(float)(3.5*inch) - inch); position.setUpperRightX(pw-inch); position.setUpperRightY(ph-(float)(3.5*inch)); aSquare.setRectangle(position); 221 222 annotations.add(aSquare); 224 225 227 PDAnnotationLine aLine = new PDAnnotationLine(); 228 229 aLine.setEndPointEndingStyle( PDAnnotationLine.LE_OPEN_ARROW ); 230 aLine.setContents("Circle->Square"); 231 aLine.setCaption(true); 233 235 position = new PDRectangle(); position.setLowerLeftX(2*inch); position.setLowerLeftY(ph-(float)(3.5*inch)-inch); position.setUpperRightX(pw-inch-inch); position.setUpperRightY(ph-(3*inch)); aLine.setRectangle(position); 241 242 float[] linepos = new float[4]; 244 linepos[0] = 2*inch; linepos[1] = ph-(float)(3.5*inch); linepos[2] = pw-(2*inch); linepos[3] = ph-(4*inch); aLine.setLine(linepos); 249 250 aLine.setBorderStyle(borderThick); 251 aLine.setColour(colourBlack); 252 253 annotations.add(aLine); 255 256 257 259 260 document.save( args[0] ); 261 } 262 finally 263 { 264 document.close(); 265 } 266 } 267 } 268 269 272 private static void usage() 273 { 274 System.err.println( "Usage: java org.pdfbox.examples.pdmodel.Annotation <output-pdf>" ); 275 } 276 } | Popular Tags |