我正在使用此代码来获取 PDF 的 AcroField 的字体大小。
但是名为“first name last name”的 AcroField 的字体大小为 0(尽管其实际字体为 32.3)。
其他字段的字体大小正在准确。请帮助我获得准确的字体大小。
我的代码是...
final AcroFields.Item item = acroFields.getFieldItem(fieldName);
ArrayList list =null;
if(item!=null)
list = item.merged;
if (list != null)
{
for (final Iterator it1 = list.iterator(); it1.hasNext();)
{
final PdfDictionary itemDict = (PdfDictionary) it1.next();
final PdfObject da = itemDict.get(PdfName.DA);
System.out.println(da.toString()); //font size is printing out to be 0;
}
}
新代码是
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfEncodings;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfStream;
public class MyTest {
public static void main(String[] args) {
String pdfName = "Crunch-Business_card_NRW_edits.pdf";
PdfStamper stamper = null;
FileOutputStream fout = null;
try{
PdfReader reader = new PdfReader(pdfName);
fout = new FileOutputStream("output.pdf");
stamper = new PdfStamper(reader, fout);
AcroFields acroFields = stamper.getAcroFields();
Map fieldMap = acroFields.getFields();
Set keys = fieldMap.keySet();
for (Iterator it = keys.iterator(); it.hasNext();)
{
String fieldName = (String) it.next();
acroFields.setField(fieldName,acroFields.getField(fieldName));
final AcroFields.Item item = acroFields.getFieldItem(fieldName);
final ArrayList list = item.merged;
if (list != null) {
for (final Iterator it1 = list.iterator(); it1.hasNext();) {
final PdfDictionary itemDict = (PdfDictionary) it1.next();
PdfDictionary appearanceDict = itemDict.getAsDict(PdfName.AP);
PdfStream normalAppearance = appearanceDict.getAsStream(PdfName.N);
System.out.println("normalAppearance======"+normalAppearance);// normalAppearance is coming null.
byte[] streamBytes = PdfReader.getStreamBytes((PRStream) normalAppearance);
System.out.println(PdfEncodings.convertToString(streamBytes, null));
}
}
}
stamper.setFreeTextFlattening(false);
stamper.setFormFlattening(false);
stamper.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
PDF 的链接是http://www.mediafire.com/view/?tpjql3ipn3xqpbo。
提前致谢。