Java iText PdfDictionary编码问题?
创建PDF文件时,我会使用以下代码将一些信息附加到该文件中,以使其对我的程序可读:Java iText PdfDictionary编码问题?,java,pdf,character-encoding,itext,Java,Pdf,Character Encoding,Itext,创建PDF文件时,我会使用以下代码将一些信息附加到该文件中,以使其对我的程序可读: PdfDictionary dictionary = new PdfDictionary(); PdfObject object; PdfName index; ArrayList<String> content = getCompactData(document); for (int i = 0; i < c
PdfDictionary dictionary = new PdfDictionary();
PdfObject object;
PdfName index;
ArrayList<String> content = getCompactData(document);
for (int i = 0; i < content.size(); i++)
{
object = new PdfString(content.get(i));
index = new PdfName(Integer.toString(i+1));
dictionary.put(index, object);
}
writer.getExtraCatalog().putAll(dictionary);
PdfDictionary dictionary=new PdfDictionary();
PdfObject对象;
PdfName指数;
ArrayList内容=getCompactData(文档);
对于(int i=0;i
打开程序时,我使用以下代码提取数据:
PdfDictionary dictionary = reader.getCatalog();
PdfName index;
PdfObject line;
ArrayList<String> data = new ArrayList<String>();
for (int i = 1; i < dictionary.size()-2; i++)
{
index = new PdfName(Integer.toString(i));
line = dictionary.getAsString(index);
data.add(line.toString());
}
PdfDictionary dictionary=reader.getCatalog();
PdfName指数;
PDF对象线;
ArrayList数据=新的ArrayList();
对于(int i=1;i
除了一个小细节,一切都很好。由于某些原因,诸如šđćž之类的字符没有正确地传递给进程。一旦我试图提取数据,我的程序就会混乱,无法识别字母
几点注意:
是吗?您错误地使用了
PdfString
类。而不是
object = new PdfString(content.get(i));
使用
而不是
data.add(line.toString());
使用
一些背景资料:
您使用的构造函数尝试使用pdfdocencode:
/**
* Constructs a <CODE>PdfString</CODE>-object containing a string in the
* standard encoding <CODE>TEXT_PDFDOCENCODING</CODE>.
*
* @param value the content of the string
*/
public PdfString(String value)
您的字符šđćž
不存在于该编码中
另一个构造函数允许您选择UTF-16BE编码:
/**
* Constructs a <CODE>PdfString</CODE>-object containing a string in the
* specified encoding.
*
* @param value the content of the string
* @param encoding an encoding
*/
public PdfString(String value, String encoding)
对于字符提取toString
只返回内部表示,而ToInocDestring
关心编码:
/**
* Returns the Unicode <CODE>String</CODE> value of this
* <CODE>PdfString</CODE>-object.
*
* @return A <CODE>String</CODE>
*/
public String toUnicodeString()
您错误地使用了
PdfString
类。而不是
object = new PdfString(content.get(i));
使用
而不是
data.add(line.toString());
使用
一些背景资料:
您使用的构造函数尝试使用pdfdocencode:
/**
* Constructs a <CODE>PdfString</CODE>-object containing a string in the
* standard encoding <CODE>TEXT_PDFDOCENCODING</CODE>.
*
* @param value the content of the string
*/
public PdfString(String value)
您的字符šđćž
不存在于该编码中
另一个构造函数允许您选择UTF-16BE编码:
/**
* Constructs a <CODE>PdfString</CODE>-object containing a string in the
* specified encoding.
*
* @param value the content of the string
* @param encoding an encoding
*/
public PdfString(String value, String encoding)
对于字符提取toString
只返回内部表示,而ToInocDestring
关心编码:
/**
* Returns the Unicode <CODE>String</CODE> value of this
* <CODE>PdfString</CODE>-object.
*
* @return A <CODE>String</CODE>
*/
public String toUnicodeString()
谢谢,成功了!另外,您可能还想写一封信,将
行
从PdfObject
更改为PdfString
谢谢,这很有效!另外,您可能还希望编写将line
从PdfObject
更改为PdfString