Spire.Doc for Java 12.3.1 已发布。本次更新带来多项新功能,如支持在在更新域时设置域属性以及支持判断文档是否存在数字签名。此外,该版本还优化了转换 Word 到 OFD 后结果文件的大小。详情请阅读以下内容。
问题修复:
- 优化了转换 Word 到 OFD 后结果文件的大小。
新功能:
- MergeImageFieldEventArgs 事件里面新增方法 setImageLink() 支持给邮件合并的图片添加超链接。
- 公开 getFieldOptions() 方法支持在在更新域时设置域属性。
- 新增 hasDigitalSignature() 方法支持判断文档是否存在数字签名。
- 新增 integrateFontTableTo 方法支持将源文档的 Fonttable 数据拷贝到目标文档。
- 新增 HtmlUrlLoadEvent 事件支持在加载 Html 文件时,对文件中的 url 的加载进行控制。
- 新增 setCustomFonts(InputStream[] fontStreamList) 方法支持通过流的方式设置自定义字体。
- 废弃 clearCustomFontsFolders() 方法,使用新增的clearCustomFonts()方法替代 。
- 废弃 setGlobalCustomFontsFolders(InputStream[] fontStreamList) 方法,使用新增的 setGlobalCustomFonts(InputStream[] fontStreamList) 替代。
- 废弃 clearGlobalCustomFontsFolders() 方法,使用新增的 clearGlobalCustomFonts() 方法替代。
Document document = new Document();
document.loadFromFile(inputFile);
String[] fieldNames = new String[]{"ImageFile"};
String[] fieldValues = new String[]{inputFile_img};
document.getMailMerge().MergeImageField = new MergeImageFieldEventHandler() {
@Override
public void invoke(Object sender, MergeImageFieldEventArgs args) {
mailMerge_MergeImageField(sender, args);
}
};
document.getMailMerge().execute(fieldNames, fieldValues);
document.saveToFile(outputFile, FileFormat.Docx);
private static void mailMerge_MergeImageField(Object sender, MergeImageFieldEventArgs field) {
String filePath = field.getImageFileName();
if (filePath != null && !"".equals(filePath)) {
try {
field.setImage(filePath);
field.setImageLink("https://www.baidu.com/");
} catch (Exception e) {
e.printStackTrace();
}
}
document.getFieldOptions().setCultureSource(FieldCultureSource.CurrentThread);
Document.hasDigitalSignature("filepath");
sourceDoc.integrateFontTableTo(Document destDoc);
public static void main(String[] args) {
Document document = new Document();
document.HtmlUrlLoadEvent = new MyDownloadEvent();
document.loadFromFile(inputFile, FileFormat.Html, XHTMLValidationType.None);
document.saveToFile(outputFile, FileFormat.PDF);
}
static class MyDownloadEvent extends HtmlUrlLoadHandler {
@Override
public void invoke(Object o, HtmlUrlLoadEventArgs htmlUrlLoadEventArgs) {
try {
byte[] bytes = downloadBytesFromURL(htmlUrlLoadEventArgs.getUrl());
htmlUrlLoadEventArgs.setDataBytes(bytes);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static byte[] downloadBytesFromURL(String urlString) throws Exception {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// 设置连接超时时间和读取超时时间
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
return outputStream.toByteArray();
} else {
throw new Exception("Failed to download content. Response code: " + responseCode);
}
}
document.setCustomFonts(InputStream[] fontStreamList);
document.clearCustomFonts();
Document.setGlobalCustomFonts(InputStream[] fontStreamList);
Document.clearGlobalCustomFonts();
获取 Spire.Doc for Java 12.3.1 请点击: