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 org.pdfbox.pdmodel.PDDocument;
033: import org.pdfbox.pdmodel.PDPage;
034: import org.pdfbox.pdmodel.common.PDRectangle;
035: import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp;
036:
037: import java.io.IOException;
038: import java.util.ArrayList;
039: import java.util.List;
040:
041: /**
042: * This is an example on how to add annotations to pages of a PDF document.
043: *
044: * @author Paul King
045: * @version $Revision: 1.1 $
046: */
047: public class RubberStamp {
048: private RubberStamp() {
049: //utility class, should not be instantiated.
050: }
051:
052: /**
053: * This will print the documents data.
054: *
055: * @param args The command line arguments.
056: *
057: * @throws Exception If there is an error parsing the document.
058: */
059: public static void main(String[] args) throws Exception {
060: if (args.length != 2) {
061: usage();
062: } else {
063: PDDocument document = null;
064: try {
065: document = PDDocument.load(args[0]);
066: if (document.isEncrypted()) {
067: throw new IOException(
068: "Encrypted documents are not supported for this example");
069: }
070: List allpages = new ArrayList();
071: document.getDocumentCatalog().getPages().getAllKids(
072: allpages);
073:
074: for (int i = 0; i < allpages.size(); i++) {
075: PDPage apage = (PDPage) allpages.get(i);
076: List annotations = apage.getAnnotations();
077:
078: PDAnnotationRubberStamp rs = new PDAnnotationRubberStamp();
079: rs.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET);
080: rs.setRectangle(new PDRectangle(100, 100));
081: rs.setContents("A top secret note");
082:
083: annotations.add(rs);
084: }
085:
086: document.save(args[1]);
087: } finally {
088: if (document != null) {
089: document.close();
090: }
091: }
092: }
093: }
094:
095: /**
096: * This will print the usage for this document.
097: */
098: private static void usage() {
099: System.err
100: .println("Usage: java org.pdfbox.examples.pdmodel.RubberStamp <input-pdf> <output-pdf>");
101: }
102: }
|