Spire.Doc for Java 12.1.0已发布。本次更新移除了对Spire.Pdf.jar的依赖,且将应用授权的方法更改为com.spire.doc.license.LicenseProvider.setLicenseKey(key)。此外,还新增了一系列新功能,如新增添加图片水印的方法。详情请阅读以下内容。
调整:
- 移除了对Spire.Pdf.jar的依赖。
- 将应用授权方法更改为com.spire.doc.license.LicenseProvider.setLicenseKey(key)。
新功能:
- 回收以下方法、类和接口。
- 声明废弃了TableCell类中的getWidth() 和 setWidth() 方法,并使用getCellWidth()和setCellWidth()方法替代。
- 变更以下命名空间。
- 变更继承关系:将ShapeGroup implements ShapeObject 变更为 ShapeGroup implements ShapeBase。
- 支持在销毁Document时,同时销毁自定义字体的相关数据。
- 变更以下枚举类。
- 变更以下方法。
- 新增添加图片水印的方法。
- Shape公开了getFill()方法用于操作图形的填充:使用getFill().setOn(false)替代setFillColor(null)方法。
- 新增支持添加图表。
- 提供了页模型Spire.Doc.Pages以获取页面的内容。
- 新增支持添加SVG图形。
Document构造中设置newEngine不再起作用,内部默认采用新引擎
HeaderType枚举
GroupedShapeCollection类
ShapeObjectTextCollection类
MailMergeData接口
EnumInterface接口
public PictureWaterMark(InputStream inputeStream,boolean washout)
public PictureWaterMark(String filename,boolean washout)
Field类中downloadImage方法
IDocOleObject接口
PointsConverter类
com.spire.license.LicenseProvider -> com.spire.doc.License.LicenseProvider
// 设置自定义字体
Document.setCustomFontsFolders(string filePath);
// 处理自定义字体
Document.clearCustomFontsFolders();
// 清除缓存中占用内存的系统字体缓存
Document.clearSystemFontCache();
Example code:
Document doc = new Document();
doc.loadFromFile("inputFile.docx");
doc.setCustomFontsFolders(@"d:\Fonts");
doc.saveToFile("output.pdf", FileFormat.PDF);
doc.close();
doc.dispose();
com.spire.doc.FileFormat.WPS -> com.spire.doc.FileFormat.Wps
com.spire.doc.FileFormat.WPT -> com.spire.doc.FileFormat.Wpt
ComparisonLevel -> TextDiffMode
ComparisonLevel getLevel() -> getTextCompareLevel()
setLevel(ComparisonLevel value) -> setTextCompareLevel(TextDiffMode)
IsPasswordProtect() -> isEncrypted()
getFillEfects() -> getFillEffects()
File imageFile = new File("data/E-iceblue.png");
BufferedImage bufferedImage = ImageIO.read(imageFile);
// 通过输入 BufferedImage 创建 PictureWatermark 类的新实例,并设置水印图像的缩放因子
PictureWatermark picture = new PictureWatermark(bufferedImage,false);
// 或者使用另一种创建 PictureWatermark 的方法
// PictureWatermark picture = new PictureWatermark();
// picture.setPicture(bufferedImage);
// picture.isWashout(false);
// 设置水印图片的缩放比例
picture.setScaling(250);
// 设置要应用于文档的水印
document.setWatermark(picture);
// 创建一个新的 Document 实例
Document document = new Document();
// 向文档添加一个节
Section section = document.addSection();
// 将段落添加到该节并向其附加文本
section.addParagraph().appendText("Line chart.");
// 添加一个新段落到该节
Paragraph newPara = section.addParagraph();
// 将折线图形状附加到指定宽度和高度的段落
ShapeObject shape = newPara.appendChart(ChartType.Line, 500, 300);
// 从形状中获取图表对象
Chart chart = shape.getChart();
// 获取图表的标题
ChartTitle title = chart.getTitle();
// 设置图表标题的文本
title.setText("My Chart");
// 清除图表中任何现有的系列
ChartSeriesCollection seriesColl = chart.getSeries();
seriesColl.clear();
// 定义类别(X 轴值)
String[] categories = { "C1", "C2", "C3", "C4", "C5", "C6" };
// 将两个具有指定类别和 Y 轴值的系列添加到图表中
seriesColl.add("AW Series 1", categories, new double[] { 1, 2, 2.5, 4, 5, 6 });
seriesColl.add("AW Series 2", categories, new double[] { 2, 3, 3.5, 6, 6.5, 7 });
// 将文档保存为Docx格式的文件
document.saveToFile("AppendLineChart.docx", FileFormat.Docx_2016);
// 释放文档资源
document.dispose();
// 创建一个新的 Document 实例
Document doc = new Document();
// 从指定文件加载文档
doc.loadFromFile(inputFile);
// 使用加载的文档创建一个FixedLayoutDocument对象
FixedLayoutDocument layoutDoc = new FixedLayoutDocument(doc);
// 创建一个StringBuilder来存储提取的文本
StringBuilder stringBuilder = new StringBuilder();
// 获取第一页的第一行并将其附加到 StringBuilder
FixedLayoutLine line = layoutDoc.getPages().get(0).getColumns().get(0).getLines().get(0);
stringBuilder.append("Line: " + line.getText() + "\r\n");
// 检索与该行关联的原始段落并将其文本附加到 StringBuilder
Paragraph para = line.getParagraph();
stringBuilder.append("Paragraph text: " + para.getText() + "\r\n");
// 检索第一页上的所有文本,包括页眉和页脚,并将其附加到 StringBuilder
String pageText = layoutDoc.getPages().get(0).getText();
stringBuilder.append(pageText + "\r\n");
// 遍历文档中的每一页并打印每页的行数
for (Object obj : layoutDoc.getPages()) {
FixedLayoutPage page = (FixedLayoutPage) obj;
LayoutCollection<LayoutElement> lines = page.getChildEntities(LayoutElementType.Line, true);
stringBuilder.append("Page " + page.getPageIndex() + " has " + lines.getCount() + " lines." + "\r\n");
}
// 对第一段的布局实体执行反向查找并将它们附加到 StringBuilder
stringBuilder.append("\r\n");
stringBuilder.append("The lines of the first paragraph:" + "\r\n");
for (Object object : layoutDoc.getLayoutEntitiesOfNode(((Section) doc.getFirstChild()).getBody().getParagraphs().get(0))) {
FixedLayoutLine paragraphLine = (FixedLayoutLine) object;
stringBuilder.append(paragraphLine.getText().trim() + "\r\n");
stringBuilder.append(paragraphLine.getRectangle().toString() + "\r\n");
stringBuilder.append("");
}
// 将提取的文本写入文件
FileWriter fileWriter = new FileWriter(new File(outputFile));
fileWriter.write(stringBuilder.toString());
fileWriter.flush();
fileWriter.close();
// 释放文档资源
doc.close();
doc.dispose();
// 创建一个新的文档对象
Document document = new Document();
// 在文档中添加一个新的节
Section section = document.addSection();
// 添加一个新的段落到该节
Paragraph paragraph = section.addParagraph();
// 将图片 (SVG) 附加到段落中
paragraph.appendPicture(inputSvg);
// 将文档保存到指定的输出文件
document.saveToFile(outputFile, FileFormat.Docx_2013);
// 关闭文档
document.dispose();
获取Spire.Doc for Java 12.1.0请点击: