- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 PDFBox 填写 PDF 表单,在保存之前将其压平。该表单具有用于文本和表单字段的自定义字体。当我在未安装此自定义字体的设备上打开输出文档(带有扁平化字段)时,普通文本的字体仍然正确,但扁平化字段的字体以后备(?)字体显示。在安装了此自定义字体的设备上,一切看起来都符合预期。
有没有办法在展平表单后强制对所有文本使用相同的自定义字体?
用于使用 PDFBox 填写 PDF 表单的代码(简化):
public class App
{
public static void main(String[] args) throws IOException {
String formTemplate = "src/main/resources/fonts.pdf";
String filledForm = "src/main/resources/fonts_out.pdf";
PDDocument pdfDocument = PDDocument.load(new File(formTemplate));
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
acroForm.getField("text").setValue("Same font in form text field (updated with PDFBox)");
acroForm.setNeedAppearances(true);
acroForm.refreshAppearances();
acroForm.flatten();
pdfDocument.save(filledForm);
pdfDocument.close();
}
}
预期:
系统未安装字体时的结果:
最佳答案
对 PDF 的一些观察(上述编码问题都不是 - 只是我的无知):
SansDroid 字体未嵌入到 PDF 中。通过将 F2
字体替换为新嵌入的 F5
字体即可解决此问题。
NeedAppearances
标志已设置,表示表单字段没有外观。任何读者都必须(重新)创建它们。在展平之前,PDFBox 不会自动完成此操作,因此我添加了这部分
为了不再发出有关丢失字体的警告,我完全删除了 F2 字体。
我通过预检运行原始 PDF,它给了我以下警告:“缺少所需的键/Subtype。路径:->Pages->Kids->[0]->Annots->[0]->AP->N "该键确实存在,但似乎表明表单字段的外观存在错误。如果我删除/N 字典,错误就会消失。流是“/Tx BMC EMC” - 也许缺少一些 EOL?但由于无论如何都会重新生成外观,因此错误随后就消失了。
使用以下代码将 DroidSans 字体嵌入到 PDF 中:
File pdf = new File("Fonts.pdf");
final PDDocument document = PDDocument.load(pdf);
FileInputStream fontFile = new FileInputStream(new File("DroidSans.ttf"));
PDFont font = PDType0Font.load(document, fontFile, false);
//1. embedd and register the font (Catalog dict)
PDAcroForm pDAcroForm = document.getDocumentCatalog().getAcroForm();
//create a new font resource
PDResources res = pDAcroForm.getDefaultResources();
if (res == null) res = new PDResources();
COSName fontName = res.add(font);
pDAcroForm.setDefaultResources(res);
//2. Now change the font of form field to the newly added font
PDField field = pDAcroForm.getField("text");
//field.setValue("Same font in form text field (updated with PDFBox)");
COSDictionary dict = field.getCOSObject();
COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
if (defaultAppearance != null){
String currentValue = dict.getString(COSName.DA);
//replace the font - this should be improved with a more general version
dict.setString(COSName.DA,currentValue.replace("F2", fontName.getName()));
//remove F2 completely
COSDictionary resources = res.getCOSObject();
for(Entry<COSName, COSBase> resource : resources.entrySet()) {
if(resource.getKey().equals(COSName.FONT)) {
COSObject fonts = (COSObject)resource.getValue();
COSDictionary fontDict = (COSDictionary)fonts.getObject();
COSName toBeRemoved=null;
for(Entry<COSName, COSBase> item : fontDict.entrySet()) {
if(item.getKey().getName().equals("F2")) {
toBeRemoved = item.getKey();
}
}
if(toBeRemoved!=null) {
fontDict.removeItem(toBeRemoved);
}
}
}
if(pDAcroForm.getNeedAppearances()) {
pDAcroForm.refreshAppearances();
pDAcroForm.setNeedAppearances(false);
}
//Flatten the document
pDAcroForm.flatten();
//Save the document
document.save("Form-Test-Result.pdf");
document.close();
请注意,上面的代码是相当静态的 - 搜索和替换名为 F2
的字体仅适用于提供的 PDF,在其他情况下则无效。您必须为此实现一个更通用的解决方案......
关于java - 使用 PDFBox 嵌入字体以扁平化 PDF 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53968324/
我有一个 TeamCity (9.0.2) 构建配置,其中包含以下工件路径模式: App\Agent\**\bin\%env.Configuration%\** => Deployment\Agent
我在 src 目录中有一个具有嵌套文件结构的项目。到目前为止,我一直在使用 babel --out-dir lib src 。最近我想到了一个想法,让用户直接使用单独的文件,所以基本上我想改造这个结构
我有一个数据库表,其中三列对于这个问题至关重要: 组 ID,将行分组在一起 开始日期 结束日期 我想从此表中创建一个 View ,以便将具有相同分组 ID 的重叠日期间隔展平。 不重叠的日期间隔不得展
我有一个很大的 pandas 数据框,有 8 列和几个 NaN 值: 0 1 2 3 4 5 6 7 8 1 Google, Inc. (Date 11/07/20
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我想将 simd_float4x4 或 simd_float3x3 矩阵展平为单个浮点元素数组。 对于常规数组我会使用 let arr = [[1,2,3],[4,5,6],[7,8,9]] prin
我正在寻找一种 Pythonic 方式来获取 OrderedDict 的 .values 的并集,在 折叠组成值的列表之后。我希望结果是一维列表,根据自定义顺序排序。 我在做什么: from coll
这个问题在这里已经有了答案: How do I make a flat list out of a list of lists? (36 个答案) 关闭 9 年前。 所以我目前正在使用 mongoe
我有一个像这样的数组。 [{"title"=>"ga:browser=Internet Explorer", "dimensions"=>[{:browser=>"Internet Explorer"
我正在尝试使用 Tabulator 创建工单列表,数据通过 AJAX url 从工单系统导入为 JSON,如下所示。 { "results": [ {
我有两个函数,每个函数都返回 CompletebleFuture实例,我想 or它们变成一个有序且可短路的 future 。 public CompletableFuture doA(); publi
我需要在 mysql 中用逗号分隔值将非主键列展平为一行。让我们举一个简单的例子。有一个名为 stud_sub 的表,其中 stud_id 和 sub_id 作为复合主键。现在我想要一个表,其中 st
这个问题在这里已经有了答案: Comprehension on a nested iterables? (13 个答案) 关闭 4 个月前。 我最近在寻找一种将嵌套的 Python 列表展平的方法,
我在 C# 中有一个 json 对象(表示为 Newtonsoft.Json.Linq.JObject 对象),我需要将它扁平化为字典。让我用一个例子来说明我的意思: { "name": "t
有人可以帮我进行以下转换吗? 这里是输入的xml: My book 200 big Smith Wallace
在下面的代码中,x 是 Future[Future[Int]] 类型,我需要将其展平为 Future[Int] .这怎么可能? def times(a:Int, b:Int) = Future
我对 R 还很陌生,我正在尝试使用 aggregate对数据帧、每个主题和数据集中的每个指标执行一些时间序列整形。这很好用,但我发现结果不是很容易使用的格式。我希望能够将结果转换回与原始数据帧相同的格
我有一个项目,我需要填写预制的 PDF,我想到的最合乎逻辑的解决方案是将预制的 PDF 制作成 PDF 表单,以便输入值应该放在标签中,然后我可以查看 PDF 中的表单标签,并将它们与值字典对齐。 我
首先,我对 Azure 还很陌生。 我已使用 Web 角色成功将 ASP.NET MVC 3 Web 应用程序部署到 Azure。该应用程序使用 Entity Framework 和 SQL Azur
我试图将 XML 层次结构展平为文本节点,其中输出标记名称是每个文本节点路径上的连字符连接标记。我对 http://www.chilkatsoft.com/xml-samples/bookstore.
我是一名优秀的程序员,十分优秀!