我正在使用Apache PDFBox读取可填写的PDF表单并根据一些数据填写字段.我使用下面的代码(根据其他SO答案的建议)获取默认外观字符串并更改它(如下所示,如果字段名称为"Field1",我将字体大小从10更改为12.
请注意,下面的代码可以通过填充方法参数的特定"名称"字段中方法参数中传递的特定"值"来正常工作.
谢谢 !
public static void setField(String name, String value ) throws IOException {
PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField( name );
COSDictionary dict = ((PDField)field).getDictionary();
COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
if (defaultAppearance != null)
{
dict.setString(COSName.DA, "/Helv 10 Tf 0 g");
if(name.equalsIgnoreCase("Field1"))
{
dict.setString(COSName.DA, "/Helv 12 Tf 0 g");
}
}
if(field instanceof PDTextbox)
{
field= new PDTextbox(acroForm, dict);
((PDField)field).setValue(value);
}
Run Code Online (Sandbox Code Playgroud)
根据mkl的回答,为了在同一个PDF中使用两种字体,我使用了以下方法:我无法获得默认字体和自定义字体一起工作,因此我在资源中添加了两种字体并使用它们.
public List<String> …Run Code Online (Sandbox Code Playgroud) 我有一个公共共享库(即在 Websphere 应用程序服务器中设置为共享库)。
该 jar 的文件夹结构是:
UtilityJAR
----src
-com
-test
-TestClass.java
---- META-INF
-resources
-template.xhtml
-css
-style.css
Run Code Online (Sandbox Code Playgroud)
在我的 web 项目中,我有一个名为 User.xhtml 的模板客户端文件,它使用来自上述共享库的模板文件
ui:composition template="/template.xhtml"
Run Code Online (Sandbox Code Playgroud)
当我在 Web 应用程序的 WEB-INF/lib 文件夹中有上述 jar 文件时,该应用程序可以正常工作,没有任何问题(可以识别 template.xhtml)。当我从这个应用程序的 Lib 文件夹中删除 jar 并将它作为共享库放在 Websphere 中时(因为我需要来自 4 个以上应用程序的这个 jar 文件,我不想在所有 4 个应用程序中复制这个 jar),我收到以下错误消息。
[9/24/14 14:09:17:936 EDT] 00000113 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0014E: Uncaught service() exception root cause Faces Servlet: java.io.FileNotFoundException: /template.xhtml Not Found in ExternalContext as a Resource
Run Code Online (Sandbox Code Playgroud)
Utility jar 中有 faces-config 并且有 @ManagedBean 注释,当 jar 位于应用程序的 WEB-INF/lib …