001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.pdmodel;
031:
032: import java.util.List;
033:
034: import org.pdfbox.pdmodel.PDDocument;
035: import org.pdfbox.pdmodel.PDPage;
036:
037: import org.pdfbox.pdmodel.interactive.action.type.PDAction;
038: import org.pdfbox.pdmodel.interactive.action.type.PDActionURI;
039: import org.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
040: import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
041:
042: /**
043: * This is an example of how to replace a URL in a PDF document. This
044: * will only replace the URL that the text refers to and not the text
045: * itself.
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.2 $
049: */
050: public class ReplaceURLs {
051: /**
052: * Constructor.
053: */
054: private ReplaceURLs() {
055: //utility class
056: }
057:
058: /**
059: * This will read in a document and replace all of the urls with
060: * http://www.pdfbox.org.
061: * <br />
062: * see usage() for commandline
063: *
064: * @param args Command line arguments.
065: *
066: * @throws Exception If there is an error during the process.
067: */
068: public static void main(String[] args) throws Exception {
069: PDDocument doc = null;
070: try {
071: if (args.length != 2) {
072: usage();
073: } else {
074: doc = PDDocument.load(args[0]);
075: List allPages = doc.getDocumentCatalog().getAllPages();
076: for (int i = 0; i < allPages.size(); i++) {
077: PDPage page = (PDPage) allPages.get(i);
078: List annotations = page.getAnnotations();
079:
080: for (int j = 0; j < annotations.size(); j++) {
081: PDAnnotation annot = (PDAnnotation) annotations
082: .get(j);
083: if (annot instanceof PDAnnotationLink) {
084: PDAnnotationLink link = (PDAnnotationLink) annot;
085: PDAction action = link.getAction();
086: if (action instanceof PDActionURI) {
087: PDActionURI uri = (PDActionURI) action;
088: String oldURI = uri.getURI();
089: String newURI = "http://www.pdfbox.org";
090: System.out.println("Page " + (i + 1)
091: + ": Replacing " + oldURI
092: + " with " + newURI);
093: uri.setURI(newURI);
094: }
095: }
096: }
097: }
098: doc.save(args[1]);
099: }
100: } finally {
101: if (doc != null) {
102: doc.close();
103: }
104: }
105: }
106:
107: /**
108: * This will print out a message telling how to use this example.
109: */
110: private static void usage() {
111: System.err.println("usage: " + ReplaceURLs.class.getName()
112: + " <input-file> <output-file>");
113: }
114: }
|