Top Description Fields Constructors Methods
org.apache.pdfbox.cos

public final Class COSString

extends COSBase
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, java.nio.charset.StandardCharsets, java.util.Arrays, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.util.Hex

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.

Authors
Ben Litchfield, John Hewson

Field Summary

Modifier and TypeField and Description
private byte[]
public static final boolean
private boolean
private static final Log

Constructor Summary

AccessConstructor and Description
public
COSString(byte[]
The raw bytes of the PDF text string or byte string.
bytes
)

Creates a new PDF string from a byte array.

public
COSString(byte[]
The raw bytes of the PDF text string or byte string.
bytes
,
boolean
forces the hexadecimal presentation of the string if set to true
forceHex
)

Creates a new PDF string from a byte array.

public
COSString(String
The string value of the object.
text
)

Creates a new text string from a Java String.

public
COSString(String
The string value of the object.
text
,
boolean
forces the hexadecimal presentation of the string if set to true
forceHex
)

Creates a new text string from a Java String.

Method Summary

Modifier and TypeMethod and Description
public void
accept(ICOSVisitor
The object to notify when visiting this object.
visitor
)

Implements abstract org.apache.pdfbox.cos.COSBase.accept.

Visitor pattern double dispatch method.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public String

Returns:

the ASCII string representation of the COSString
getASCII
()

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

public byte[]

Returns:

a clone of the underlying byte[] representation of the COSString
getBytes
()

Returns the raw bytes of the string using a new byte array.

public boolean

Returns:

true if the COSString is written in hex form
getForceHexForm
()

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

public String

Returns:

the PDF string representation of the COSString
getString
()

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

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public static COSString

Returns:

A cos string with the hex characters converted to their actual bytes.
parseHex
(String
A hex string.
hex
)

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

public void
setForceHexForm(boolean
True to force hex.
value
)
Deprecated to be removed in a future release.

Sets whether to force the string is to be written in hex form.

public void
setValue(byte[]
The raw bytes of the PDF text string or byte string.
value
)
Deprecated to be removed in a future release.

Sets the raw value of this string.

public String

Returns:

A hex string representing the bytes in this string.
toHexString
()

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

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from org.apache.pdfbox.cos.COSBase:
getCOSObjectgetKeyisDirectsetDirectsetKey

Field Detail

bytesback to summary
private byte[] bytes
FORCE_PARSINGback to summary
public static final boolean FORCE_PARSING
forceHexFormback to summary
private boolean forceHexForm
LOGback to summary
private static final Log LOG

Constructor Detail

COSStringback to summary
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:byte[]

The raw bytes of the PDF text string or byte string.

COSStringback to summary
public COSString(byte[] bytes, boolean forceHex)

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:byte[]

The raw bytes of the PDF text string or byte string.

forceHex:boolean

forces the hexadecimal presentation of the string if set to true

COSStringback to summary
public COSString(String text)

Creates a new text string from a Java String.

Parameters
text:String

The string value of the object.

COSStringback to summary
public COSString(String text, boolean forceHex)

Creates a new text string from a Java String.

Parameters
text:String

The string value of the object.

forceHex:boolean

forces the hexadecimal presentation of the string if set to true

Method Detail

acceptback to summary
public void accept(ICOSVisitor visitor) throws IOException

Implements abstract org.apache.pdfbox.cos.COSBase.accept.

Visitor pattern double dispatch method.

Parameters
visitor:ICOSVisitor

The object to notify when visiting this object.

Annotations
@Override
Exceptions
IOException:
If an error occurs while visiting this object.
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getASCIIback to summary
public String getASCII()

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

Returns:String

the ASCII string representation of the COSString

getBytesback to summary
public byte[] getBytes()

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

Returns:byte[]

a clone of the underlying byte[] representation of the COSString

getForceHexFormback to summary
public boolean getForceHexForm()

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

Returns:boolean

true if the COSString is written in hex form

getStringback to summary
public String getString()

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

Returns:String

the PDF string representation of the COSString

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
parseHexback to summary
public static COSString parseHex(String hex) throws IOException

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

Parameters
hex:String

A hex string.

Returns:COSString

A cos string with the hex characters converted to their actual bytes.

Exceptions
IOException:
If there is an error with the hex string.
setForceHexFormback to summary
public void setForceHexForm(boolean value)

Deprecated

to be removed in a future release.

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

Parameters
value:boolean

True to force hex.

Annotations
@Deprecated
setValueback to summary
public void setValue(byte[] value)

Deprecated

to be removed in a future release.

Sets the raw value of this string.

Parameters
value:byte[]

The raw bytes of the PDF text string or byte string.

Annotations
@Deprecated
toHexStringback to summary
public String toHexString()

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

Returns:String

A hex string representing the bytes in this string.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override