001: /**
002: * Copyright (c) 2006, 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.util;
031:
032: import org.pdfbox.cos.COSName;
033: import org.pdfbox.exceptions.InvalidPasswordException;
034: import org.pdfbox.exceptions.WrappedIOException;
035:
036: import org.pdfbox.pdmodel.PDDocument;
037: import org.pdfbox.pdmodel.PDPage;
038: import org.pdfbox.pdmodel.graphics.xobject.PDXObject;
039: import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
040: import org.pdfbox.util.Matrix;
041: import org.pdfbox.util.PDFOperator;
042: import org.pdfbox.util.PDFStreamEngine;
043: import org.pdfbox.util.ResourceLoader;
044:
045: import java.awt.geom.AffineTransform;
046: import java.awt.geom.NoninvertibleTransformException;
047: import java.io.IOException;
048:
049: import java.util.List;
050: import java.util.Map;
051:
052: /**
053: * This is an example on how to get the x/y coordinates of image locations.
054: *
055: * Usage: java org.pdfbox.examples.util.PrintImageLocations <input-pdf>
056: *
057: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
058: * @version $Revision: 1.3 $
059: */
060: public class PrintImageLocations extends PDFStreamEngine {
061: /**
062: * Default constructor.
063: *
064: * @throws IOException If there is an error loading text stripper properties.
065: */
066: public PrintImageLocations() throws IOException {
067: super (ResourceLoader
068: .loadProperties("Resources/PDFTextStripper.properties"));
069: }
070:
071: /**
072: * This will print the documents data.
073: *
074: * @param args The command line arguments.
075: *
076: * @throws Exception If there is an error parsing the document.
077: */
078: public static void main(String[] args) throws Exception {
079: if (args.length != 1) {
080: usage();
081: } else {
082: PDDocument document = null;
083: try {
084: document = PDDocument.load(args[0]);
085: if (document.isEncrypted()) {
086: try {
087: document.decrypt("");
088: } catch (InvalidPasswordException e) {
089: System.err
090: .println("Error: Document is encrypted with a password.");
091: System.exit(1);
092: }
093: }
094: PrintImageLocations printer = new PrintImageLocations();
095: List allPages = document.getDocumentCatalog()
096: .getAllPages();
097: for (int i = 0; i < allPages.size(); i++) {
098: PDPage page = (PDPage) allPages.get(i);
099: System.out.println("Processing page: " + i);
100: printer.processStream(page, page.findResources(),
101: page.getContents().getStream());
102: }
103: } finally {
104: if (document != null) {
105: document.close();
106: }
107: }
108: }
109: }
110:
111: /**
112: * This is used to handle an operation.
113: *
114: * @param operator The operation to perform.
115: * @param arguments The list of arguments.
116: *
117: * @throws IOException If there is an error processing the operation.
118: */
119: protected void processOperator(PDFOperator operator, List arguments)
120: throws IOException {
121: String operation = operator.getOperation();
122: if (operation.equals("Do")) {
123: COSName objectName = (COSName) arguments.get(0);
124: Map xobjects = getResources().getXObjects();
125: PDXObject xobject = (PDXObject) xobjects.get(objectName
126: .getName());
127: if (xobject instanceof PDXObjectImage) {
128: try {
129: PDPage page = getCurrentPage();
130: Matrix ctm = getGraphicsState()
131: .getCurrentTransformationMatrix();
132: double rotationInRadians = (page.findRotation() * Math.PI) / 180;
133:
134: AffineTransform rotation = new AffineTransform();
135: rotation.setToRotation(rotationInRadians);
136: AffineTransform rotationInverse = rotation
137: .createInverse();
138: Matrix rotationInverseMatrix = new Matrix();
139: rotationInverseMatrix
140: .setFromAffineTransform(rotationInverse);
141: Matrix rotationMatrix = new Matrix();
142: rotationMatrix.setFromAffineTransform(rotation);
143:
144: Matrix unrotatedCTM = ctm
145: .multiply(rotationInverseMatrix);
146:
147: System.out.println("Found image["
148: + objectName.getName() + "] " + "at "
149: + unrotatedCTM.getXPosition() + ","
150: + unrotatedCTM.getYPosition());
151: } catch (NoninvertibleTransformException e) {
152: throw new WrappedIOException(e);
153: }
154: }
155: } else {
156: super .processOperator(operator, arguments);
157: }
158: }
159:
160: /**
161: * This will print the usage for this document.
162: */
163: private static void usage() {
164: System.err
165: .println("Usage: java org.pdfbox.examples.pdmodel.PrintImageLocations <input-pdf>");
166: }
167:
168: }
|