gpt4 book ai didi

pdf - 如何使用iText从PDF文件中获取所有字段和值?

转载 作者:行者123 更新时间:2023-12-05 01:16:16 26 4
gpt4 key购买 nike

使用下面的代码,我试图获取所有字段及其值,但是只返回字段值。我需要做什么才能同时获得两者?

   package PrintFiled;
/*

Usage: java TestDocument <pdf file>

*/

import java.io.File;
import java.io.FileOutputStream;
import java.util.Collection;

import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;

import org.pdfbox.pdmodel.interactive.form.PDField;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.RadioCheckField;
import com.lowagie.text.pdf.PdfSignature;

import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.AcroFields.*;

public class TestDocument
{
public static void main(String[] args) throws Exception
{

// get the destination

String location = "test/";

if (location != null && !location.endsWith(File.separator))

{

location = location + File.separator;

}



PdfReader reader = new PdfReader(location + "acc.pdf");

String name = "Output.pdf";

PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(location + name));

AcroFields form = stamp.getAcroFields();


String last = "subform[0].LastName[0]";

String first = "subform[0].FirstName[0]";

String ProcessDate="subform[0].ProcessDate[0]";

form.setField(last, "HRISTOV");

form.setField(first, "NEDKO");
form.setField(ProcessDate, "Process");

System.out.println("last Name :"+last.toString());


Map val =new HashMap();
//val=form.getFieldCache();
//System.out.println("Value :"+val);
Iterator it = val.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
System.out.println("Key :"+pairs.getKey() + " = "+"Value :" + pairs.getValue());
}

Collection fieldlist = form.getFields().keySet();
// for (Iterator i = val.iterator(); i.hasNext(); )
for (Iterator i = fieldlist.iterator(); i.hasNext(); )
{

System.out.println(i.next().toString());

System.out.println("Value :"+val);
}
/*List fields = form.getFields();
Iterator fieldsIter = fields.iterator();

System.out.println(new Integer(fields.size()).toString()
+ " top-level fields were found on the form");

while (fieldsIter.hasNext()) {
PDField field = (PDField) fieldsIter.next();
// processField(field, "|--", field.getPartialName());
System.out.println("Field :"+fieldsIter);
}
*/
System.out.println("First Name:"+form.getField(first));
System.out.println("LastName :"+form.getField(last));
System.out.println("ProcessDate :"+form.getField(ProcessDate));

// close pdf stamper
stamp.setFormFlattening(true);
stamp.close();
reader.close();
}
}

最佳答案

要使用iText获取所有字段及其值:

// you only need a PdfStamper if you're going to change the existing PDF.
PdfReader reader = new PdfReader( pdfPath );

AcroFields fields = reader.getAcroFields();

Set<String> fldNames = fields.getFields().keySet();

for (String fldName : fldNames) {
System.out.println( fldName + ": " + fields.getField( fldName ) );
}


除此之外,如果我们能看到给您带来麻烦的PDF,将对您有帮助。

关于pdf - 如何使用iText从PDF文件中获取所有字段和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6056401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com