Spire.Doc for Java 是一款专业的 Java Word 组件,开发人员使用它可以轻松地将 Word 文档创建、读取、编辑、转换和打印等功能集成到自己的 Java 应用程序中。作为一款完全独立的组件,Spire.Doc for Java 的运行环境无需安装 Microsoft Office。同时兼容大部分国产操作系统,能够在中标麒麟和中科方德等国产操作系统中正常运行。Spire.Doc for Java 支持 WPS生成的 Word 格式文档(.wps, .wpt)
Spire.Doc for Java 能执行多种 Word 文档处理任务,包括生成、读取、转换和打印 Word 文档,插入图片,添加页眉和页脚,创建表格,添加表单域和邮件合并域,添加书签,添加文本和图片水印,设置背景颜色和图片,内容复制,添加脚注和尾注,添加超链接、添加数字签名,加密和解密 Word 文档,修改、替换、读取、添加、回复、删除和提取批注,添加形状,添加图像印章等。
该文将详细介绍如何使用Spire.Presentation for Java设置幻灯片段落的缩进样式与间距。
import com.spire.presentation.*;
public class IndentStyle {
public static void main(String[] args) throws Exception{
//加载示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
//获取幻灯片图形
IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);
//设置第一个段落首行缩进和间距
shape.getTextFrame().getParagraphs().get(0).setIndent(20);
shape.getTextFrame().getParagraphs().get(0).setLeftMargin(10);
shape.getTextFrame().getParagraphs().get(0).setSpaceAfter(10);
//设置第三个段落悬挂缩进和间距
shape.getTextFrame().getParagraphs().get(2).setIndent(-100);
shape.getTextFrame().getParagraphs().get(2).setLeftMargin(40);
shape.getTextFrame().getParagraphs().get(2).setSpaceBefore(0);
shape.getTextFrame().getParagraphs().get(2).setSpaceAfter(0);
//保存文档
String output = "output/indent.pptx";
presentation.saveToFile(output, FileFormat.PPTX_2010);
}
}
效果图:

本文介绍使用Spire.Presentation for Java编辑PPT幻灯片中的图片,包括设置图片大小、旋转角度、可选文本、可选标题、图片位置、透明度等等。
import com.spire.presentation.*;
public class ModifyImage {
public static void main(String[] args) throws Exception {
//创建实例,加载PPT测试文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");
//获取第二张幻灯片
ISlide slide = ppt.getSlides().get(1);
//遍历幻灯片中的形状
for( int i = 0; i< slide.getShapes().getCount();i++){
IShape shape = slide.getShapes().get(i);
//设置图片高、宽、位置、旋转、可选文本、像素、名称等
if(shape instanceof SlidePicture){
SlidePicture pic = (SlidePicture) shape;
pic.setHeight(200);
pic.setWidth(400);
pic.setAlternativeText("原始图片文件");
pic.setAlternativeTitle("标题1");
pic.setRotation(30);
pic.setLeft(200);
pic.setTop(150);
pic.setName("图片1");
pic.getPictureFill().getPicture().setTransparency(60);
}
}
//保存文档
ppt.saveToFile("ModifyImage.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}

Spire.Presentation 5.8.1已发布。该版本支持添加节、设置SmartArt节点轮廓线条样式和节点间的连接线条样式,并修复了将PPT转为PDF时出现的问题。新功能及问题修复详情如下。
新功能:
Presentation presentation = new Presentation();
presentation.LoadFromFile(pptfile);
ISlide slide = presentation.Slides[0];
//添加节
presentation.SectionList.Add("section1", slide);
//获取节
Section section;
String name;
for (int i = 0; i < presentation.SectionList.Count; i++)
{
section = presentation.SectionList[i];
name = section.Name;
foreach (long id in section.SlideIdList)
{
}
}
ISmartArt smartArt = presentation.Slides[0].Shapes[0] as ISmartArt;
int count =smartArt.Nodes.Count;
ISmartArtNode node;
for (int i = 0; i < count; i++)
{
node = smartArt.Nodes[i];
node.Line.FillType = FillFormatType.Solid;
node.Line.SolidFillColor.Color = Color.Gray;
node.Line.Width = 2;
}
ISmartArt smartArt = presentation.Slides[0].Shapes[0] as ISmartArt;
int count =smartArt.Nodes.Count;
ISmartArtNode node;
for (int i = 0; i < count; i++)
{
node = smartArt.Nodes[i];
node.LinkLine.FillType = FillFormatType.Solid;
node.LinkLine.SolidFillColor.Color = Color.Gray;
node.LinkLine.Width = 2;
node.LinkLine.DashStyle = LineDashStyleType.SystemDash;
}
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html
Spire.Doc for Java 3.8.1现已发布。该版本支持设置"Hyperlink base"文档属性,同时也支持获取水印的类型。另外,一些在.xml转.docx,HTML转PDF,Word转PDF及加载和保存Word文档时出现的问题得以成功解决。更多详情参加下文。
新功能:
doc.getBuiltinDocumentProperties().setHyperLinkBase(data);
WatermarkBase waterbase = doc.getDocument().getWatermark();
String waterType = waterbase.getType().name();
int value =waterbase.getType().getValue();
问题修复:
Spire.PDF 6.8.1 于今日发布。该版本主要解决了一些在PDF转图片,加载、合并、拆分、操作和打印PDF文件时出现的问题。详情如下。
问题修复:

TIFF(标签图像文件格式)诞生于 20 世纪 80 年代中期,是一种能够在不同计算机平台间保存高质量彩色图像的标准格式。由于 TIFF 格式广泛应用于传真、文档扫描及各类文件交换处理领域,在日常工作中,将 Excel 表格转换为 TIFF 图像的需求屡见不鲜。
本文将演示如何利用 Spire.XLS for Java,通过编程方式高效地将 Excel 文件转换为 TIFF 格式。
Spire.XLS for Java 是一款专业的 Java Excel 组件,允许开发人员在不安装 Microsoft Office 的情况下,直接通过代码实现 Excel 文件的创建、读取、编辑和转换。
使用 Spire.XLS 转换 Excel 文件为 TIFF 有以下几个主要优势:
如果你习惯使用 Maven 管理项目,只需在 pom.xml 文件中添加对应的依赖:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>16.9.2</version>
</dependency>
</dependencies>
你也可以手动下载 Spire.XLS 的 JAR 包并导入到项目的库路径中。
在许多文档归档、报表分享或系统对接的场景中,往往需要将完整的 Excel 文件一次性转换为 TIFF 图像,以便统一存储或对外提供不可编辑的报表文件。例如,财务月报、项目汇总表等文件,通常要求保留所有工作表内容,并按既定顺序输出。
Spire.XLS for Java 支持直接从 Workbook 层级进行转换,可将 Excel 中的所有工作表依次渲染并合并为一个 多页 TIFF 文件。这种方式无需逐个处理工作表,不仅实现简单,而且能够完整保留原始文档结构,适用于对完整性要求较高的场景。
以下是实现该功能的基本步骤:
import com.spire.xls.Workbook;
public class ConvertExcelToTIFF {
public static void main(String[] args) {
// 创建 Workbook 实例
Workbook workbook = new Workbook();
// 加载 Excel 文件
workbook.loadFromFile("/input/示例.xlsx");
// 将整个 Excel(所有工作表)保存为一个多页 TIFF 文件
workbook.saveToTiff("/output/Excel文件转TIFF.tiff");
}
}
原 Excel 文件与转换后的 TIFF 文件对比:

提示:在一些高要求的业务场景中,默认的图像分辨率可能无法满足需求。你可以通过 ConverterSetting 类来调整输出文件 的 DPI。
Excel 文件可能包含多个工作表,而在很多实际应用场景中,我们往往只需要导出某个特定数据表,如年度统计表或资产负债表。Spire.XLS for Java 提供了极其简便的接口,允许开发者通过索引直接定位工作表,并将其渲染为单页或多页 TIFF 图像。这种方式不仅能有效减小文件体积,还能确保接收方无法修改报表数据,从而提升了文档的安全性和正式感。
以下是实现该功能的详细步骤:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ConvertExcelToTIFF {
public static void main(String[] args) {
// 创建一个 Workbook 类的实例
Workbook workbook = new Workbook();
// 加载一个 Excel 文件
workbook.loadFromFile("/input/示例.xlsx");
// 获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 将第一个工作表保存为 TIFF 文件
sheet.saveToTiff("/output/Excel转TIFF.tiff");
}
}
Excel 文件与输出的 TIFF 文件对比预览:

在处理大型 Excel 报表时,我们有时并不需要导出整张工作表,而只需截取其中的核心数据区、图表摘要或某个特定统计汇总表。Spire.XLS for Java 提供了一个灵活的方法,允许开发者通过指定起始行、列索引和结束行、列索引,精准锁定需要转换的“目标快照”。这种局部转换方式能够极大提升信息传递的效率,并去除冗余的空白单元格或无关背景。
以下是实现该功能的详细步骤:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ConvertExcelToTIFF {
public static void main(String[] args) {
// 创建一个 Workbook 类的实例
Workbook workbook = new Workbook();
// 加载 Excel 文件
workbook.loadFromFile("/input/示例.xlsx");
// 获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
// 将指定的单元格区域保存为 TIFF 文件
sheet.saveToTiff("/output/单元格区域转TIFF.tiff",1,1,5,7);
}
}
源文件与 TIFF 对比预览:

建议:如果你的业务需求是将数据发布到网页或移动端,TIFF 可能并不是最佳选择。你可以将 Excel 转换为 PNG 等其它图片格式,它更轻量、兼容性更好。
通过 Spire.XLS for Java,可以快速将 Excel 文件转换为 TIFF 图像。从转换整个文件到转换工作表、单元格区域,你可以根据实际需求准确控制输出文件,在保证报表完整性的同时有效避免数据被修改,适用于归档、分发和正式交付等多种业务场景。如需了解更多功能或获取技术支持,欢迎随时联系我们。
本文详细介绍如何使用Spire.Presentation在Java应用程序中设置幻灯片中文字的方向。
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
public class setTextDirection {
public static void main(String[] args) throws Exception{
//创建PPT文档(默认包含一页空白幻灯片)
Presentation ppt = new Presentation();
//添加新矩形框到第一个幻灯片
IAutoShape textboxShape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(200, 70, 400, 100));
textboxShape.getShapeStyle().getLineColor().setColor(Color.white);
textboxShape.getFill().setFillType(FillFormatType.SOLID);
textboxShape.getFill().getSolidColor().setColor(Color.orange);
//添加文字到矩形框
textboxShape.getTextFrame().setText("You Are Welcome Here");
//设置文字方向为横排(默认横排)
textboxShape.getTextFrame().setVerticalTextType(VerticalTextType.HORIZONTAL);
//添加一个带中文的文本框到幻灯片
textboxShape = ppt.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(350, 200, 100, 200));
textboxShape.getShapeStyle().getLineColor().setColor(Color.white);
textboxShape.getFill().setFillType(FillFormatType.SOLID);
textboxShape.getFill().getSolidColor().setColor(Color.orange);
textboxShape.getTextFrame().setText("欢迎光临");
//设置文字方向为东亚竖排(避免文字旋转90度)
textboxShape.getTextFrame().setVerticalTextType(VerticalTextType.EAST_ASIAN_VERTICAL);
//保存文档
String result = "output/setTextDirection.pptx";
ppt.saveToFile(result, FileFormat.PPTX_2013);
}
}
效果图:

本文介绍如何使用Spire.Doc for Java让Word表格的列宽自适应内容,或者让Word表格的宽度自适应页面宽度,或者固定表格的列宽。
列宽自适应内容
import com.spire.doc.*;
import com.spire.doc.fields.TextRange;
public class AutofitToContent {
public static void main(String[] args) {
//创建Document对象
Document document = new Document();
//添加节
Section section = document.addSection();
//添加表格
Table table = section.addTable();
//设置行数和列数
table.resetCells(3, 2);
//添加内容到单元格并设置字体
TextRange textRange = table.get(0, 0).addParagraph().appendText("产品编号");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(0, 1).addParagraph().appendText("名称");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(1, 0).addParagraph().appendText("T1052");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(1, 1).addParagraph().appendText("YT机械键盘(红轴)");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(2, 0).addParagraph().appendText("T1062");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(2, 1).addParagraph().appendText("WX G41M-P3主板");
textRange.getCharacterFormat().setFontName("宋体");
//列宽自动适应内容
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);
//保存文档
document.saveToFile("AutofitToContent.docx", FileFormat.Docx);
}
}

表格自适应页面宽度
import com.spire.doc.*;
import com.spire.doc.fields.TextRange;
public class AutofitToWindow {
public static void main(String[] args) {
//创建Document对象
Document document = new Document();
//添加节
Section section = document.addSection();
//添加表格
Table table = section.addTable();
//设置行数和列数
table.resetCells(3, 2);
//添加内容到单元格并设置字体
TextRange textRange = table.get(0, 0).addParagraph().appendText("产品编号");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(0, 1).addParagraph().appendText("名称");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(1, 0).addParagraph().appendText("T1052");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(1, 1).addParagraph().appendText("YT机械键盘(红轴)");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(2, 0).addParagraph().appendText("T1062");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(2, 1).addParagraph().appendText("WX G41M-P3主板");
textRange.getCharacterFormat().setFontName("宋体");
//表格自适应页面宽度
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
//保存文档
document.saveToFile("AutofitToWindow.docx", FileFormat.Docx);
}
}

固定列宽
import com.spire.doc.*;
import com.spire.doc.fields.TextRange;
public class FixColumnWidths {
public static void main(String[] args) {
//创建Document对象
Document document = new Document();
//添加节
Section section = document.addSection();
//添加表格
Table table = section.addTable();
//设置行数和列数
table.resetCells(3, 2);
//添加内容到单元格并设置字体
TextRange textRange = table.get(0, 0).addParagraph().appendText("产品编号");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(0, 1).addParagraph().appendText("名称");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(1, 0).addParagraph().appendText("T1052");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(1, 1).addParagraph().appendText("YT机械键盘(红轴)");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(2, 0).addParagraph().appendText("T1062");
textRange.getCharacterFormat().setFontName("宋体");
textRange = table.get(2, 1).addParagraph().appendText("WX G41M-P3主板");
textRange.getCharacterFormat().setFontName("宋体");
//设置列宽
for (int i = 0; i < table.getRows().getCount(); i++) {
table.get(i,0).setCellWidth(80f,CellWidthType.Point);
table.get(i,1).setCellWidth(160f,CellWidthType.Point);
}
//固定列宽
table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
//保存文档
document.saveToFile("FixColumnWidths.docx", FileFormat.Docx);
}
}

本文介绍使用Spire.Presentation for Java删除PPT幻灯片中的图片。
import com.spire.presentation.*;
public class RemoveImages {
public static void main(String[] args) throws Exception{
//加载PPT文档
Presentation ppt = new Presentation();
ppt.loadFromFile("sample.pptx");
//获取指定幻灯片
ISlide slide = ppt.getSlides().get(1);
//遍历幻灯片中的形状
for( int i = 0; i< slide.getShapes().getCount();i++){
IShape shape = slide.getShapes().get(i);
//删除图片
if(shape instanceof SlidePicture){
SlidePicture pic = (SlidePicture) shape;
slide.getShapes().remove(pic);
}
}
//保存文档
ppt.saveToFile("removeimgs.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
图片删除效果:
