栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么pdf仅包含一个字段约为500Kb

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么pdf仅包含一个字段约为500Kb

在acroform默认资源中有一个嵌入的“ Arial”字体,请参见Root/AcroForm/DR/Font/Arial/FontDescriptor/FontFile2。

您或创建PDF的人无缘无故地添加了它。该字体未使用/未引用。对于acroform默认资源,您可以检查每个字段的/ DA条目(默认外观)是否包含字体名称。

当您以某种方式删除字段时,还从acroForm默认资源中删除了字体。(您并未写出删除方式)

这是一些执行此操作的代码(大多数情况下都缺少空检查):

    PDAcroForm acroForm = doc.getdocumentCatalog().getAcroForm();    PDResources defaultResources = acroForm.getDefaultResources();    COSDictionary fontDict = (COSDictionary) defaultResources.getCOSObject().getDictionaryObject(COSName.FONT);    List<String> defaultAppearances = new ArrayList<>();    List<COSName> fontDeletionList = new ArrayList<>();    for (PDField field : acroForm.getFieldTree())    {        if (field instanceof PDVariableText)        { PDVariableText vtField = (PDVariableText) field; defaultAppearances.add(vtField.getDefaultAppearance());        }    }    for (COSName fontName : defaultResources.getFontNames())    {        if (COSName.HELV.equals(fontName) || COSName.ZA_DB.equals(fontName))        { // Adobe default, always keep continue;        }        boolean found = false;        for (String da : defaultAppearances)        { if (da != null && da.contains("/" + fontName.getName())) {     found = true;     break; }        }        System.out.println(fontName + ": " + found);        if (!found)        { fontDeletionList.add(fontName);        }    }    System.out.println("deletion list: " + fontDeletionList);    for (COSName fontName : fontDeletionList)    {        fontDict.removeItem(fontName);    }

生成的文件现在为5KB。

我还没有检查注释。其中一些还具有/ DA字符串,但不清楚在重建缺失的外观流时是否使用acroform默认资源字体。

更新:这是一些其他代码,用Helv替换Arial:

for (PDField field : acroForm.getFieldTree()){    if (field instanceof PDVariableText)    {        PDVariableText vtField = (PDVariableText) field;        String defaultAppearance = vtField.getDefaultAppearance();        if (defaultAppearance.startsWith("/Arial"))        { vtField.setDefaultAppearance("/Helv " + defaultAppearance.substring(7)); vtField.getWidgets().get(0).setAppearance(null); // this removes the font usage vtField.setValue(vtField.getValueAsString());        }        defaultAppearances.add(vtField.getDefaultAppearance());    }}

请注意,这可能不是一个好主意,因为标准的14种字体只有有限的字符。尝试

vtField.setValue("Ayşe");

然后您会得到一个例外。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569757.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号