Spire.XLS 10.9.16 现已发布。该版本支持设置透视表域的重复项目标签选项, 并成功修复了一些在转换Excel到PDF、转换XLS到XLSX/ ODS时出现的问题。新功能及问题修复详情如下。
新功能:
pt.PivotFields["OnHand"].RepeatItemLabels = true;
问题修复:
本文将介绍如何使用Spire.Presentation for .NET取消PowerPoint文档中的形状组合。
原PowerPoint文档含有2个形状组合:

using Spire.Presentation;
namespace UngroupShapes
{
class Program
{
static void Main(string[] args)
{
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.LoadFromFile("Sample.pptx");
//获取第一张幻灯片
ISlide slide = ppt.Slides[0];
//遍历幻灯片中的形状
for(int i = 0; i< slide.Shapes.Count;i++)
{
IShape shape = slide.Shapes[i];
//判断形状是否是组合形状
if (shape is GroupShape)
{
GroupShape groupShape = shape as GroupShape;
//取消形状组合
slide.Ungroup(groupShape);
}
}
//保存结果文档
ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013);
}
}
}
Imports Spire.Presentation
Namespace UngroupShapes
Class Program
Private Shared Sub Main(ByVal args As String())
Dim ppt As Presentation = New Presentation()
ppt.LoadFromFile("Sample.pptx")
Dim slide As ISlide = ppt.Slides(0)
For i As Integer = 0 To slide.Shapes.Count - 1
Dim shape As IShape = slide.Shapes(i)
If TypeOf shape Is GroupShape Then
Dim groupShape As GroupShape = TryCast(shape, GroupShape)
slide.Ungroup(groupShape)
End If
Next
ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013)
End Sub
End Class
End Namespace
取消形状组合后的结果文档:

在处理 HTML 文件时,你可能会发现它们在不同的浏览器和屏幕尺寸下的显示效果并不一致。而将 HTML 转换为 PDF 则可以有效地保留其布局和格式,从而确保内容在不同设备和平台上的呈现保持一致。本文将介绍如何在 Spire.Doc for Java 的帮助下通过 Java 将 HTML 文件或 HTML 字符串转换为 PDF。
Spire.Doc for Java 是一款功能强大且专业的 Java 组件,可用于在不依赖 Microsoft Office 的环境下轻松处理 Word 文档,实现文档自动化操作。 要在项目中安装 Spire.Doc,有两种常见方式:
● 手动引入:将 Spire.Doc.jar 文件添加为 Java 项目的依赖项。你可以从官方网站下载该 JAR 文件。
● 使用 Maven 管理依赖:如果你使用的是 Maven 项目,可以在 pom.xml 文件中添加以下依赖代码来导入 Spire.Doc:
<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>
在很多情况下,为了确保文档在不同设备上具有一致的显示效果、避免排版错乱,将 HTML 转换为 PDF 是一种理想的解决方案。由于 PDF 具有固定版式的特点,它能在各种终端上保持一致的排版与布局。 借助 Spire.Doc,你可以轻松地将 HTML 文件转换为 PDF。只需使用 Document.saveToFile() 方法即可实现。具体操作步骤如下:
● 创建一个 Document 对象。
● 通过 Document.loadFromFile() 方法加载 HTML 文件。
● 使用 Document.saveToFile(String fileName, FileFormat.PDF) 方法将 HTML 文件转换并保存为 PDF 格式。
import com.spire.doc.*;
import com.spire.doc.documents.XHTMLValidationType;
public class htmlFileToPdf {
public static void main(String[] args) {
// 创建一个 Document 类的对象
Document document = new Document();
// 加载一个 HTML 文件
document.loadFromFile("E:/Administrator/Python1/input/项目进度.html", FileFormat.Html, XHTMLValidationType.None);
// 将 HTML 文件保存为 PDF
document.saveToFile("E:/Administrator/Python1/output/HTML转PDF.pdf", FileFormat.PDF);
document.dispose();
}
}

要将 HTML 字符串转换为 PDF,你可以先通过 Paragraph.appendHTML() 方法将该字符串添加到 Word 文档中的段落里,然后再将文档保存为 PDF 文件。具体操作步骤如下:
● 创建一个 Document 对象。
● 使用 Document.addSection() 方法添加一个节。
● 使用 Section.addParagraph() 方法添加一个段落。
● 指定 HTML 字符串,并通过 Paragraph.appendHTML() 方法将其添加到段落中。
● 使用 Document.saveToFile(String fileName, FileFormat.PDF) 方法将文档保存为 PDF 格式。
下方代码展示了如何将一个包含文本和表格内容的 HTML 字符串转换为 PDF:
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import java.io.*;
public class htmlstringtopdf {
public static void main(String[] args) throws IOException {
// 创建一个 Document 类的对象
Document document = new Document();
// 在文档中添加一个节
Section sec = document.addSection();
// 在节中添加一个段落
Paragraph paragraph = sec.addParagraph();
// 自定义 HTML 字符串
String htmlString = "<!DOCTYPE html>\n" +
"<html lang=\"zh-CN\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>月度销售报告</title>\n" +
" <style>\n" +
" body {\n" +
" font-family: \"Microsoft YaHei\", sans-serif;\n" +
" line-height: 1.6;\n" +
" padding: 20px;\n" +
" }\n" +
" table {\n" +
" border-collapse: collapse;\n" +
" width: 60%;\n" +
" margin-top: 20px;\n" +
" }\n" +
" th, td {\n" +
" border: 1px solid #666;\n" +
" padding: 8px 12px;\n" +
" text-align: center;\n" +
" }\n" +
" th {\n" +
" background-color: #f2f2f2;\n" +
" }\n" +
" </style>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
" <h2>月度销售报告</h2>\n" +
"\n" +
" <p>本月销售部门整体表现良好。下表为部分地区销售数据汇总:</p>\n" +
"\n" +
" <table>\n" +
" <tr>\n" +
" <th>地区</th>\n" +
" <th>负责人</th>\n" +
" <th>销售额(万元)</th>\n" +
" <th>完成率</th>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>华北地区</td>\n" +
" <td>张伟</td>\n" +
" <td>150</td>\n" +
" <td>125%</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>华东地区</td>\n" +
" <td>李静</td>\n" +
" <td>120</td>\n" +
" <td>100%</td>\n" +
" </tr>\n" +
" </table>\n" +
"\n" +
"</body>\n" +
"</html>\n";
// 将 HTML 字符串添加到段落中
paragraph.appendHTML(htmlString);
// 将 HTML 字符串保存为 PDF 文件
document.saveToFile("E:/Administrator/Python1/output/htmlString转PDF.pdf", FileFormat.PDF);
document.dispose();
}
}

本指南介绍了如何使用 Java 将 HTML 文件或 HTML 字符串转换为 PDF 文件。借助 Spire.Doc,你可以轻松实现这一过程,快速高效地完成文档转换任务。如果你对该工具感兴趣,欢迎下载试用免费版。如需更多功能支持或商业授权,欢迎随时联系我们!
Spire.XLS 3.9.3 for Java已发布。该版本增加了新的方法以支持将一个Excel文档转换为TIFF文件,同时在转换Excel到Image时支持了设置dpi的功能。 此外,该版本修复了大量转换时出现的问题。详情请阅读以下。
新功能:
workbook.getConverterSetting().setXDpi(300);
workbook.getConverterSetting().setYDpi(300);
Worksheet sheet;
for (int i = 0; i < wb.getWorksheets().size(); i++) {
sheet = wb.getWorksheets().get(i);
sheet.saveToImage(outputFile + i + ".png");
}
问题修复:
Spire. Presentation 5.9.5现已发布。该版本支持对组合图形取消组合和GroupShapes方法返回GroupShape对象,并新增了GetPlaceholderShapes方法来通过占位符获取幻灯片母版版式中的形状的功能。此外,该版本还修复了转换加载PPTX文档,拷贝幻灯片等情况时出现的问题。详情请阅读以下内容。
新功能:
GroupShape groupShape = presentation.Slides[0].Shapes[0] as GroupShape;
presentation.Slides[0].Ungroup(groupShape);
Placeholder placeholder = presentation.Slides[1].Shapes[0].Placeholder;
IShape[] shapes = presentation.Slides[1].GetPlaceholderShapes(placeholder);
for (int i = 0; i < shapes.Length; i++)
{
if (shapes[i] is IAutoShape)
{
IAutoShape autoShape = shapes[i] as IAutoShape;
if (autoShape.TextFrame != null)
{
Console.WriteLine(autoShape.TextFrame.Text);
}
}
}
ArrayList groupShapeList = new ArrayList();
groupShapeList.Add(shape1);
groupShapeList.Add(shape2);
groupShapeList.Add(shape3);
GroupShape groupshape = ppt.Slides[0].GroupShapes(groupShapeList);
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html

在处理篇幅较长的 Word 文档时,为了提升团队协作效率,通常需要将大型文件拆分成多个较小的文档并分发给不同成员。为了避免手动剪切粘贴浪费时间和精力,本文将演示如何通过 Java 拆分 Word 文档。这种自动化的处理方式不仅能大幅提升工作效率,还能确保文档内容的准确性,帮助你轻松实现高效的文档管理。
在开始编写代码之前,我们需要引入专业的 Java 库——Spire.Doc for Java 。它是一款功能丰富的 Word 文档处理组件,最大的优势在于无需安装 Microsoft Word 即可独立实现文档的创建、编辑、拆分与合并,非常适合集成在服务器端或自动化脚本中。
你可以通过以下两种方式轻松地将该类库集成到你的 Java 项目中:
<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.doc</artifactId>
<version>14.9.5</version>
</dependency>
</dependencies>
在完成库的安装与环境配置后,我们就可以开始探索具体的拆分方案了。在 Word 文档中,节是一个特殊的单位,它允许文档的每一部分拥有独立的页面设置、页边距以及页眉页脚。如果你的文档结构分明(如合同、标书或报告),插入分节符可以用来区分不同的章节。Spire.Doc for Java 完美支持通过识别这些分节符来完成自动化拆分 Word 文档。
步骤解析:
下面是在 Java 中按照节拆分 Word 文档的完整代码示例:
import com.spire.doc.*;
public class splitDocBySectionBreak {
public static void main(String[] args) throws Exception {
// 创建 Document 实例
Document document = new Document();
// 加载 Word 示例文档
document.loadFromFile("/input/示例文档.docx");
// 定义新的 Word 文档对象
Document newWord;
// 遍历原始 Word 文档中的所有节
for (int i = 0; i < document.getSections().getCount(); i++) {
newWord = new Document();
// 克隆原始文档的每一个节,并将其作为新节添加到新文档中
newWord.getSections().add(document.getSections().get(i).deepClone());
// 修正后的保存路径:使用循环变量 i 进行拼接,并去掉多余的双引号转义符
String outputPath = "/output/result1/拆分结果_" + i + ".docx";
// 保存结果文档
newWord.saveToFile(outputPath, FileFormat.Docx_2013);
}
}
}
输出结果预览:

提示:按节拆分的关键在于对文档元素的深度克隆。如果你不仅想拆分文档,还希望在不同文件间灵活移动或备份特定内容,可以参考这篇关于 Java 复制 Word 文档的指南,进一步了解 Java 操作 Word 文档的底层逻辑。
在 Word 文档中,分页符是定义页面结束和新页面开始的常用标识。通过识别这些标记,我们可以精确地将一个长文档切割成多个单页或多页的小文件。以下是利用 Java 按页面拆分 Word 文档的具体逻辑与代码实现。
步骤解析:
下面是完整的代码示例,展示了 Java 按照页面拆分一个 docx 文档:
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class splitDocByPageBreak {
public static void main(String[] args) throws Exception {
// 创建 Document 类的对象,并加载 Word 文档
Document original = new Document();
original.loadFromFile("/input/示例文档.docx");
// 创建一个新 Word 文档并为其添加一个节
Document newWord = new Document();
Section section = newWord.addSection();
int index = 0;
// 遍历原始文档中的所有节
for (int s = 0; s < original.getSections().getCount(); s++) {
Section sec = original.getSections().get(s);
// 遍历每个节中的所有正文子对象
for (int c = 0; c < sec.getBody().getChildObjects().getCount(); c++) {
DocumentObject obj = sec.getBody().getChildObjects().get(c);
if (obj instanceof Paragraph) {
Paragraph para = (Paragraph) obj;
sec.cloneSectionPropertiesTo(section);
// 将原始节中的段落对象添加到新文档的节中
section.getBody().getChildObjects().add(para.deepClone());
for (int i = 0; i < para.getChildObjects().getCount(); i++) {
DocumentObject parobj = para.getChildObjects().get(i);
if (parobj instanceof Break) {
Break break1 = (Break) parobj;
if (break1.getBreakType().equals(BreakType.Page_Break)) {
// 获取分页符在段落中的索引
int indexId = para.getChildObjects().indexOf(parobj);
// 从该段落中移除分页符
Paragraph newPara = (Paragraph) section.getBody().getLastParagraph();
newPara.getChildObjects().removeAt(indexId);
// 保存新生成的 Word 文档
newWord.saveToFile("/output/result/拆分结果"+index+".docx", FileFormat.Docx);
index++;
// 创建新文档并添加新节
newWord = new Document();
section = newWord.addSection();
// 再次将当前段落对象(分页符之后的部分)添加到新文档的节中
section.getBody().getChildObjects().add(para.deepClone());
if (section.getParagraphs().get(0).getChildObjects().getCount() == 0) {
// 移除首个空白段落
section.getBody().getChildObjects().removeAt(0);
} else {
// 移除分页符之前的子对象
while (indexId >= 0) {
section.getParagraphs().get(0).getChildObjects().removeAt(indexId);
indexId--;
}
}
}
}
}
}
if (obj instanceof Table) {
// 将原始节中的表格对象添加到新文档的节中
section.getBody().getChildObjects().add(obj.deepClone());
}
}
}
// 保存最后一个拆分出的文档
newWord.saveToFile("/output/result/拆分结果"+index+".docx", FileFormat.Docx);
}
}
输出结果预览:

答: 在 “开始” 选项卡中点击 “显示/隐藏编辑标记” 功能,文档中的分页符或分节符会以虚线形式显现。或者使用大纲形式阅览文档,这样也可以快速检查分节符和分页符的确切位置。
答: 这通常是因为原文档的拆分点包含了多余的分页符或换行符。在代码中,你可以尝试在保存前检查 section.getBody().getChildObjects(),并手动移除索引为 0 的空白段落。
答: 完全可以。在使用 Document.saveToFile() 方法时,只需修改第二个参数即可。例如,将 FileFormat.Docx 改为 FileFormat.PDF,即可在拆分的同时完成格式转换。
掌握使用 Java 拆分 Word 文档的技术,能够极大地提升文档管理的效率和准确性。无论是简单地按节拆分,还是高效精准的按页切割,Spire.Doc for Java 都能提供稳定的支持。若在实践过程中遇到任何技术难题或特殊需求,欢迎随时联系我们,获取技术支持。
本文将介绍如何使用Spire.PDF for Java来将SVG转换为PDF及添加SVG图片到PDF文档。首先请确保产品版本号为3.9.6及以上。
SVG转PDF
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
public class SVGToPDF {
public static void main(String[] args) {
//加载示例SVG图片
PdfDocument doc = new PdfDocument();
doc.loadFromSvg("https://cdn.e-iceblue.cn/C:\\Users\\Test1\\Desktop\\logo.svg");
//保存为PDF文档
doc.saveToFile("output/ToPDF.pdf", FileFormat.PDF);
}
}
转换前后对比:

添加SVG图片到PDF
在将已有的SVG图片添加到PDF文档时,可设置图片的位置和大小。
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import java.awt.*;
import java.awt.geom.Point2D;
public class AddSVGImagetoPDF {
public static void main(String[] args) {
//加载示例SVG图片
PdfDocument doc1 = new PdfDocument();
doc1.loadFromSvg("https://cdn.e-iceblue.cn/C:\\Users\\Test1\\Desktop\\logo.svg");
//加载示例PDF文档
PdfDocument doc2 = new PdfDocument();
doc2.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
//设置图片在文档中的位置和大小
doc2.getPages().get(0).getCanvas().drawTemplate(doc1.getPages().get(0).createTemplate(),
new Point2D.Float(100,150), new Dimension(200,200) );
//保存PDF文档
doc2.saveToFile("output/AddSVGImagetoPDF.pdf", FileFormat.PDF);
doc1.close();
doc2.close();
}
}
添加效果:

本文将介绍如何使用Spire.XLS for Java判断Excel工作表中是否包含合并单元格并对合并单元格进行取消合并。
原Excel文档:

import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class DetectMergedCells {
public static void main(String[] args) throws Exception {
//创建Workbook实例
Workbook workbook = new Workbook();
//加载Excel文档
workbook.loadFromFile( "Input.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//获取第一个工作表中的合并单元格并存入CellRange数组
CellRange[] range = sheet.getMergedCells();
//遍历数组,取消单元格合并
if(range != null && range.length > 0) {
for (CellRange cell : range) {
cell.unMerge();
}
}
//保存结果文档
workbook.saveToFile("DetectMergedCells.xlsx", ExcelVersion.Version2013);
}
}
结果文档:

Spire.PDF 3.9.6 for Java现已发布。该版本支持转换SVG到PDF,添加SVG图片到PDF文档中,以及以流式布局转换PDF到Word的功能,并且修复了绘制字形和提取文本乱码的问题。
新功能:
PdfDocument doc = new PdfDocument();
doc.loadFromSvg(inputFile_svg);
doc.saveToFile(outputFile);
PdfDocument doc1 = new PdfDocument();
doc1.loadFromSvg(inputFile_svg);
PdfDocument doc2 = new PdfDocument();
doc2.loadFromFile(inputFile_pdf);
doc2.getPages().get(0).getCanvas().drawTemplate(doc1.getPages().get(0).createTemplate(), new Point2D.Float(10,10), new Dimension(100,100) );
doc2.saveToFile(outputFile, FileFormat.PDF);
doc1.close();
doc2.close();
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(inputFile);
pdf.getConvertOptions().setConvertToWordUsingFlow(true);
pdf.saveToFile(outputFile_doc, FileFormat.DOC);
问题修复:
之前我们介绍了如何使用 PresentationPrintDocument 打印 PowerPoint 文档, 该文将介绍如何使用PrinterSettings 类打印PowerPoint 文
import com.spire.ms.Printing.*;
import com.spire.presentation.*;
public class PrintPPT {
public static void main(String[] args) throws Exception {
//加载文档
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
//使用PrinterSettings 对象打印幻灯片
PrinterSettings ps = new PrinterSettings();
ps.setPrintRange(PrintRange.AllPages);
//ps.setPrintToFile(true);
//打印时幻灯片加框
presentation.setSlideFrameForPrint(true);
//灰度打印
presentation.setGrayLevelForPrint(true);
//每四张幻灯片打印到一页
presentation.setSlideCountPerPageForPrint(PageSlideCount.Four);
//设置打印方向
presentation.setOrderForPrint(Order.Horizontal);
//选择需要打印的幻灯片
presentation.SelectSlidesForPrint("1", "3");
//打印文档
presentation.print(ps);
presentation.dispose();
}
}