From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../java/org/pdfbox/pdmodel/fdf/FDFAnnotation.java | 114 +++ .../java/org/pdfbox/pdmodel/fdf/FDFCatalog.java | 195 ++++++ .../java/org/pdfbox/pdmodel/fdf/FDFDictionary.java | 465 +++++++++++++ .../java/org/pdfbox/pdmodel/fdf/FDFDocument.java | 377 ++++++++++ src/main/java/org/pdfbox/pdmodel/fdf/FDFField.java | 763 +++++++++++++++++++++ .../java/org/pdfbox/pdmodel/fdf/FDFIconFit.java | 227 ++++++ .../java/org/pdfbox/pdmodel/fdf/FDFJavaScript.java | 172 +++++ .../pdfbox/pdmodel/fdf/FDFNamedPageReference.java | 128 ++++ .../org/pdfbox/pdmodel/fdf/FDFOptionElement.java | 129 ++++ src/main/java/org/pdfbox/pdmodel/fdf/FDFPage.java | 148 ++++ .../java/org/pdfbox/pdmodel/fdf/FDFPageInfo.java | 85 +++ .../java/org/pdfbox/pdmodel/fdf/FDFTemplate.java | 167 +++++ src/main/java/org/pdfbox/pdmodel/fdf/package.html | 9 + 13 files changed, 2979 insertions(+) create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFAnnotation.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFCatalog.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFDictionary.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFDocument.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFField.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFIconFit.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFJavaScript.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFNamedPageReference.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFOptionElement.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFPage.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFPageInfo.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/FDFTemplate.java create mode 100644 src/main/java/org/pdfbox/pdmodel/fdf/package.html (limited to 'src/main/java/org/pdfbox/pdmodel/fdf') diff --git a/src/main/java/org/pdfbox/pdmodel/fdf/FDFAnnotation.java b/src/main/java/org/pdfbox/pdmodel/fdf/FDFAnnotation.java new file mode 100644 index 0000000..3d121e7 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/fdf/FDFAnnotation.java @@ -0,0 +1,114 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.fdf; + +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSInteger; +import org.pdfbox.cos.COSNumber; + +import org.pdfbox.pdmodel.common.COSObjectable; + +/** + * This represents an FDF annotation that is part of the FDF document. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.2 $ + */ +public class FDFAnnotation implements COSObjectable +{ + private COSDictionary annot; + + /** + * Default constructor. + */ + public FDFAnnotation() + { + annot = new COSDictionary(); + } + + /** + * Constructor. + * + * @param a The FDF annotation. + */ + public FDFAnnotation( COSDictionary a ) + { + annot = a; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSBase getCOSObject() + { + return annot; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSDictionary getCOSDictionary() + { + return annot; + } + + /** + * This will get the page number or null if it does not exist. + * + * @return The page number. + */ + public Integer getPage() + { + Integer retval = null; + COSNumber page = (COSNumber)annot.getDictionaryObject( "Page" ); + if( page != null ) + { + retval = new Integer( page.intValue() ); + } + return retval; + } + + /** + * This will set the page. + * + * @param page The page number. + */ + public void setPage( int page ) + { + annot.setItem( "Page", new COSInteger( page ) ); + } + +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/pdmodel/fdf/FDFCatalog.java b/src/main/java/org/pdfbox/pdmodel/fdf/FDFCatalog.java new file mode 100644 index 0000000..f6959ee --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/fdf/FDFCatalog.java @@ -0,0 +1,195 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.fdf; + +import java.io.IOException; +import java.io.Writer; + +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; + +import org.pdfbox.pdmodel.common.COSObjectable; + +import org.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; + +import org.w3c.dom.Element; + +/** + * This represents an FDF catalog that is part of the FDF document. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.2 $ + */ +public class FDFCatalog implements COSObjectable +{ + private COSDictionary catalog; + + /** + * Default constructor. + */ + public FDFCatalog() + { + catalog = new COSDictionary(); + } + + /** + * Constructor. + * + * @param cat The FDF documents catalog. + */ + public FDFCatalog( COSDictionary cat ) + { + catalog = cat; + } + + /** + * This will create an FDF catalog from an XFDF XML document. + * + * @param element The XML document that contains the XFDF data. + * @throws IOException If there is an error reading from the dom. + */ + public FDFCatalog( Element element ) throws IOException + { + this(); + FDFDictionary fdfDict = new FDFDictionary( element ); + setFDF( fdfDict ); + } + + /** + * This will write this element as an XML document. + * + * @param output The stream to write the xml to. + * + * @throws IOException If there is an error writing the XML. + */ + public void writeXML( Writer output ) throws IOException + { + FDFDictionary fdf = getFDF(); + fdf.writeXML( output ); + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSBase getCOSObject() + { + return catalog; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSDictionary getCOSDictionary() + { + return catalog; + } + + /** + * This will get the version that was specified in the catalog dictionary. + * + * @return The FDF version. + */ + public String getVersion() + { + return catalog.getNameAsString( "Version" ); + } + + /** + * This will set the version of the FDF document. + * + * @param version The new version for the FDF document. + */ + public void setVersion( String version ) + { + catalog.setName( "Version", version ); + } + + /** + * This will get the FDF dictionary. + * + * @return The FDF dictionary. + */ + public FDFDictionary getFDF() + { + COSDictionary fdf = (COSDictionary)catalog.getDictionaryObject( "FDF" ); + FDFDictionary retval = null; + if( fdf != null ) + { + retval = new FDFDictionary( fdf ); + } + else + { + retval = new FDFDictionary(); + setFDF( retval ); + } + return retval; + } + + /** + * This will set the FDF document. + * + * @param fdf The new FDF dictionary. + */ + public void setFDF( FDFDictionary fdf ) + { + catalog.setItem( "FDF", fdf ); + } + + /** + * This will get the signature or null if there is none. + * + * @return The signature. + */ + public PDSignature getSignature() + { + PDSignature signature = null; + COSDictionary sig = (COSDictionary)catalog.getDictionaryObject( "Sig" ); + if( sig != null ) + { + signature = new PDSignature( sig ); + } + return signature; + } + + /** + * This will set the signature that is associated with this catalog. + * + * @param sig The new signature. + */ + public void setSignature( PDSignature sig ) + { + catalog.setItem( "Sig", sig ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/pdmodel/fdf/FDFDictionary.java b/src/main/java/org/pdfbox/pdmodel/fdf/FDFDictionary.java new file mode 100644 index 0000000..ab25bc7 --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/fdf/FDFDictionary.java @@ -0,0 +1,465 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.fdf; + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSStream; +import org.pdfbox.cos.COSString; + +import org.pdfbox.pdmodel.common.COSObjectable; +import org.pdfbox.pdmodel.common.COSArrayList; +import org.pdfbox.pdmodel.common.filespecification.PDFileSpecification; +import org.pdfbox.pdmodel.common.filespecification.PDSimpleFileSpecification; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * This represents an FDF dictionary that is part of the FDF document. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.6 $ + */ +public class FDFDictionary implements COSObjectable +{ + private COSDictionary fdf; + + /** + * Default constructor. + */ + public FDFDictionary() + { + fdf = new COSDictionary(); + } + + /** + * Constructor. + * + * @param fdfDictionary The FDF documents catalog. + */ + public FDFDictionary( COSDictionary fdfDictionary ) + { + fdf = fdfDictionary; + } + + /** + * This will create an FDF dictionary from an XFDF XML document. + * + * @param fdfXML The XML document that contains the XFDF data. + * @throws IOException If there is an error reading from the dom. + */ + public FDFDictionary( Element fdfXML ) throws IOException + { + this(); + NodeList nodeList = fdfXML.getChildNodes(); + for( int i=0; i\n" ); + } + COSArray ids = this.getID(); + if( ids != null ) + { + COSString original = (COSString)ids.getObject( 0 ); + COSString modified = (COSString)ids.getObject( 1 ); + output.write( "\n"); + } + List fields = getFields(); + if( fields != null && fields.size() > 0 ) + { + output.write( "\n" ); + for( int i=0; i\n" ); + } + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSBase getCOSObject() + { + return fdf; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSDictionary getCOSDictionary() + { + return fdf; + } + + /** + * The source file or target file: the PDF document file that + * this FDF file was exported from or is intended to be imported into. + * + * @return The F entry of the FDF dictionary. + */ + public PDFileSpecification getFile() + { + return PDFileSpecification.createFS( fdf.getDictionaryObject( "F" ) ); + } + + /** + * This will set the file specification. + * + * @param fs The file specification. + */ + public void setFile( PDFileSpecification fs ) + { + fdf.setItem( "F", fs ); + } + + /** + * This is the FDF id. + * + * @return The FDF ID. + */ + public COSArray getID() + { + return (COSArray)fdf.getDictionaryObject( "ID" ); + } + + /** + * This will set the FDF id. + * + * @param id The new id for the FDF. + */ + public void setID( COSArray id ) + { + fdf.setItem( "ID", id ); + } + + /** + * This will get the list of FDF Fields. This will return a list of FDFField + * objects. + * + * @return A list of FDF fields. + */ + public List getFields() + { + List retval = null; + COSArray fieldArray = (COSArray)fdf.getDictionaryObject( "Fields" ); + if( fieldArray != null ) + { + List fields = new ArrayList(); + for( int i=0; i\n" ); + output.write( "\n" ); + + getCatalog().writeXML( output ); + + output.write( "\n" ); + } + + + + /** + * This will get the low level document. + * + * @return The document that this layer sits on top of. + */ + public COSDocument getDocument() + { + return document; + } + + /** + * This will get the FDF Catalog. This is guaranteed to not return null. + * + * @return The documents /Root dictionary + */ + public FDFCatalog getCatalog() + { + FDFCatalog retval = null; + COSDictionary trailer = document.getTrailer(); + COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT ); + if( root == null ) + { + retval = new FDFCatalog(); + setCatalog( retval ); + } + else + { + retval = new FDFCatalog( root ); + } + return retval; + } + + /** + * This will set the FDF catalog for this FDF document. + * + * @param cat The FDF catalog. + */ + public void setCatalog( FDFCatalog cat ) + { + COSDictionary trailer = document.getTrailer(); + trailer.setItem( COSName.ROOT, cat ); + } + + /** + * This will load a document from a file. + * + * @param filename The name of the file to load. + * + * @return The document that was loaded. + * + * @throws IOException If there is an error reading from the stream. + */ + public static FDFDocument load( String filename ) throws IOException + { + return load( new BufferedInputStream( new FileInputStream( filename ) ) ); + } + + /** + * This will load a document from a file. + * + * @param file The name of the file to load. + * + * @return The document that was loaded. + * + * @throws IOException If there is an error reading from the stream. + */ + public static FDFDocument load( File file ) throws IOException + { + return load( new BufferedInputStream( new FileInputStream( file ) ) ); + } + + /** + * This will load a document from an input stream. + * + * @param input The stream that contains the document. + * + * @return The document that was loaded. + * + * @throws IOException If there is an error reading from the stream. + */ + public static FDFDocument load( InputStream input ) throws IOException + { + PDFParser parser = new PDFParser( input ); + parser.parse(); + return parser.getFDFDocument(); + } + + /** + * This will load a document from a file. + * + * @param filename The name of the file to load. + * + * @return The document that was loaded. + * + * @throws IOException If there is an error reading from the stream. + */ + public static FDFDocument loadXFDF( String filename ) throws IOException + { + return loadXFDF( new BufferedInputStream( new FileInputStream( filename ) ) ); + } + + /** + * This will load a document from a file. + * + * @param file The name of the file to load. + * + * @return The document that was loaded. + * + * @throws IOException If there is an error reading from the stream. + */ + public static FDFDocument loadXFDF( File file ) throws IOException + { + return loadXFDF( new BufferedInputStream( new FileInputStream( file ) ) ); + } + + /** + * This will load a document from an input stream. + * + * @param input The stream that contains the document. + * + * @return The document that was loaded. + * + * @throws IOException If there is an error reading from the stream. + */ + public static FDFDocument loadXFDF( InputStream input ) throws IOException + { + Document doc = XMLUtil.parse( input ); + return new FDFDocument( doc ); + } + + /** + * This will save this document to the filesystem. + * + * @param fileName The file to save as. + * + * @throws IOException If there is an error saving the document. + * @throws COSVisitorException If an error occurs while generating the data. + */ + public void save( File fileName ) throws IOException, COSVisitorException + { + save( new FileOutputStream( fileName ) ); + } + + /** + * This will save this document to the filesystem. + * + * @param fileName The file to save as. + * + * @throws IOException If there is an error saving the document. + * @throws COSVisitorException If an error occurs while generating the data. + */ + public void save( String fileName ) throws IOException, COSVisitorException + { + save( new FileOutputStream( fileName ) ); + } + + /** + * This will save the document to an output stream. + * + * @param output The stream to write to. + * + * @throws IOException If there is an error writing the document. + * @throws COSVisitorException If an error occurs while generating the data. + */ + public void save( OutputStream output ) throws IOException, COSVisitorException + { + COSWriter writer = null; + try + { + writer = new COSWriter( output ); + writer.write( document ); + writer.close(); + } + finally + { + if( writer != null ) + { + writer.close(); + } + } + } + + /** + * This will save this document to the filesystem. + * + * @param fileName The file to save as. + * + * @throws IOException If there is an error saving the document. + * @throws COSVisitorException If an error occurs while generating the data. + */ + public void saveXFDF( File fileName ) throws IOException, COSVisitorException + { + saveXFDF( new BufferedWriter( new FileWriter( fileName ) ) ); + } + + /** + * This will save this document to the filesystem. + * + * @param fileName The file to save as. + * + * @throws IOException If there is an error saving the document. + * @throws COSVisitorException If an error occurs while generating the data. + */ + public void saveXFDF( String fileName ) throws IOException, COSVisitorException + { + saveXFDF( new BufferedWriter( new FileWriter( fileName ) ) ); + } + + /** + * This will save the document to an output stream and close the stream. + * + * @param output The stream to write to. + * + * @throws IOException If there is an error writing the document. + * @throws COSVisitorException If an error occurs while generating the data. + */ + public void saveXFDF( Writer output ) throws IOException, COSVisitorException + { + try + { + writeXML( output ); + } + finally + { + if( output != null ) + { + output.close(); + } + } + } + + /** + * This will close the underlying COSDocument object. + * + * @throws IOException If there is an error releasing resources. + */ + public void close() throws IOException + { + document.close(); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/pdmodel/fdf/FDFField.java b/src/main/java/org/pdfbox/pdmodel/fdf/FDFField.java new file mode 100644 index 0000000..35b90ed --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/fdf/FDFField.java @@ -0,0 +1,763 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.fdf; + +import java.io.IOException; +import java.io.Writer; + +import java.util.ArrayList; +import java.util.List; + +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSInteger; +import org.pdfbox.cos.COSName; +import org.pdfbox.cos.COSNumber; +import org.pdfbox.cos.COSStream; +import org.pdfbox.cos.COSString; + +import org.pdfbox.pdmodel.common.COSObjectable; +import org.pdfbox.pdmodel.common.COSArrayList; +import org.pdfbox.pdmodel.common.PDTextStream; + +import org.pdfbox.pdmodel.interactive.action.PDActionFactory; +import org.pdfbox.pdmodel.interactive.action.PDAdditionalActions; +import org.pdfbox.pdmodel.interactive.action.type.PDAction; + +import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary; + +import org.pdfbox.util.XMLUtil; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * This represents an FDF field that is part of the FDF document. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.4 $ + */ +public class FDFField implements COSObjectable +{ + private COSDictionary field; + + /** + * Default constructor. + */ + public FDFField() + { + field = new COSDictionary(); + } + + /** + * Constructor. + * + * @param f The FDF field. + */ + public FDFField( COSDictionary f ) + { + field = f; + } + + /** + * This will create an FDF field from an XFDF XML document. + * + * @param fieldXML The XML document that contains the XFDF data. + * @throws IOException If there is an error reading from the dom. + */ + public FDFField( Element fieldXML ) throws IOException + { + this(); + this.setPartialFieldName( fieldXML.getAttribute( "name" ) ); + NodeList nodeList = fieldXML.getChildNodes(); + List kids = new ArrayList(); + for( int i=0; i 0 ) + { + setKids( kids ); + } + + } + + /** + * This will write this element as an XML document. + * + * @param output The stream to write the xml to. + * + * @throws IOException If there is an error writing the XML. + */ + public void writeXML( Writer output ) throws IOException + { + output.write( "\n"); + Object value = getValue(); + if( value != null ) + { + output.write( "" + value + "\n" ); + } + PDTextStream rt = getRichText(); + if( rt != null ) + { + output.write( "" + rt.getAsString() + "\n" ); + } + List kids = getKids(); + if( kids != null ) + { + for( int i=0; i\n"); + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSBase getCOSObject() + { + return field; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSDictionary getCOSDictionary() + { + return field; + } + + /** + * This will get the list of kids. This will return a list of FDFField objects. + * This will return null if the underlying list is null. + * + * @return The list of kids. + */ + public List getKids() + { + COSArray kids = (COSArray)field.getDictionaryObject( "Kids" ); + List retval = null; + if( kids != null ) + { + List actuals = new ArrayList(); + for( int i=0; i + * Address.State
+ * Address.City
+ * + * @return The partial field name. + */ + public String getPartialFieldName() + { + return field.getString( "T" ); + } + + /** + * This will set the partial field name. + * + * @param partial The partial field name. + */ + public void setPartialFieldName( String partial ) + { + field.setString( "T", partial ); + } + + /** + * This will set the value for the field. This will return type will either be
+ * String : Checkboxes, Radio Button
+ * java.util.List of strings: Choice Field + * PDTextStream: Textfields + * + * @return The value of the field. + * + * @throws IOException If there is an error getting the value. + */ + public Object getValue() throws IOException + { + Object retval = null; + COSBase value = field.getDictionaryObject( "V" ); + if( value instanceof COSName ) + { + retval = ((COSName)value).getName(); + } + else if( value instanceof COSArray ) + { + retval = COSArrayList.convertCOSStringCOSArrayToList( (COSArray)value ); + } + else if( value instanceof COSString || value instanceof COSStream ) + { + retval = PDTextStream.createTextStream( value ); + } + else if( value == null ) + { + //Ok, value is null so do nothing + } + else + { + throw new IOException( "Error:Unknown type for field import" + value ); + } + return retval; + } + + /** + * You should pass in a string, or a java.util.List of strings to set the + * value. + * + * @param value The value that should populate when imported. + * + * @throws IOException If there is an error setting the value. + */ + public void setValue( Object value ) throws IOException + { + COSBase cos = null; + if( value instanceof List ) + { + cos = COSArrayList.convertStringListToCOSStringCOSArray( (List)value ); + } + else if( value instanceof String ) + { + cos = COSName.getPDFName( (String)value ); + } + else if( value instanceof COSObjectable ) + { + cos = ((COSObjectable)value).getCOSObject(); + } + else if( value == null ) + { + //do nothing and let cos remain null as well. + } + else + { + throw new IOException( "Error:Unknown type for field import" + value ); + } + field.setItem( "V", cos ); + } + + /** + * This will get the Ff entry of the cos dictionary. If it it not present then + * this method will return null. + * + * @return The field flags. + */ + public Integer getFieldFlags() + { + Integer retval = null; + COSNumber ff = (COSNumber)field.getDictionaryObject( "Ff" ); + if( ff != null ) + { + retval = new Integer( ff.intValue() ); + } + return retval; + } + + /** + * This will get the field flags that are associated with this field. The Ff entry + * in the FDF field dictionary. + * + * @param ff The new value for the field flags. + */ + public void setFieldFlags( Integer ff ) + { + COSInteger value = null; + if( ff != null ) + { + value = new COSInteger( ff.intValue() ); + } + field.setItem( "Ff", value ); + } + + /** + * This will get the field flags that are associated with this field. The Ff entry + * in the FDF field dictionary. + * + * @param ff The new value for the field flags. + */ + public void setFieldFlags( int ff ) + { + field.setItem( "Ff", new COSInteger( ff ) ); + } + + /** + * This will get the SetFf entry of the cos dictionary. If it it not present then + * this method will return null. + * + * @return The field flags. + */ + public Integer getSetFieldFlags() + { + Integer retval = null; + COSNumber ff = (COSNumber)field.getDictionaryObject( "SetFf" ); + if( ff != null ) + { + retval = new Integer( ff.intValue() ); + } + return retval; + } + + /** + * This will get the field flags that are associated with this field. The SetFf entry + * in the FDF field dictionary. + * + * @param ff The new value for the "set field flags". + */ + public void setSetFieldFlags( Integer ff ) + { + COSInteger value = null; + if( ff != null ) + { + value = new COSInteger( ff.intValue() ); + } + field.setItem( "SetFf", value ); + } + + /** + * This will get the field flags that are associated with this field. The SetFf entry + * in the FDF field dictionary. + * + * @param ff The new value for the "set field flags". + */ + public void setSetFieldFlags( int ff ) + { + field.setItem( "SetFf", new COSInteger( ff ) ); + } + + /** + * This will get the ClrFf entry of the cos dictionary. If it it not present then + * this method will return null. + * + * @return The field flags. + */ + public Integer getClearFieldFlags() + { + Integer retval = null; + COSNumber ff = (COSNumber)field.getDictionaryObject( "ClrFf" ); + if( ff != null ) + { + retval = new Integer( ff.intValue() ); + } + return retval; + } + + /** + * This will get the field flags that are associated with this field. The ClrFf entry + * in the FDF field dictionary. + * + * @param ff The new value for the "clear field flags". + */ + public void setClearFieldFlags( Integer ff ) + { + COSInteger value = null; + if( ff != null ) + { + value = new COSInteger( ff.intValue() ); + } + field.setItem( "ClrFf", value ); + } + + /** + * This will get the field flags that are associated with this field. The ClrFf entry + * in the FDF field dictionary. + * + * @param ff The new value for the "clear field flags". + */ + public void setClearFieldFlags( int ff ) + { + field.setItem( "ClrFf", new COSInteger( ff ) ); + } + + /** + * This will get the F entry of the cos dictionary. If it it not present then + * this method will return null. + * + * @return The widget field flags. + */ + public Integer getWidgetFieldFlags() + { + Integer retval = null; + COSNumber f = (COSNumber)field.getDictionaryObject( "F" ); + if( f != null ) + { + retval = new Integer( f.intValue() ); + } + return retval; + } + + /** + * This will get the widget field flags that are associated with this field. The F entry + * in the FDF field dictionary. + * + * @param f The new value for the field flags. + */ + public void setWidgetFieldFlags( Integer f ) + { + COSInteger value = null; + if( f != null ) + { + value = new COSInteger( f.intValue() ); + } + field.setItem( "F", value ); + } + + /** + * This will get the field flags that are associated with this field. The F entry + * in the FDF field dictionary. + * + * @param f The new value for the field flags. + */ + public void setWidgetFieldFlags( int f ) + { + field.setItem( "F", new COSInteger( f ) ); + } + + /** + * This will get the SetF entry of the cos dictionary. If it it not present then + * this method will return null. + * + * @return The field flags. + */ + public Integer getSetWidgetFieldFlags() + { + Integer retval = null; + COSNumber ff = (COSNumber)field.getDictionaryObject( "SetF" ); + if( ff != null ) + { + retval = new Integer( ff.intValue() ); + } + return retval; + } + + /** + * This will get the widget field flags that are associated with this field. The SetF entry + * in the FDF field dictionary. + * + * @param ff The new value for the "set widget field flags". + */ + public void setSetWidgetFieldFlags( Integer ff ) + { + COSInteger value = null; + if( ff != null ) + { + value = new COSInteger( ff.intValue() ); + } + field.setItem( "SetF", value ); + } + + /** + * This will get the widget field flags that are associated with this field. The SetF entry + * in the FDF field dictionary. + * + * @param ff The new value for the "set widget field flags". + */ + public void setSetWidgetFieldFlags( int ff ) + { + field.setItem( "SetF", new COSInteger( ff ) ); + } + + /** + * This will get the ClrF entry of the cos dictionary. If it it not present then + * this method will return null. + * + * @return The widget field flags. + */ + public Integer getClearWidgetFieldFlags() + { + Integer retval = null; + COSNumber ff = (COSNumber)field.getDictionaryObject( "ClrF" ); + if( ff != null ) + { + retval = new Integer( ff.intValue() ); + } + return retval; + } + + /** + * This will get the field flags that are associated with this field. The ClrF entry + * in the FDF field dictionary. + * + * @param ff The new value for the "clear widget field flags". + */ + public void setClearWidgetFieldFlags( Integer ff ) + { + COSInteger value = null; + if( ff != null ) + { + value = new COSInteger( ff.intValue() ); + } + field.setItem( "ClrF", value ); + } + + /** + * This will get the field flags that are associated with this field. The ClrF entry + * in the FDF field dictionary. + * + * @param ff The new value for the "clear field flags". + */ + public void setClearWidgetFieldFlags( int ff ) + { + field.setItem( "ClrF", new COSInteger( ff ) ); + } + + /** + * This will get the appearance dictionary that specifies the appearance of + * a pushbutton field. + * + * @return The AP entry of this dictionary. + */ + public PDAppearanceDictionary getAppearanceDictionary() + { + PDAppearanceDictionary retval = null; + COSDictionary dict = (COSDictionary)field.getDictionaryObject( "AP" ); + if( dict != null ) + { + retval = new PDAppearanceDictionary( dict ); + } + return retval; + } + + /** + * This will set the appearance dictionary. + * + * @param ap The apperance dictionary. + */ + public void setAppearanceDictionary( PDAppearanceDictionary ap ) + { + field.setItem( "AP", ap ); + } + + /** + * This will get named page references.. + * + * @return The named page references. + */ + public FDFNamedPageReference getAppearanceStreamReference() + { + FDFNamedPageReference retval = null; + COSDictionary ref = (COSDictionary)field.getDictionaryObject( "APRef" ); + if( ref != null ) + { + retval = new FDFNamedPageReference( ref ); + } + return retval; + } + + /** + * This will set the named page references. + * + * @param ref The named page references. + */ + public void setAppearanceStreamReference( FDFNamedPageReference ref ) + { + field.setItem( "APRef", ref ); + } + + /** + * This will get the icon fit that is associated with this field. + * + * @return The IF entry. + */ + public FDFIconFit getIconFit() + { + FDFIconFit retval = null; + COSDictionary dic = (COSDictionary)field.getDictionaryObject( "IF" ); + if( dic != null ) + { + retval = new FDFIconFit( dic ); + } + return retval; + } + + /** + * This will set the icon fit entry. + * + * @param fit The icon fit object. + */ + public void setIconFit( FDFIconFit fit ) + { + field.setItem( "IF", fit ); + } + + /** + * This will return a list of options for a choice field. The value in the + * list will be 1 of 2 types. java.lang.String or FDFOptionElement. + * + * @return A list of all options. + */ + public List getOptions() + { + List retval = null; + COSArray array = (COSArray)field.getDictionaryObject( "Opt" ); + if( array != null ) + { + List objects = new ArrayList(); + for( int i=0; i + * + * To quote the PDF Spec + * "An array of two numbers between 0.0 and 1.0 indicating the fraction of leftover + * space to allocate at the left and bottom of the icon. A value of [0.0 0.0] positions the + * icon at the bottom-left corner of the annotation rectangle; a value of [0.5 0.5] centers it + * within the rectangle. This entry is used only if the icon is scaled proportionally. Default + * value: [0.5 0.5]." + * + * @return The fractional space to allocate. + */ + public PDRange getFractionalSpaceToAllocate() + { + PDRange retval = null; + COSArray array = (COSArray)fit.getDictionaryObject( "A" ); + if( array == null ) + { + retval = new PDRange(); + retval.setMin( .5f ); + retval.setMax( .5f ); + setFractionalSpaceToAllocate( retval ); + } + else + { + retval = new PDRange( array ); + } + return retval; + } + + /** + * This will set frational space to allocate. + * + * @param space The space to allocate. + */ + public void setFractionalSpaceToAllocate( PDRange space ) + { + fit.setItem( "A", space ); + } + + /** + * This will tell if the icon should scale to fit the annotation bounds. Default: false + * + * @return A flag telling if the icon should scale. + */ + public boolean shouldScaleToFitAnnotation() + { + return fit.getBoolean( "FB", false ); + } + + /** + * This will tell the icon to scale. + * + * @param value The flag value. + */ + public void setScaleToFitAnnotation( boolean value ) + { + fit.setBoolean( "FB", value ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/pdmodel/fdf/FDFJavaScript.java b/src/main/java/org/pdfbox/pdmodel/fdf/FDFJavaScript.java new file mode 100644 index 0000000..dfa6b0f --- /dev/null +++ b/src/main/java/org/pdfbox/pdmodel/fdf/FDFJavaScript.java @@ -0,0 +1,172 @@ +/** + * Copyright (c) 2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.pdmodel.fdf; + +import java.util.ArrayList; +import java.util.List; + +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSName; + +import org.pdfbox.pdmodel.common.COSObjectable; +import org.pdfbox.pdmodel.common.COSArrayList; +import org.pdfbox.pdmodel.common.PDTextStream; +import org.pdfbox.pdmodel.common.PDNamedTextStream; + +/** + * This represents an FDF JavaScript dictionary that is part of the FDF document. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.2 $ + */ +public class FDFJavaScript implements COSObjectable +{ + private COSDictionary js; + + /** + * Default constructor. + */ + public FDFJavaScript() + { + js = new COSDictionary(); + } + + /** + * Constructor. + * + * @param javaScript The FDF java script. + */ + public FDFJavaScript( COSDictionary javaScript ) + { + js = javaScript; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSBase getCOSObject() + { + return js; + } + + /** + * Convert this standard java object to a COS object. + * + * @return The cos object that matches this Java object. + */ + public COSDictionary getCOSDictionary() + { + return js; + } + + /** + * This will get the javascript that is executed before the import. + * + * @return Some javascript code. + */ + public PDTextStream getBefore() + { + return PDTextStream.createTextStream( js.getDictionaryObject( "Before" ) ); + } + + /** + * This will set the javascript code the will get execute before the import. + * + * @param before A reference to some javascript code. + */ + public void setBefore( PDTextStream before ) + { + js.setItem( "Before", before ); + } + + /** + * This will get the javascript that is executed after the import. + * + * @return Some javascript code. + */ + public PDTextStream getAfter() + { + return PDTextStream.createTextStream( js.getDictionaryObject( "After" ) ); + } + + /** + * This will set the javascript code the will get execute after the import. + * + * @param after A reference to some javascript code. + */ + public void setAfter( PDTextStream after ) + { + js.setItem( "After", after ); + } + + /** + * This will return a list of PDNamedTextStream objects. This is the "Doc" + * entry of the pdf document. These will be added to the PDF documents + * javascript name tree. This will not return null. + * + * @return A list of all named javascript entries. + */ + public List getNamedJavaScripts() + { + List retval = null; + COSArray array = (COSArray)js.getDictionaryObject( "Doc" ); + List namedStreams = new ArrayList(); + if( array == null ) + { + array = new COSArray(); + js.setItem( "Doc", array ); + } + for( int i=0; i + + + + + +The fdf package will handle all of the logic used for FDF objects inside of the PDF/FDF document. + + -- cgit v1.2.3