Spire.Office for Java 9.1.4已发布。在该版本中,Spire.PDF for Java 提高了绘制水印的效率;Spire.Doc for Java 增加了添加图像水印的新方法;Spire.Presentation for Java 提升了从PowerPoint 到 SVG格式的转换速度。此外,一些已知问题也在该版本中得到修复。详情请阅读以下内容。
获取 Spire.Office for Java 9.1.4请点击:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
Spire.PDF for Java
新功能:
- 提高绘制水印的效率。
- 新增PdfTextReplacer接口来实现替换文本的功能。
- 新增PdfImageHelper接口来实现删除图片、提取图片、替换图片和压缩图片的功能。 关键代码如下:
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("sample.pdf");
PdfPageBase page = pdf.getPages().get(0);
PdfTextReplacer replacer = new PdfTextReplacer(page);
PdfTextReplaceOptions options= new PdfTextReplaceOptions();
options.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord));
replacer.replaceText("www.google.com", "1234567");
pdf.saveToFile(outputFile);
PdfImageHelper imageHelper = new PdfImageHelper();
PdfImageInfo[] imageInfoCollection= imageHelper.getImagesInfo(page);
Delete image:
imageHelper.deleteImage(imageInfoCollection[0]);
Extract image:
int index = 0;
for (com.spire.pdf.utilities.PdfImageInfo img : imageInfoCollection) {
BufferedImage image = img.getImage();
File output = new File(outputFile_Img + String.format("img_%d.png", index));
ImageIO.write(image, "PNG", output);
index++;
}
PdfImage image = PdfImage.fromFile("ImgFiles/E-iceblue logo.png");
imageHelper.replaceImage(imageInfoCollection[i], image);
Compress image:
for (PdfPageBase page : (Iterable<PdfPageBase>)doc.getPages())
{
if (page != null)
{
if (imageHelper.getImagesInfo(page) != null)
{
for (com.spire.pdf.utilities.PdfImageInfo info : imageHelper.getImagesInfo(page))
{
info.tryCompressImage();
}
}
}
}
问题修复:
- 修复了保存文档时程序抛出java.lang.StringIndexOutOfBoundsException异常的问题。
- 修复了多线程下批量对多个PDF文档执行取代文本的操作后进行保存但没有生成结果文档的问题。
Spire.XLS for Java
问题修复:
- 修复了用WPS工具进行打印预览被保存出的XLSX文档时页边距不正确的问题。
- 修复了获取没有填充色的单元格的填充色值为(0,0,0)的问题。
- 修复了转换HTML文档到XLSX文档时单元格的边框样式丢失的问题。
Spire.Doc for Java
调整:
- 移除了对Spire.Pdf.jar的依赖。
- 将应用授权方法更改为com.spire.doc.license.LicenseProvider.setLicenseKey(key)。
- 将命名空间com.spire.ms.Printing.*更改为com.spire.doc.printing.*。
新功能:
- 回收以下方法、类和接口。
- 声明废弃了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();
问题修复:
- 修复了在将Doc文档转换为Docx文档时,程序抛出“Error loading file: Unsupported file format”异常的问题。
- 修复了将Word文档转换为PDF文档时形状位置偏移的问题。
- 修复了将Word文档转换为PDF文档时,程序抛出"java.lang.OutOfMemoryError"异常的问题。
- 修复了将Word文档转换为PDF文档时图片多出边框的问题。
- 修复了将Word文档转换为PDF文档时表格宽度不正确的问题。
- 修复了将Word文档转换为PDF文档时分页不正确的问题。
- 修复了将Doc文档转换为XML文档时内容丢失的问题。
- 修复了保存文档数据到内存流时程序挂起的问题。
- 修复了将Word文档转换为PDF文档时文字乱码的问题。
- 修复了将Word文档转换为PDF文档时,程序抛出"This is not a structured storage file."异常的问题。
- 修复了将Word文档转换为PDF文档时表格样式不正确的问题。
- 修复了将Word文档转换为PDF文档时,程序抛出"java.lang.AssertionError"异常的问题。
- 修复了将Word文档转换为PDF文档时内容丢失的问题。
- 修复了新增行和合并行后表格格式不正确的问题。
- 修复了将Word文档转换为PDF文档时,程序抛出"java.lang.NullPointerException"异常的问题。
- 修复了将Word文档转换为PDF文档时,程序抛出"java.lang.ClassCastException"异常的问题。
- 修复了合并Word文档后导致公式无法编辑的问题。
- 修复了将Word文档转换为PDF文档时图表数据格式不正确的问题。
- 修复了加载Word文档时,程序抛出"java.lang.NullPointerException"异常的问题。
- 修复了设置文档密码保护后部分内容多出黑色背景的问题。
- 修复了拆分单元格后表格错乱的问题。
Spire.OCR for Java
问题修复:
- 修复了在JDK17和JDK21下运行时程序抛出 “java.lang.NoClassDefFoundError” 异常的问题。
Spire.Presentation for Java
新功能:
- 提高转换PPT到SVG的速度。
- 新增加载带密码的流文件的方法。
- 支持通过坐标创建不规则的多边形的功能。
- 支持通过两个点来绘制线条的功能。
presentation.loadFromStream(inputStream, FileFormat.AUTO,"password");
Presentation ppt = new Presentation();
ISlide slide = ppt.getSlides().get(0);
List<Point2D> points = new ArrayList<>();
points.add(new Point2D.Float(50f, 50f));
points.add(new Point2D.Float(50f, 150f));
points.add(new Point2D.Float(60f, 200f));
points.add(new Point2D.Float(200f, 200f));
points.add(new Point2D.Float(220f, 150f));
points.add(new Point2D.Float(150f, 90f));
points.add(new Point2D.Float(50f, 50f));
IAutoShape autoShape = slide.getShapes().appendFreeformShape(points);
autoShape.getFill().setFillType(FillFormatType.NONE);
ppt.saveToFile("out.pptx", FileFormat.PPTX_2013);
ppt.dispose();
Presentation ppt = new Presentation();
ppt.getSlides().get(0).getShapes().appendShape(ShapeType.LINE, new Point2D.Float(50, 70), new Point2D.Float(150, 120));
ppt.saveToFile( "result.pptx ,FileFormat.PPIX_2013),
ppt.dispose().