我正在使用这段代码来获得AcroField的字体大小。
但是名为"first name last name“的AcroField的字体大小是0(尽管它的实际字体是32.3)。
其他字段的字体大小即将到来,accurate.Kindly帮助我获得准确的字体大小。
我的代码是...
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。
提前谢谢。
发布于 2013-03-12 00:46:29
本质上:
0表示“自动调整大小”:你必须计算一个合适的大小。
详细说明:
让我们看一下文档中的字段定义:
52 0 obj
<<
/Ff 41943042
/F 4
/Type/Annot
/RV(<?xml version="1.0"?>
<body xfa:APIVersion="Acroform:2.7.0.0" xfa:spec="2.1"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<p dir="ltr"
style="margin-top:0pt;margin-bottom:0pt;text-valign:middle;
font-family:'Alternate Gothic LT';font-size:30pt;
color:#ffffff">first name last name</p>
</body>)
/Subtype/Widget
/DV(first name last name)
/T(name)
/V(first name last name)
/DS(font: 'Alternate Gothic LT',sans-serif 12.0pt; text-align:left; color:#FFFFFF )
/AP<</N 7 0 R>>
/P 21 0 R
/MK<<>>
/FT/Tx
/Rect[36.8297 87.7383 250.89 129.353]
/DA(/AlternateGothicLT-No3 0 Tf 1 1 1 rg)
>>
endobj在7 0中的外观流的内容中:
q Q /Tx
BMC
q
0 0 214.06 41.61 re W n
q
BT
1 0 0 1 2 7.14 Tm
/AlternateGothicLT-No3 32.31 Tf
1 1 1 rg
(first name last name)Tj
0 g
ET
Q
Q
EMC 因此,就像您通过iText阅读一样,DA (默认外观)字符串将字体大小设置为0。
根据PDF规范ISO 32000-1,第435页,这意味着:
默认外观字符串(DA)包含建立图形状态参数所需的任何图形状态或文本状态运算符,例如文本大小和颜色,以显示字段的可变文本。只有文本对象中允许的操作符才能出现在这个字符串中(参见图9)。字符串至少应包括Tf (文本字体)运算符及其两个操作数font和size。指定的字体值应与默认资源字典(从交互式表单字典的DR条目引用;参见表218)的字体条目中的资源名称相匹配。大小的零值表示字体应自动调整大小:其大小应作为注释矩形高度的函数来计算。
,因此必须计算大小以填充可用空间,而不是更多。
在外观流中,您可以看到外观的最后创建者认为32.31pt可以完成这项工作。
编辑
您可以像这样提取正常外观流的字节数:
PdfDictionary appearanceDict = itemDict.getAsDict(PdfName.AP);
PdfStream normalAppearance = appearanceDict.getAsStream(PdfName.N);
byte[] streamBytes = PdfReader.getStreamBytes((PRStream) normalAppearance);
System.out.println(PdfEncodings.convertToString(streamBytes, null));发布于 2013-03-23 18:42:45
我已经尝试了@mkl提供的解决方案,并得到了预期的结果。这是@mkl解释的解决方案。
PdfDictionary appearanceDict = itemDict.getAsDict(PdfName.AP);
PdfStream normalAppearance = appearanceDict.getAsStream(PdfName.N);
byte[] streamBytes = PdfReader.getStreamBytes((PRStream) normalAppearance);
System.out.println(PdfEncodings.convertToString(streamBytes, null));https://stackoverflow.com/questions/15336149
复制相似问题