这是一个关于xfa的问题。着急用,看看那位了解这方面知识的大侠能够帮助解答一下。文件“zhangxu_org2.pdf”是用adobe designer生成的pdf文件。文件“zhangxu_org2.xml”是符合xfa规范要求的纯文本文件,全部内容如下
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<OrgPage>
<SubPage2>
<textfild1>zhangxu </textfild1>
<textfild1>Tom </textfild1>
<textfild1>Marry </textfild1>
<textfild1>Mala </textfild1>
</SubPage2>
<pageAllRows>
<oneRow>
<textfild1>Company 1 </textfild1>
<textfild1>Company 2 </textfild1>
<textfild1>company 3 </textfild1>
</oneRow>
<oneRow>
<textfild1>Company 4 </textfild1>
<textfild1>Company 5 </textfild1>
<textfild1>company 6 </textfild1>
</oneRow>
<oneRow>
<textfild1>Company 7 </textfild1>
<textfild1>Company 8 </textfild1>
<textfild1>company 9 </textfild1>
</oneRow>
</pageAllRows>
</OrgPage>
</form1>
</xfa:data>
</xfa:datasets>在使用下面的代码操作后,能够把数据正确导入pdf文件里,生成的"filled_xfa_zhangxu_org2.pdf"并能正常打开。但是当我把 “zhangxu”从英文修改为中文“张某”后。使用下面的代码操作后,生成的"filled_xfa_zhangxu_org2.pdf"就不能打开了。报“xml解析错误:格式错误(无效的标记)(错误代码4),文件的第182行的第24列”错误。
请问,下面代码如何修改可以防止这个错误的发生。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;public class FillDynamicXfa {
public static final String RESOURCE_PDF = "zhangxu_org2.pdf";
public static final String RESOURCE_DATA = "zhangxu_org2.xml";
public static final String RESULT = "filled_xfa_zhangxu_org2.pdf";public static void main(String[] args) {
try {
PdfReader reader = new PdfReader(RESOURCE_PDF);
File file = new File(RESOURCE_DATA);
byte[] data = new byte[(int)file.length()];
FileInputStream is = new FileInputStream(file);
int offset = 0;
        int numRead = 0;
        int datalength = data.length;
        while (offset < datalength
              && (numRead=is.read(data, offset, datalength - offset)) >= 0) {
            offset += numRead;
        }
PdfDictionary root = reader.getCatalog();
PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
PdfArray xfa = acroform.getAsArray(PdfName.XFA);
for (int i = 0; i < xfa.size(); i += 2) {
if ("datasets".equals(xfa.getAsString(i).toString())) {
PRStream s = (PRStream)xfa.getAsStream(i + 1);
s.setData(data);
}
}
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(RESULT));
stamper.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}下面是上面例子的另外一个一模一样的版本,如果哪位大侠需要运行可以从这个链接下载所需的pdf文件,和xml文件和源代码。
http://www.1t3xt.info/examples/browse/?page=example&id=433
需要的jar包是iText-2.1.7.jar
http://www.lowagie.com/iText/download.html