📄 pdfield.java
字号:
/**
* Copyright (c) 2003-2006, 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.interactive.form;
import org.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions;
import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.pdfbox.pdmodel.common.COSArrayList;
import org.pdfbox.pdmodel.common.COSObjectable;
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.pdmodel.common.PDTextStream;
import org.pdfbox.pdmodel.fdf.FDFField;
import org.pdfbox.util.BitFlagHelper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* This is the superclass for a Field element in a PDF.
* Based on the COS object model from PDFBox.
*
* @author sug
* @version $Revision: 1.23 $
*/
public abstract class PDField implements COSObjectable
{
/**
* A Ff flag.
*/
public static final int FLAG_READ_ONLY = 1;
/**
* A Ff flag.
*/
public static final int FLAG_REQUIRED = 1 << 1;
/**
* A Ff flag.
*/
public static final int FLAG_NO_EXPORT = 1 << 2;
private PDAcroForm acroForm;
private COSDictionary dictionary;
/**
* Constructor.
*
* @param theAcroForm The form that this field is part of.
*/
public PDField( PDAcroForm theAcroForm )
{
acroForm = theAcroForm;
dictionary = new COSDictionary();
//no required fields in base field class
}
/**
* Creates a COSField from a COSDictionary, expected to be
* a correct object definition for a field in PDF.
*
* @param theAcroForm The form that this field is part of.
* @param field the PDF objet to represent as a field.
*/
public PDField(PDAcroForm theAcroForm, COSDictionary field)
{
acroForm = theAcroForm;
dictionary = field;
}
/**
* Returns the partial name of the field.
*
* @return the name of the field
*/
public String getPartialName()
{
return getDictionary().getString( "T" );
}
/**
* This will set the partial name of the field.
*
* @param name The new name for the field.
*/
public void setPartialName( String name )
{
getDictionary().setString( "T", name );
}
/**
* Returns the fully qualified name of the field, which is a concatenation of
* the names of all the parents fields.
*
* @return the name of the field
*
* @throws IOException If there is an error generating the fully qualified name.
*/
public String getFullyQualifiedName() throws IOException
{
PDField parent = getParent();
String parentName = null;
if( parent != null )
{
parentName = parent.getFullyQualifiedName();
}
String finalName = getPartialName();
if( parentName != null )
{
finalName = parentName + "." + finalName;
}
return finalName;
}
/**
* Get the FT entry of the field. This is a read only field and is set depending
* on the actual type. The field type is an inheritable attribute. This method will
* return only the direct value on this object. Use the findFieldType for an upward
* recursive search.
*
* @return The Field type.
*
* @see PDField#findFieldType()
*/
public String getFieldType()
{
return getDictionary().getNameAsString( "FT" );
}
/**
* Find the field type and optionally do a recursive upward search. Sometimes the fieldtype
* will be specified on the parent instead of the direct object. This will look at this
* object for the field type, if none is specified then it will look to the parent if there
* is a parent. If there is no parent and no field type has been found then this
* will return null.
*
* @return The field type or null if none was found.
*/
public String findFieldType()
{
return findFieldType( getDictionary() );
}
private String findFieldType( COSDictionary dic )
{
String retval = dic.getNameAsString( "FT" );
if( retval == null )
{
COSDictionary parent = (COSDictionary)dic.getDictionaryObject( "Parent", "P" );
if( parent != null )
{
retval = findFieldType( parent );
}
}
return retval;
}
/**
* setValue sets the fields value to a given string.
*
* @param value the string value
*
* @throws IOException If there is an error creating the appearance stream.
*/
public abstract void setValue(String value) throws IOException;
/**
* getValue gets the fields value to as a string.
*
* @return The string value of this field.
*
* @throws IOException If there is an error getting the value.
*/
public abstract String getValue() throws IOException;
/**
* sets the field to be read-only.
*
* @param readonly The new flag for readonly.
*/
public void setReadonly(boolean readonly)
{
BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_READ_ONLY, readonly );
}
/**
*
* @return true if the field is readonly
*/
public boolean isReadonly()
{
return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_READ_ONLY );
}
/**
* sets the field to be required.
*
* @param required The new flag for required.
*/
public void setRequired(boolean required)
{
BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_REQUIRED, required );
}
/**
*
* @return true if the field is required
*/
public boolean isRequired()
{
return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_REQUIRED );
}
/**
* sets the field to be not exported..
*
* @param noExport The new flag for noExport.
*/
public void setNoExport(boolean noExport)
{
BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_NO_EXPORT, noExport );
}
/**
*
* @return true if the field is not to be exported.
*/
public boolean isNoExport()
{
return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_NO_EXPORT );
}
/**
* This will get the flags for this field.
*
* @return flags The set of flags.
*/
public int getFieldFlags()
{
int retval = 0;
COSInteger ff = (COSInteger)getDictionary().getDictionaryObject( COSName.getPDFName( "Ff" ) );
if( ff != null )
{
retval = ff.intValue();
}
return retval;
}
/**
* This will set the flags for this field.
*
* @param flags The new flags.
*/
public void setFieldFlags( int flags )
{
COSInteger ff = new COSInteger( flags );
getDictionary().setItem( COSName.getPDFName( "Ff" ), ff );
}
/**
* This will import a fdf field from a fdf document.
*
* @param fdfField The fdf field to import.
*
* @throws IOException If there is an error importing the data for this field.
*/
public void importFDF( FDFField fdfField ) throws IOException
{
Object fieldValue = fdfField.getValue();
int fieldFlags = getFieldFlags();
if( fieldValue != null )
{
if( fieldValue instanceof String )
{
setValue( (String)fieldValue );
}
else if( fieldValue instanceof PDTextStream )
{
setValue( ((PDTextStream)fieldValue).getAsString() );
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -