我有以下用于 pdf 修改的 iText 5 示例,
public static void modifyPdfForRequestMap
(
HashMap<String, String> fieldValuesMap,
String srcPdfFile,
String outFile,
boolean formFlatten,
boolean xfaForm,
boolean fieldManipulateFlag
)
{
PdfReader reader = null;
PdfStamper stamper= null;
HashMap<String, String> modifiedFieldNamesMap = null;
if(fieldValuesMap != null)
{
try
{
reader = new PdfReader(srcPdfFile);
if(fieldManipulateFlag)
{
AcroFields fields = reader.getAcroFields();
String[] fieldNames = null;
if(fields != null)
{
Map<String, Item> formFields = fields.getFields();
if(formFields != null)
{
Set<String> fldNames = formFields.keySet();
fieldNames = new String[fldNames.size()];
int i =0;
for (String fldName : fldNames)
{
fieldNames[i]=(fldName);
i++;
}
}
}
if(fieldManipulateFlag)
{
modifiedFieldNamesMap = new HashMap<String, String>();
if(fieldValuesMap != null && !fieldValuesMap.isEmpty()
&& fieldNames != null && fieldNames.length > 0)
{
Iterator<String> keys = fieldValuesMap.keySet().iterator();
while (keys.hasNext())
{
String key = (String) keys.next();
String value = (String)fieldValuesMap.get(key);
int fieldsCount = fieldNames.length;
for(int i=0; i <fieldsCount; i++)
{
String fieldName = fieldNames[i];
if(fieldName != null
&& fieldName.indexOf("."+key+"[") != -1)
{
modifiedFieldNamesMap.put(fieldName, value);
}
else if(fieldName != null && fieldName.equals(key))
{
modifiedFieldNamesMap.put(key, value);
}
}
}
}
}
else
{
modifiedFieldNamesMap = fieldValuesMap;
}
}
else
{
modifiedFieldNamesMap = fieldValuesMap;
}
DeleteFile(outFile);
stamper = new PdfStamper(reader, new FileOutputStream(outFile));
if(stamper != null )
{
AcroFields form = stamper.getAcroFields();
XfaForm xfa = form.getXfa();
if(xfa != null && xfa.isXfaPresent() && !xfaForm)
{
form.removeXfa();
}
if(form != null)
{
HashMap<String, Item> fields = (HashMap<String, Item>) form.getFields();
Iterator<String> iter = modifiedFieldNamesMap.keySet().iterator();
while (iter.hasNext())
{
String fieldName = (String) iter.next();
String fieldValue = modifiedFieldNamesMap.get(fieldName);
int fieldType = form.getFieldType(fieldName);
if(fields != null && fields.containsKey(fieldName) && fieldValue != null)
{
if(fieldType == AcroFields.FIELD_TYPE_LIST && fieldValue.indexOf(",") != -1)
{
form.setListOption(fieldName, fieldValue.split(","),fieldValue.split(","));
}
else
{
form.setField(fieldName, fieldValue);
}
}
}
stamper.setFormFlattening(formFlatten);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (DocumentException e)
{
e.printStackTrace();
}
finally
{
try
{
if(stamper != null)
{
stamper.close();
}
if(reader != null)
{
reader.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
else if(formFlatten)
{
try
{
try
{
reader = new PdfReader(srcPdfFile);
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
stamper = new PdfStamper(reader, new FileOutputStream(outFile));
}
catch (Exception e)
{
e.printStackTrace();
}
stamper.setFormFlattening(formFlatten);
}
finally
{
try
{
if(stamper != null)
{
stamper.close();
}
if(reader != null)
{
reader.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
modifiedFieldNamesMap = null;
}
我正在尝试转换为 iText 7,但我很难转换函数的最后一部分,其中列表类型字段值和简单的 Acrofield 值被插入到文档中。
:------------------------------------------------ --------------
PdfFormField field = form.getField( fieldName );
int fieldType = form.getFieldType (fieldName);
if( fields != null && fields.containsKey(fieldName) && fieldValue != null )
{
if(fieldValue.indexOf(",") != -1 && fieldType == AcroFields.FIELD_TYPE_LIST )
{
form.setListOption(fieldName, fieldValue.split(","),fieldValue.split(","));
}
else
{
form.setField(fieldName, fieldValue);
}
}
}
提前感谢您的帮助。