1 31 package org.pdfbox.examples.pdmodel; 32 33 import java.util.List ; 34 35 import org.pdfbox.pdmodel.PDDocument; 36 import org.pdfbox.pdmodel.PDPage; 37 38 import org.pdfbox.pdmodel.interactive.action.type.PDAction; 39 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI; 40 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotation; 41 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink; 42 43 44 52 public class ReplaceURLs 53 { 54 57 private ReplaceURLs() 58 { 59 } 61 62 72 public static void main(String [] args) throws Exception 73 { 74 PDDocument doc = null; 75 try 76 { 77 if( args.length != 2 ) 78 { 79 usage(); 80 } 81 else 82 { 83 doc = PDDocument.load( args[0] ); 84 List allPages = doc.getDocumentCatalog().getAllPages(); 85 for( int i=0; i<allPages.size(); i++ ) 86 { 87 PDPage page = (PDPage)allPages.get( i ); 88 List annotations = page.getAnnotations(); 89 90 for( int j=0; j<annotations.size(); j++ ) 91 { 92 PDAnnotation annot = (PDAnnotation)annotations.get( j ); 93 if( annot instanceof PDAnnotationLink ) 94 { 95 PDAnnotationLink link = (PDAnnotationLink)annot; 96 PDAction action = link.getAction(); 97 if( action instanceof PDActionURI ) 98 { 99 PDActionURI uri = (PDActionURI)action; 100 String oldURI = uri.getURI(); 101 String newURI = "http://www.pdfbox.org"; 102 System.out.println( "Page " + (i+1) +": Replacing " + oldURI + " with " + newURI ); 103 uri.setURI( newURI ); 104 } 105 } 106 } 107 } 108 doc.save( args[1] ); 109 } 110 } 111 finally 112 { 113 if( doc != null ) 114 { 115 doc.close(); 116 } 117 } 118 } 119 120 123 private static void usage() 124 { 125 System.err.println( "usage: " + ReplaceURLs.class.getName() + " <input-file> <output-file>" ); 126 } 127 } | Popular Tags |