我创建了一个PDF表单,然后使用PDFStamper和iText 2.1.4填写字段。当字段中包含国际字符时,生成的PDF中的字符看起来很奇怪。我检查了输出文件,国际字符在那里看起来很好,所以我假设它是某种字体问题。
我已经附上了填写的PDF证书的屏幕截图,上面有滑稽的字符(正确的名字是GÓMEZORTÍZ)。
这是我正在使用的Java代码:
import java.io.*;
import java.util.*;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
class Field
{
public String key;
public String value;
public Field(String key, String value)
{
this.key = key;
this.value = value;
}
}
public class PdfCertificate
{
public static void main(String[] args)
throws IOException, DocumentException
{
String input_file = args[0];
String field_file = args[1];
String output_file = args[2];
FileInputStream fstream = new FileInputStream(field_file);
BufferedReader bstream = new BufferedReader(new InputStreamReader(fstream));
String line;
Vector vector = new Vector();
while ((line = bstream.readLine()) != null)
{
int comma = line.indexOf(',');
String key = line.substring(0, comma).trim();
String value = line.substring(comma + 1, line.length()).trim();
System.out.println(key + " => " + value);
Field field = new Field(key, value);
vector.add(field);
}
dstream.close();
PdfReader reader = new PdfReader(input_file);
FileOutputStream output_stream = new FileOutputStream(output_file);
PdfStamper stamper = new PdfStamper(reader, output_stream);
AcroFields form = stamper.getAcroFields();
for (int i = 0; i < vector.size(); i++)
{
Field field = (Field) vector.elementAt(i);
form.setField(field.key, field.value);
}
stamper.setFormFlattening(true);
stamper.close();
}
}
干杯, 格雷厄姆