org.apache.pdfbox.cos

## Class COSString

- java.lang.Object
- - org.apache.pdfbox.cos.COSBase
  - - org.apache.pdfbox.cos.COSString

All Implemented Interfaces:
  COSObjectable

    public final class COSString
    extends COSBase

A string object, which may be a text string, a PDFDocEncoded string, ASCII string, or byte string.

Text strings are used for character strings that contain information intended to be human-readable, such as text annotations, bookmark names, article names, document information, and so forth.

PDFDocEncoded strings are used for characters that are represented in a single byte.

ASCII strings are used for characters that are represented in a single byte using ASCII encoding.

Byte strings are used for binary data represented as a series of bytes, but the encoding is not known. The bytes of the string need not represent characters.

Author:
  Ben Litchfield, John Hewson

### Field Summary

Fields
`static boolean` `FORCE_PARSING`

### Constructor Summary

Constructors
Constructor and Description
`COSString(byte[] bytes)` Creates a new PDF string from a byte array.
`COSString(String text)` Creates a new text string from a Java String.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`Object` `accept(ICOSVisitor visitor)` Visitor pattern double dispatch method.
`boolean` `equals(Object obj)`
`String` `getASCII()` Returns the content of this string as a PDF ASCII string.
`byte[]` `getBytes()` Returns the raw bytes of the string.
`boolean` `getForceHexForm()` Returns true if the string is to be written in hex form.
`String` `getString()` Returns the content of this string as a PDF text string.
`int` `hashCode()`
`static COSString` `parseHex(String hex)` This will create a COS string from a string of hex characters.
`void` `setForceHexForm(boolean value)` Sets whether to force the string is to be written in hex form.
`void` `setValue(byte[] value)` Sets the raw value of this string.
`String` `toHexString()` This will take this string and create a hex representation of the bytes that make the string.
`String` `toString()`

### Methods inherited from class org.apache.pdfbox.cos.COSBase

`getCOSObject, isDirect, setDirect`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Field Detail

#### FORCE_PARSING

    public static final boolean FORCE_PARSING

### Constructor Detail

#### COSString

    public COSString(byte[] bytes)

Creates a new PDF string from a byte array. This method can be used to read a string from an existing PDF file, or to create a new byte string.

Parameters:
  `bytes` - The raw bytes of the PDF text string or byte string.

#### COSString

    public COSString(String text)

Creates a new text string from a Java String.

Parameters:
  `text` - The string value of the object.

### Method Detail

#### parseHex

    public static COSString parseHex(String hex)
                              throws IOException

This will create a COS string from a string of hex characters.

Parameters:
  `hex` - A hex string.
Returns:
  A cos string with the hex characters converted to their actual bytes.
Throws:
  `IOException` - If there is an error with the hex string.

#### setValue

    public void setValue(byte[] value)

Sets the raw value of this string.

Parameters:
  `value` - The raw bytes of the PDF text string or byte string.

#### setForceHexForm

    public void setForceHexForm(boolean value)

Sets whether to force the string is to be written in hex form. This is needed when signing PDF files.

Parameters:
  `value` - True to force hex.

#### getForceHexForm

    public boolean getForceHexForm()

Returns true if the string is to be written in hex form.

Returns:
  the hex representation of this string.

#### getString

    public String getString()

Returns the content of this string as a PDF text string.

Returns:
  the string representation of this string using the given encoding.

#### getASCII

    public String getASCII()

Returns the content of this string as a PDF ASCII string.

Returns:
  the ASCII representation of this string.

#### getBytes

    public byte[] getBytes()

Returns the raw bytes of the string. Best used with a PDF byte string.

Returns:
  the raw bytes of this string.

#### toHexString

    public String toHexString()

This will take this string and create a hex representation of the bytes that make the string.

Returns:
  A hex string representing the bytes in this string.

#### accept

    public Object accept(ICOSVisitor visitor)
                  throws IOException

Visitor pattern double dispatch method.

Specified by:
  `accept` in class `COSBase`
Parameters:
  `visitor` - The object to notify when visiting this object.
Returns:
  any object, depending on the visitor implementation, or null
Throws:
  `IOException` - If an error occurs while visiting this object.

#### equals

    public boolean equals(Object obj)

Overrides:
  `equals` in class `Object`

#### hashCode

    public int hashCode()

Overrides:
  `hashCode` in class `Object`

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`