⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfield.java

📁 非常有用的操作pdf文件的java源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                throw new IOException( "Unknown field type:" + fieldValue.getClass().getName() );
            }
        }
        Integer ff = fdfField.getFieldFlags();
        if( ff != null )
        {
            setFieldFlags( ff.intValue() );
        }
        else
        {
            //these are suppose to be ignored if the Ff is set.
            Integer setFf = fdfField.getSetFieldFlags();

            if( setFf != null )
            {
                int setFfInt = setFf.intValue();
                fieldFlags = fieldFlags | setFfInt;
                setFieldFlags( fieldFlags );
            }

            Integer clrFf = fdfField.getClearFieldFlags();
            if( clrFf != null )
            {
                //we have to clear the bits of the document fields for every bit that is
                //set in this field.
                //
                //Example:
                //docFf = 1011
                //clrFf = 1101
                //clrFfValue = 0010;
                //newValue = 1011 & 0010 which is 0010
                int clrFfValue = clrFf.intValue();
                clrFfValue ^= 0xFFFFFFFF;
                fieldFlags = fieldFlags & clrFfValue;
                setFieldFlags( fieldFlags );
            }
        }

        PDAnnotationWidget widget = getWidget();
        if( widget != null )
        {
            int annotFlags = widget.getAnnotationFlags();
            Integer f = fdfField.getWidgetFieldFlags();
            if( f != null && widget != null )
            {
                widget.setAnnotationFlags( f.intValue() );
            }
            else
            {
                //these are suppose to be ignored if the F is set.
                Integer setF = fdfField.getSetWidgetFieldFlags();
                if( setF != null )
                {
                    annotFlags = annotFlags | setF.intValue();
                    widget.setAnnotationFlags( annotFlags );
                }

                Integer clrF = fdfField.getClearWidgetFieldFlags();
                if( clrF != null )
                {
                    //we have to clear the bits of the document fields for every bit that is
                    //set in this field.
                    //
                    //Example:
                    //docF = 1011
                    //clrF = 1101
                    //clrFValue = 0010;
                    //newValue = 1011 & 0010 which is 0010
                    int clrFValue = clrF.intValue();
                    clrFValue ^= 0xFFFFFFFFL;
                    annotFlags = annotFlags & clrFValue;
                    widget.setAnnotationFlags( annotFlags );
                }
            }
        }
        List fdfKids = fdfField.getKids();
        List pdKids = getKids();
        for( int i=0; fdfKids != null && i<fdfKids.size(); i++ )
        {
            FDFField fdfChild = (FDFField)fdfKids.get( i );
            String fdfName = fdfChild.getPartialFieldName();
            for( int j=0; j<pdKids.size(); j++ )
            {
                Object pdChildObj = pdKids.get( j );
                if( pdChildObj instanceof PDField )
                {
                    PDField pdChild = (PDField)pdChildObj;
                    if( fdfName != null && fdfName.equals( pdChild.getPartialName() ) )
                    {
                        pdChild.importFDF( fdfChild );
                    }
                }
            }
        }
    }

    /**
     * This will get the single associated widget that is part of this field.  This
     * occurs when the Widget is embedded in the fields dictionary.  Sometimes there
     * are multiple sub widgets associated with this field, in which case you want to
     * use getKids().  If the kids entry is specified, then the first entry in that
     * list will be returned.
     *
     * @return The widget that is associated with this field.
     * @throws IOException If there is an error getting the widget object.
     */
    public PDAnnotationWidget getWidget() throws IOException
    {
        PDAnnotationWidget retval = null;
        List kids = getKids();
        if( kids == null )
        {
            retval = new PDAnnotationWidget( getDictionary() );
        }
        else if( kids.size() > 0 )
        {
            Object firstKid = kids.get( 0 );
            if( firstKid instanceof PDAnnotationWidget )
            {
                retval = (PDAnnotationWidget)firstKid;
            }
            else
            {
                retval = ((PDField)firstKid).getWidget();
            }
        }
        else
        {
            retval = null;
        }
        return retval;
    }
    
    /**
     * Get the parent field to this field, or null if none exists.
     * 
     * @return The parent field.
     * 
     * @throws IOException If there is an error creating the parent field.
     */
    public PDField getParent() throws IOException
    {
        PDField parent = null;
        COSDictionary parentDic = (COSDictionary)getDictionary().getDictionaryObject( "Parent" );
        if( parentDic != null )
        {
            parent = PDFieldFactory.createField( getAcroForm(), parentDic );
        }
        return parent;
    }
    
    /**
     * Set the parent of this field.
     * 
     * @param parent The parent to this field.
     */
    public void setParent( PDField parent )
    {
        getDictionary().setItem( "Parent", parent );
    }
    
    /**
     * This will find one of the child elements.  The name array are the components
     * of the name to search down the tree of names.  The nameIndex is where to
     * start in that array.  This method is called recursively until it finds
     * the end point based on the name array.
     * 
     * @param name An array that picks the path to the field.
     * @param nameIndex The index into the array.
     * @return The field at the endpoint or null if none is found.
     * @throws IOException If there is an error creating the field.
     */
    public PDField findKid( String[] name, int nameIndex ) throws IOException
    {
        PDField retval = null;
        COSArray kids = (COSArray)getDictionary().getDictionaryObject( COSName.KIDS );
        if( kids != null )
        {
            for (int i = 0; retval == null && i < kids.size(); i++)
            {
                COSDictionary kidDictionary = (COSDictionary)kids.getObject(i);
                if( name[nameIndex].equals( kidDictionary.getString( "T" ) ) )
                {
                    retval = PDFieldFactory.createField( acroForm, kidDictionary );
                    if( name.length > nameIndex+1 )
                    {
                        retval = retval.findKid( name, nameIndex+1 );
                    }
                }
            }
        }
        return retval;
    }

    /**
     * This will get all the kids of this field.  The values in the list
     * will either be PDWidget or PDField.  Normally they will be PDWidget objects
     * unless this is a non-terminal field and they will be child PDField objects.
     *
     * @return A list of either PDWidget or PDField objects.
     * @throws IOException If there is an error retrieving the kids.
     */
    public List getKids() throws IOException
    {
        List retval = null;
        COSArray kids = (COSArray)getDictionary().getDictionaryObject(COSName.KIDS);
        if( kids != null )
        {
            List kidsList = new ArrayList();
            for (int i = 0; i < kids.size(); i++)
            {
                COSDictionary kidDictionary = (COSDictionary)kids.getObject(i);
                COSDictionary parent = (COSDictionary)kidDictionary.getDictionaryObject( "Parent" );
                if( kidDictionary.getDictionaryObject( "FT" ) != null ||
                    (parent != null && parent.getDictionaryObject( "FT" ) != null ) )
                {
                    kidsList.add( PDFieldFactory.createField( acroForm, kidDictionary ));
                }
                else if( "Widget".equals( kidDictionary.getNameAsString( "Subtype" ) ) )
                {
                    kidsList.add( new PDAnnotationWidget( kidDictionary ) );
                }
                else
                {
                    //
                    kidsList.add( PDFieldFactory.createField( acroForm, kidDictionary ));
                }
            }
            retval = new COSArrayList( kidsList, kids );
        }
        return retval;
    }

    /**
     * This will set the list of kids.
     *
     * @param kids The list of child widgets.
     */
    public void setKids( List kids )
    {
        COSArray kidsArray = COSArrayList.converterToCOSArray( kids );
        getDictionary().setItem( COSName.KIDS, kidsArray );
    }

    /**
     * This will return a string representation of this field.
     *
     * @return A string representation of this field.
     */
    public String toString()
    {
        return "" + getDictionary().getDictionaryObject( COSName.getPDFName( "V" ) );
    }

    /**
     * This will get the acroform that this field is part of.
     *
     * @return The form this field is on.
     */
    public PDAcroForm getAcroForm()
    {
        return acroForm;
    }

    /**
     * This will set the form this field is on.
     *
     * @param value The new form to use.
     */
    public void setAcroForm(PDAcroForm value)
    {
        acroForm = value;
    }

    /**
     * This will get the dictionary associated with this field.
     *
     * @return The dictionary that this class wraps.
     */
    public COSDictionary getDictionary()
    {
        return dictionary;
    }

    /**
     * Convert this standard java object to a COS object.
     *
     * @return The cos object that matches this Java object.
     */
    public COSBase getCOSObject()
    {
        return dictionary;
    }
    
    /**
     * Get the additional actions for this field.  This will return null
     * if there are no additional actions for this field.
     * 
     * @return The actions of the field.
     */
    public PDFormFieldAdditionalActions getActions()
    {
        COSDictionary aa = (COSDictionary)dictionary.getDictionaryObject( "AA" );
        PDFormFieldAdditionalActions retval = null;
        if( aa != null )
        {
            retval = new PDFormFieldAdditionalActions( aa );
        }
        return retval;
    }
    
    /**
     * Set the actions of the field.
     * 
     * @param actions The field actions.
     */
    public void setActions( PDFormFieldAdditionalActions actions )
    {
        dictionary.setItem( "AA", actions );
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -