1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.IOException ; 54 import java.io.OutputStream ; 55 56 72 73 public class PdfString extends PdfObject { 74 75 77 78 protected String value = NOTHING; 79 protected String originalValue = null; 80 81 82 protected String encoding = TEXT_PDFDOCENCODING; 83 protected int objNum = 0; 84 protected int objGen = 0; 85 protected boolean hexWriting = false; 86 87 89 92 93 public PdfString() { 94 super(STRING); 95 } 96 97 102 103 public PdfString(String value) { 104 super(STRING); 105 this.value = value; 106 } 107 108 114 115 public PdfString(String value, String encoding) { 116 super(STRING); 117 this.value = value; 118 this.encoding = encoding; 119 } 120 121 126 127 public PdfString(byte[] bytes) { 128 super(STRING); 129 value = PdfEncodings.convertToString(bytes, null); 130 encoding = NOTHING; 131 } 132 133 135 138 139 public void toPdf(PdfWriter writer, OutputStream os) throws IOException { 140 byte b[] = getBytes(); 141 PdfEncryption crypto = null; 142 if (writer != null) 143 crypto = writer.getEncryption(); 144 if (crypto != null) { 145 b = crypto.encryptByteArray(b); 146 } 147 if (hexWriting) { 148 ByteBuffer buf = new ByteBuffer(); 149 buf.append('<'); 150 int len = b.length; 151 for (int k = 0; k < len; ++k) 152 buf.appendHex(b[k]); 153 buf.append('>'); 154 os.write(buf.toByteArray()); 155 } 156 else 157 os.write(PdfContentByte.escapeString(b)); 158 } 159 160 165 166 public String toString() { 167 return value; 168 } 169 170 172 177 178 public String getEncoding() { 179 return encoding; 180 } 181 182 public String toUnicodeString() { 183 if (encoding != null && encoding.length() != 0) 184 return value; 185 getBytes(); 186 if (bytes.length >= 2 && bytes[0] == (byte)254 && bytes[1] == (byte)255) 187 return PdfEncodings.convertToString(bytes, PdfObject.TEXT_UNICODE); 188 else 189 return PdfEncodings.convertToString(bytes, PdfObject.TEXT_PDFDOCENCODING); 190 } 191 192 void setObjNum(int objNum, int objGen) { 193 this.objNum = objNum; 194 this.objGen = objGen; 195 } 196 197 void decrypt(PdfReader reader) { 198 PdfEncryption decrypt = reader.getDecrypt(); 199 if (decrypt != null) { 200 originalValue = value; 201 decrypt.setHashKey(objNum, objGen); 202 bytes = PdfEncodings.convertToBytes(value, null); 203 bytes = decrypt.decryptByteArray(bytes); 204 value = PdfEncodings.convertToString(bytes, null); 205 } 206 } 207 208 public byte[] getBytes() { 209 if (bytes == null) { 210 if (encoding != null && encoding.equals(TEXT_UNICODE) && PdfEncodings.isPdfDocEncoding(value)) 211 bytes = PdfEncodings.convertToBytes(value, TEXT_PDFDOCENCODING); 212 else 213 bytes = PdfEncodings.convertToBytes(value, encoding); 214 } 215 return bytes; 216 } 217 218 public byte[] getOriginalBytes() { 219 if (originalValue == null) 220 return getBytes(); 221 return PdfEncodings.convertToBytes(originalValue, null); 222 } 223 224 public PdfString setHexWriting(boolean hexWriting) { 225 this.hexWriting = hexWriting; 226 return this; 227 } 228 229 public boolean isHexWriting() { 230 return hexWriting; 231 } 232 } | Popular Tags |