org.apache.pdfbox.cos.COSString Java Examples

The following examples show how to use org.apache.pdfbox.cos.COSString. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PDSeedValueCertificate.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns list of key usages of certificate strings where each string is 9 characters long and each character is
 * one of these values {0, 1, X} 0 for must not set, 1 for must set, X for don't care. each index in the string
 * represents a key usage:
 * <ol>
 * <li>digitalSignature</li>
 * <li>non-Repudiation</li>
 * <li>keyEncipherment</li>
 * <li>dataEncipherment</li>
 * <li>keyAgreement</li>
 * <li>keyCertSign</li>
 * <li>cRLSign</li>
 * <li>encipherOnly</li>
 * <li>decipherOnly</li>
 * </ol>
 * 
 * @return list of key usages
 */
public List<String> getKeyUsage()
{
    COSBase base = this.dictionary.getDictionaryObject(COSName.KEY_USAGE);
    if (base instanceof COSArray)
    {
        COSArray array = (COSArray) base;
        List<String> keyUsageExtensions = new LinkedList<String>();
        for (COSBase item : array)
        {
            if (item instanceof COSString)
            {
                keyUsageExtensions.add(((COSString) item).getString());
            }
        }
        return keyUsageExtensions;
    }
    return null;
}
 
Example #2
Source File: SecurityHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will decrypt a string.
 *
 * @param string the string to decrypt.
 * @param objNum The object number.
 * @param genNum The object generation number.
 *
 * @throws IOException If an error occurs writing the new string.
 */
private void decryptString(COSString string, long objNum, long genNum) throws IOException
{
	// String encrypted with identity filter
	if (COSName.IDENTITY.equals(stringFilterName))
	{
        return;
	}
	
    ByteArrayInputStream data = new ByteArrayInputStream(string.getBytes());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try
    {
        encryptData(objNum, genNum, data, outputStream, true /* decrypt */);
        string.setValue(outputStream.toByteArray());
    }
    catch (IOException ex)
    {
        LOG.error("Failed to decrypt COSString of length " + string.getBytes().length + 
                " in object " + objNum + ": " + ex.getMessage());
    }
}
 
Example #3
Source File: PDIndexed.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private byte[] getLookupData() throws IOException
{
    if (lookupData == null)
    {
        COSBase lookupTable = array.getObject(3);
        if (lookupTable instanceof COSString)
        {
            lookupData = ((COSString) lookupTable).getBytes();
        }
        else if (lookupTable instanceof COSStream)
        {
            lookupData = new PDStream((COSStream)lookupTable).toByteArray();
        }
        else if (lookupTable == null)
        {
            lookupData = new byte[0];
        }
        else
        {
            throw new IOException("Error: Unknown type for lookup table " + lookupTable);
        }
    }
    return lookupData;
}
 
Example #4
Source File: FDFJavaScript.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the javascript that is executed before the import.
 *
 * @return Some javascript code.
 */
public String getBefore()
{
    COSBase base = dictionary.getDictionaryObject(COSName.BEFORE);
    if (base instanceof COSString)
    {
        return ((COSString) base).getString();
    }
    else if (base instanceof COSStream)
    {
        return ((COSStream) base).toTextString();
    }
    else
    {
        return null;
    }
}
 
Example #5
Source File: FDFJavaScript.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the javascript that is executed after the import.
 *
 * @return Some javascript code.
 */
public String getAfter()
{
    COSBase base = dictionary.getDictionaryObject(COSName.AFTER);
    if (base instanceof COSString)
    {
        return ((COSString) base).getString();
    }
    else if (base instanceof COSStream)
    {
        return ((COSStream) base).toTextString();
    }
    else
    {
        return null;
    }
}
 
Example #6
Source File: FDFField.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the COS value of this field.
 * 
 * @return The COS value of the field.
 * @throws IOException If there is an error getting the value.
 */
public COSBase getCOSValue() throws IOException
{
    COSBase value = field.getDictionaryObject(COSName.V);
    
    if (value instanceof COSName)
    {
        return value;
    }
    else if (value instanceof COSArray)
    {
        return value;
    }
    else if (value instanceof COSString || value instanceof COSStream)
    {
        return value;
    }
    else if (value != null)
    {
        throw new IOException("Error:Unknown type for field import" + value);
    }
    else
    {
        return null;
    }
}
 
Example #7
Source File: FDFField.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will set the rich text that is associated with this field.
 *
 * @return The rich text XHTML stream.
 */
public String getRichText()
{
    COSBase rv = field.getDictionaryObject(COSName.RV);
    if (rv == null)
    {
        return null;
    }
    else if (rv instanceof COSString)
    {
        return ((COSString) rv).getString();
    }
    else
    {
        return ((COSStream) rv).toTextString();
    }
}
 
Example #8
Source File: FDFAnnotation.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Get a text or text stream.
 *
 * Some dictionary entries allow either a text or a text stream.
 *
 * @param base the potential text or text stream
 * @return the text stream
 */
protected final String getStringOrStream(COSBase base)
{
    if (base == null)
    {
        return "";
    }
    else if (base instanceof COSString)
    {
        return ((COSString) base).getString();
    }
    else if (base instanceof COSStream)
    {
        return ((COSStream) base).toTextString();
    }
    else
    {
        return "";
    }
}
 
Example #9
Source File: PDNameTreeNode.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will return a map of names on this level. The key will be a string,
 * and the value will depend on where this class is being used.
 *
 * @return ordered map of COS objects or <code>null</code> if the dictionary
 * contains no 'Names' entry on this level.
 *
 * @throws IOException If there is an error while creating the sub types.
 * @see #getKids()
 */
public Map<String, T> getNames() throws IOException
{
    COSArray namesArray = (COSArray)node.getDictionaryObject( COSName.NAMES );
    if( namesArray != null )
    {
        Map<String, T> names = new LinkedHashMap<String, T>();
        for( int i=0; i<namesArray.size(); i+=2 )
        {
            COSString key = (COSString)namesArray.getObject(i);
            COSBase cosValue = namesArray.getObject( i+1 );
            names.put( key.getString(), convertCOSToPD(cosValue) );
        }
        return Collections.unmodifiableMap(names);
    }
    else
    {
        return null;
    }
}
 
Example #10
Source File: COSArrayList.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private List<COSBase> toCOSObjectList( Collection<?> list )
{
    List<COSBase> cosObjects = new ArrayList<COSBase>();
    for (Object next : list)
    {
        if( next instanceof String )
        {
            cosObjects.add( new COSString( (String)next ) );
        }
        else
        {
            COSObjectable cos = (COSObjectable)next;
            cosObjects.add( cos.getCOSObject() );
        }
    }
    return cosObjects;
}
 
Example #11
Source File: COSArrayList.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void add(int index, E element)
{
    //when adding if there is a parentDict then change the item
    //in the dictionary from a single item to an array.
    if( parentDict != null )
    {
        parentDict.setItem( dictKey, array );
        //clear the parent dict so it doesn't happen again, there might be
        //a usecase for keeping the parentDict around but not now.
        parentDict = null;
    }
    actual.add( index, element );
    if( element instanceof String )
    {
        array.add( index, new COSString( (String)element ) );
    }
    else
    {
        array.add( index, ((COSObjectable)element).getCOSObject() );
    }
}
 
Example #12
Source File: PDVariableText.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Get a text as text stream.
 *
 * Some dictionary entries allow either a text or a text stream.
 *
 * @param base the potential text or text stream
 * @return the text stream
 */
protected final String getStringOrStream(COSBase base)
{
    if (base == null)
    {
        return "";
    }
    else if (base instanceof COSString)
    {
        return ((COSString)base).getString();
    }
    else if (base instanceof COSStream)
    {
        return ((COSStream)base).toTextString();
    }
    else
    {
        return "";
    }
}
 
Example #13
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Constructor for reading an existing DA string.
 * 
 * @param defaultResources DR entry
 * @param defaultAppearance DA entry
 * @throws IOException If the DA could not be parsed
 */
PDDefaultAppearanceString(COSString defaultAppearance, PDResources defaultResources) throws IOException
{
    if (defaultAppearance == null)
    {
        throw new IllegalArgumentException("/DA is a required entry");
    }
    
    if (defaultResources == null)
    {
        throw new IllegalArgumentException("/DR is a required entry");
    }
    
    this.defaultResources = defaultResources;
    processAppearanceStringOperators(defaultAppearance.getBytes());
}
 
Example #14
Source File: PDSignature.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private byte[] getConvertedContents(InputStream is) throws IOException
{
    ByteArrayOutputStream byteOS = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int c;
    while ((c = is.read(buffer)) != -1)
    {
        // Filter < and (
        if(buffer[0]==0x3C || buffer[0]==0x28)
        {
            byteOS.write(buffer, 1, c);
        }
        // Filter > and )
        else if(buffer[c-1]==0x3E || buffer[c-1]==0x29)
        {
            byteOS.write(buffer, 0, c-1);
        }
        else
        {
            byteOS.write(buffer, 0, c);
        }
    }
    is.close();

    return COSString.parseHex(byteOS.toString("ISO-8859-1")).getBytes();
}
 
Example #15
Source File: PDAnnotationMarkup.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will retrieve the rich text stream which is displayed in the popup window.
 *
 * @return the rich text stream.
 */
public String getRichContents()
{
    COSBase base = getCOSObject().getDictionaryObject(COSName.RC);
    if (base instanceof COSString)
    {
        return ((COSString) base).getString();
    }
    else if (base instanceof COSStream)
    {
        return ((COSStream) base).toTextString();
    }
    else
    {
        return null;
    }
}
 
Example #16
Source File: PDNameTreeNode.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Set the names for this node. This method will set the appropriate upper and lower limits
 * based on the keys in the map.
 *
 * @param names map of names to objects, or <code>null</code> for nothing.
 */
public void setNames( Map<String, T> names )
{
    if( names == null )
    {
        node.setItem( COSName.NAMES, (COSObjectable)null );
        node.setItem( COSName.LIMITS, (COSObjectable)null);
    }
    else
    {
        COSArray array = new COSArray();
        List<String> keys = new ArrayList<String>(names.keySet());
        Collections.sort(keys);
        for (String key : keys) 
        {
            array.add(new COSString(key));
            array.add(names.get(key));
        }
        node.setItem(COSName.NAMES, array);
        calculateLimits();
    }
}
 
Example #17
Source File: PdfUtils.java    From geoportal-server-harvester with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a datum string for the projection.
 * 
 * @param datumObj the "Datum" property in the GeoPDF's projection dictionary
 * 
 * @returns the datum string. If it can't determine the appropriate string, defaults to WGS 84.
 */
private static String datumTranslation(COSBase datumObj) {

    if (datumObj instanceof COSString) {
        String datumKey = ((COSString) datumObj).getString();

        if (datumKey.startsWith("NAS")) {
            return "GEOGCS[\"GCS_North_American_1927\",DATUM[\"D_North_American_1927\",SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";
        } else if (datumKey.startsWith("WG") || datumKey.equals("WE")) {
            return "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";
        }

        // TODO Add more datum keys, from the GeoPDF specification
    } 

    LOG.warn("Assuming WGS84 for GeoPDF datum...");
    return "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]";
}
 
Example #18
Source File: PDActionURI.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the uniform resource identifier to resolve. It should be encoded in 7-bit
 * ASCII, but UTF-8 and UTF-16 are supported too.
 *
 * @return The URI entry of the specific URI action dictionary or null if there isn't any.
 */
public String getURI()
{
    COSBase base = action.getDictionaryObject(COSName.URI);
    if (base instanceof COSString)
    {
        byte[] bytes = ((COSString) base).getBytes();
        if (bytes.length >= 2)
        {
            // UTF-16 (BE)
            if ((bytes[0] & 0xFF) == 0xFE && (bytes[1] & 0xFF) == 0xFF)
            {
                return action.getString(COSName.URI);
            }
            // UTF-16 (LE)
            if ((bytes[0] & 0xFF) == 0xFF && (bytes[1] & 0xFF) == 0xFE)
            {
                return action.getString(COSName.URI);
            }
        }
        return new String(bytes, Charsets.UTF_8);
    }
    return null;
}
 
Example #19
Source File: PDTargetDirectory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * If the value in the /P entry is a string, this will get a named destination in the current
 * document that provides the page number of the file attachment annotation.
 *
 * @return a named destination or null if the /P entry value is missing or not a string.
 */
public PDNamedDestination getNamedDestination()
{
    COSBase base = dict.getDictionaryObject(COSName.P);
    if (base instanceof COSString)
    {
        return new PDNamedDestination((COSString) base);
    }
    return null;
}
 
Example #20
Source File: PDSignature.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the contents.
 *
 * @param bytes contents to be used
 */
public void setContents(byte[] bytes)
{
    COSString string = new COSString(bytes);
    string.setForceHexForm(true);
    dictionary.setItem(COSName.CONTENTS, string);
}
 
Example #21
Source File: PDSeedValueCertificate.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * (Optional) A list of byte arrays containing DER-encoded X.509v3 certificates that are
 * acceptable for signing. if
 * <b>Subject</b> is not null and {@link #isSubjectRequired()} is true then the subject
 * constraint is enforced on the subjects in this array subjects.
 *
 * @param subjects list of byte arrays containing DER-encoded X.509v3 certificates that are
 * acceptable for signing.
 */
public void setSubject(List<byte[]> subjects)
{
    COSArray array = new COSArray();
    for (byte[] subject : subjects)
    {
        array.add(new COSString(subject));
    }
    this.dictionary.setItem(COSName.SUBJECT, array);
}
 
Example #22
Source File: COSArrayList.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will take an array of COSString and return a COSArrayList of
 * java.lang.String values.
 *
 * @param stringArray The existing name Array.
 *
 * @return The list of String objects.
 */
public static List<String> convertCOSStringCOSArrayToList( COSArray stringArray )
{
    List<String> retval = null;
    if( stringArray != null )
    {
        List<String> string = new ArrayList<String>();
        for( int i=0; i<stringArray.size(); i++ )
        {
            string.add( ((COSString)stringArray.getObject( i )).getString() );
        }
        retval = new COSArrayList<String>( string, stringArray );
    }
    return retval;
}
 
Example #23
Source File: COSArrayList.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean add(E o)
{
    //when adding if there is a parentDict then change the item
    //in the dictionary from a single item to an array.
    if( parentDict != null )
    {
        parentDict.setItem( dictKey, array );
        //clear the parent dict so it doesn't happen again, there might be
        //a usecase for keeping the parentDict around but not now.
        parentDict = null;
    }
    //string is a special case because we can't subclass to be COSObjectable
    if( o instanceof String )
    {
        array.add( new COSString( (String)o ) );
    }
    else
    {
        if(array != null)
        {
            array.add(((COSObjectable)o).getCOSObject());
        }
    }
    return actual.add(o);
}
 
Example #24
Source File: PDSeedValueCertificate.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Removes a key usage extension
 *
 * @param keyUsageExtension ASCII string that consists of {0, 1, X}
 */
public void removeKeyUsage(String keyUsageExtension)
{
    COSBase base = this.dictionary.getDictionaryObject(COSName.KEY_USAGE);
    if (base instanceof COSArray)
    {
        COSArray array = (COSArray) base;
        array.remove(new COSString(keyUsageExtension));
    }
}
 
Example #25
Source File: PDEncryption.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will get the UE entry in the standard encryption dictionary.
 *
 * @return A 32 byte array or null if there is no user encryption key.
 *
 * @throws IOException If there is an error accessing the data.
 */
public byte[] getUserEncryptionKey() throws IOException
{
    byte[] ue = null;
    COSString userEncryptionKey = (COSString)dictionary.getDictionaryObject( COSName.UE );
    if( userEncryptionKey != null )
    {
        ue = userEncryptionKey.getBytes();
    }
    return ue;
}
 
Example #26
Source File: PDSeedValueCertificate.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static List<byte[]> getListOfByteArraysFromCOSArray(COSArray array)
{
    List<byte[]> result = new LinkedList<byte[]>();
    for (COSBase item : array)
    {
        if (item instanceof COSString)
        {
            result.add(((COSString) item).getBytes());
        }
    }
    return result;
}
 
Example #27
Source File: PDFontDescriptor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the Panose entry of the Style dictionary, if any.
 *
 * @return A Panose wrapper object.
 */
public PDPanose getPanose()
{
    COSDictionary style = (COSDictionary)dic.getDictionaryObject(COSName.STYLE);
    if (style != null)
    {
        COSString panose = (COSString)style.getDictionaryObject(COSName.PANOSE);
        byte[] bytes = panose.getBytes();
        return new PDPanose(bytes);
    }
    return null;
}
 
Example #28
Source File: PDFontDescriptor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * A string representing the preferred font family.
 *
 * @return The font family.
 */
public String getFontFamily()
{
    String retval = null;
    COSString name = (COSString)dic.getDictionaryObject( COSName.FONT_FAMILY );
    if( name != null )
    {
        retval = name.getString();
    }
    return retval;
}
 
Example #29
Source File: PdfBoxDict.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] getBinariesValue(String name) throws IOException {
	COSBase val = wrapped.getDictionaryObject(name);
	if (val instanceof COSString) {
		return ((COSString) val).getBytes();
	}
	throw new IOException(name + " was expected to be a COSString element but was : " + val);
}
 
Example #30
Source File: PDStandardAttributeObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets an array of strings.
 * 
 * @param name the attribute name
 * @param values the array of strings
 */
protected void setArrayOfString(String name, String[] values)
{
    COSBase oldBase = this.getCOSObject().getDictionaryObject(name);
    COSArray array = new COSArray();
    for (String value : values)
    {
        array.add(new COSString(value));
    }
    this.getCOSObject().setItem(name, array);
    COSBase newBase = this.getCOSObject().getDictionaryObject(name);
    this.potentiallyNotifyChanged(oldBase, newBase);
}