Class COSString

java.lang.Object
org.sejda.sambox.cos.COSBase
org.sejda.sambox.cos.COSString
All Implemented Interfaces:
COSObjectable, Encryptable

public final class COSString extends COSBase implements Encryptable
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
  • Constructor Details

    • 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.
  • Method Details

    • 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 or not to force the string is to be written in hex form. This is needed when signing PDF files.
      Parameters:
      value - True to force hex.
    • isForceHexForm

      public boolean isForceHexForm()
      Returns:
      true if the string is to be written in hex form.
    • getString

      public String getString()
      Returns:
      the content PDF text string as defined in Chap 7.9 of PDF 32000-1:2008.
    • getBytes

      public byte[] getBytes()
      Returns:
      the raw bytes of the string. Best used with a PDF byte string.
    • toHexString

      public String toHexString()
      Returns:
      A hex string representing the bytes in this string.
    • encryptable

      public boolean encryptable()
      Specified by:
      encryptable in interface Encryptable
      Returns:
      true if the can be encrypted
    • encryptable

      public void encryptable(boolean encryptable)
      Description copied from interface: Encryptable
      Sets if the object can be encrypted
      Specified by:
      encryptable in interface Encryptable
    • accept

      public void accept(COSVisitor visitor) throws IOException
      Description copied from class: COSBase
      Visitor pattern for the COS model objects
      Specified by:
      accept in class COSBase
      Throws:
      IOException
    • 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
    • newInstance

      public static COSString newInstance(byte[] value)
      Factory method for a COSString from a byte array
      Parameters:
      value -
      Returns:
      a new instance
    • parseLiteral

      public static COSString parseLiteral(String literal)
      Factory method creating a COSString from a literal string.
      Parameters:
      literal - A literal string.
      Returns:
      A COSString encoded with PDFDocEncoding encoding if possible, with Charsets#UTF_16BE otherwise.
      Throws:
      IOException - If there is an error with the hex string.
    • parseHex

      public static COSString parseHex(String hex) throws IOException
      Factory method creating a COSString 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.