在Excel中,我们可以选中含有批注的单元格,右键并点击“显示/隐藏批注”选项来更改该批注的显示状态。本文将介绍如何使用Spire.XLS for Java实现在Excel文档中隐藏或显示批注。
原Excel文档:

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class HideOrShowComments {
public static void main(String[] args){
//创建Workbook实例
Workbook workbook = new Workbook();
//加载Excel文档
workbook.loadFromFile("CommentExample.xlsx");
//获取第一张工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//隐藏工作表中第一、二个批注
sheet.getComments().get(0).isVisible(false);
sheet.getComments().get(1).isVisible(false);
//显示第三个批注
sheet.getComments().get(2).isVisible(true);
//保存结果文档
workbook.saveToFile("HideOrShowComment.xlsx", ExcelVersion.Version2013);
}
}
结果文档:

Spire.PDF for Java 3.5.1已正式发布。该版本支持PDF签名添加时间戳、设置在PDF页面是否显示签名信息以及绘制箭头、云朵、椭圆、矩形和连接线条形状等标记。另外,也修复了在提取PDF中的文本、将PDF转为图片时出现的一些问题。新功能及问题修复详情如下。
新功能:
//配置时间戳服务器
String url = "https://freetsa.org/tsr";
signature.configureTimestamp(url);
signature.addShowConfigureText(SignatureConfigureText.Contact_Info);//显示Contact_Info(默认显示所有)
signature.removeShowConfigureText(SignatureConfigureText.Contact_Info);//不显示Contact_Info
signature.setShowConfigureText(EnumSet.of(SignatureConfigureText.Contact_Info));//只显示Contact_Info
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.getPages().add();
//箭头
String text1 = "this is Arrow annotation";
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20);
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.blue));
PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
page.getCanvas().drawString(text1, font, brush1, 50, 50, leftAlignment);
Dimension2D dimension = font.measureString(text1);
Rectangle2D.Float bounds = new Rectangle2D.Float(50, 50, (float) dimension.getWidth(), (float) dimension.getHeight());
int[] linePoints = new int[]{92, (int) (page.getSize().getHeight() - bounds.getY() - 60),
(int) (92 + bounds.getWidth()), (int) (page.getSize().getHeight() - bounds.getY() - 60)};
PdfLineAnnotation annotation1 = new PdfLineAnnotation(linePoints, "LineArrow annotation test");
annotation1.setBeginLineStyle(PdfLineEndingStyle.OpenArrow);
annotation1.setEndLineStyle(PdfLineEndingStyle.OpenArrow);
annotation1.setBackColor(new PdfRGBColor(Color.red));
annotation1.setCaptionType(PdfLineCaptionType.Inline);
annotation1.setLineCaption(true);
((PdfNewPage) page).getAnnotations().add(annotation1);
//云朵
String text2 = "this is Cloud annotation";
PdfBrush brush2 = PdfBrushes.getBlue();
page.getCanvas().drawString(text2, font, brush2, 50, 200);
Point2D point2D[] = new Point2D[]{
new Point2D.Float(30, 200),
new Point2D.Float(300, 180),
new Point2D.Float(300, 250),
new Point2D.Float(30, 220),
new Point2D.Float(30, 200)
};
PdfPolygonAnnotation annotation2 = new PdfPolygonAnnotation(page, point2D);
annotation2.setText("PolygonCloud annotation test");
annotation2.setAuthor("E-iceblue");
annotation2.setSubject("test");
annotation2.setModifiedDate(new Date());
annotation2.setBorderEffect(PdfBorderEffect.Big_Cloud);
annotation2.setLocation(new Point2D.Float(190, 230));
annotation2.setColor(new PdfRGBColor(Color.GRAY));
((PdfNewPage) page).getAnnotations().add(annotation2);
//椭圆
String text3 = "this is Circle annotation";
PdfBrush brush3 = PdfBrushes.getBlue();
Dimension2D dimension2D = font.measureString(text3);
dimension2D.setSize(dimension2D.getWidth() + 35, dimension2D.getHeight() + 20);
page.getCanvas().drawString(text3, font, brush3, 50, 300);
Rectangle2D.Float annotationBounds1 = new Rectangle2D.Float();
annotationBounds1.setFrame(new Point2D.Float(36, (float) 290), dimension2D);
PdfSquareAndCircleAnnotation annotation3 = new PdfSquareAndCircleAnnotation(annotationBounds1);
annotation3.setSubType(PdfSquareAndCircleAnnotationType.Circle);
float[] f1 = {0.5f, 0.5f, 0.5f, 0.5f};
annotation3.setRectangularDifferenceArray(f1);
annotation3.setText("Circle annotation test");
annotation3.setColor(new PdfRGBColor(Color.RED));
annotation3.setModifiedDate(new Date());
annotation3.setName("*****");
LineBorder border1 = new LineBorder();
border1.setBorderWidth(2);
annotation3.setLineBorder(border1);
((PdfNewPage) page).getAnnotations().add(annotation3);
//矩形
String text4 = "this is Square annotation";
PdfBrush brush4 = PdfBrushes.getBlue();
Dimension2D dimension4 = font.measureString(text4);
dimension2D.setSize(dimension2D.getWidth() + 80, dimension2D.getHeight() + 20);
page.getCanvas().drawString(text4, font, brush4, 50, 400);
Rectangle2D.Float annotationBounds2 = new Rectangle2D.Float();
annotationBounds2.setFrame(new Point2D.Float(30, (float) 400), dimension4);
PdfSquareAndCircleAnnotation annotation4 = new PdfSquareAndCircleAnnotation(annotationBounds2);
annotation4.setSubType(PdfSquareAndCircleAnnotationType.Square);
float[] f2 = {0.5f, 0.5f, 0.5f, 0.5f};
annotation4.setRectangularDifferenceArray(f2);
annotation4.setText("Square annotation test");
annotation4.setColor(new PdfRGBColor(Color.RED));
annotation4.setModifiedDate(new Date());
annotation4.setName("*****");
LineBorder border2 = new LineBorder();
border2.setBorderWidth(2);
annotation4.setLineBorder(border2);
((PdfNewPage) page).getAnnotations().add(annotation4);
//连接线
String text5 = "this is Connected lines annotation";
PdfBrush brush5 = PdfBrushes.getBlue();
page.getCanvas().drawString(text5, font, brush5, 50, 465);
Point2D pointzd[] = new Point2D[]{
new Point2D.Float(30, 470),
new Point2D.Float(300, 450),
new Point2D.Float(300, 520),
new Point2D.Float(30, 490),
new Point2D.Float(30, 470)
};
PdfPolygonAnnotation annotation5 = new PdfPolygonAnnotation(page, pointzd);
annotation5.setText("Connected Lines annotation test");
annotation5.setAuthor("冰蓝");
annotation5.setSubject("test");
annotation5.setModifiedDate(new Date());
annotation5.setBorderEffect(PdfBorderEffect.None);
annotation5.setLocation(new Point2D.Float(190, 230));
annotation5.setColor(new PdfRGBColor(Color.GRAY));
((PdfNewPage) page).getAnnotations().add(annotation5);
pdf.saveToFile("output/annotation.pdf");
for (PdfPageBase page : (Iterable) doc.getPages()) {
content.append(page.extractText(true,true,true));
}
问题修复:
在日常数据处理工作中,我们经常需要从 Excel 表格中提取插入的文本框内容,比如汇总备注、审阅意见等。但如果手动复制粘贴,效率低,且容易出错。本文将介绍如何使用 Spire.XLS for Java 快速批量提取 Excel 中文本框的内容,提高工作效率。
Spire.XLS for Java 是一款功能强大的 Excel 处理库,可让 Java 开发者无需安装 Microsoft Office 即可操作 Excel 文件。它支持读取、创建、修改、转换 Excel 等基础功能,同时也能访问和操作工作表中的文本框、图片、图表等对象。 你可以通过下面两种方法安装这个 Java 库:
方案一:如果你使用 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 文件手动添加到你的 Java 编译器或构建路径中。
如果你的 Excel 文件中只包含一个文本框,或者你清楚自己需要提取的是特定的文本框内容,那么可以使用本章介绍的方法直接读取并打印该文本框的内容。这种方式非常简单快捷,适用于仅需在控制台查看文本结果的场景,为后续将内容保存到文本文件打下基础。接下来,让我们看看如何通过 Java 代码实现这个目标。
详细步骤解析:
下方的代码演示了如何使用 Java 提取第二个工作表中第一个文本框的内容,并将其打印到控制台上:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;
public class ExtractTextFromTextbox {
public static void main(String[] args) {
// 创建 Workbook 对象
Workbook workbook = new Workbook();
// 加载示例文档
workbook.loadFromFile("/示例文档.xlsx");
// 获取第二个工作表
Worksheet sheet = workbook.getWorksheets().get(1);
//获取第二个文本框
XlsTextBoxShape shape = (XlsTextBoxShape)sheet.getTextBoxes().get(0);
// 获取文本框中的文本
String text = shape.getText();
System.out.print(text);
}
}

在上一章中,我们了解了如何读取单个文本框的内容并将其打印到控制台。然而,在实际工作中,Excel 文件往往包含多个文本框。本章将介绍如何通过循环一次性提取所有文本框的内容,为批量处理提供便利。
事实上,提取所有 Excel 中文本框的过程与提取单个文本框几乎一致,唯一的区别是在获取内容前需要使用 for 循环遍历所有文本框。具体的实现可以参考以下代码示例。
下方代码展示了怎样获取第二个工作表中的所有文本框的内容,并将其打印在控制台上:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;
public class ExtractTextFromTextbox {
public static void main(String[] args) {
// 创建Workbook对象
Workbook workbook = new Workbook();
// 加载示例文档
workbook.loadFromFile("/示例文档.xlsx");
// 获取第二个工作表
Worksheet sheet = workbook.getWorksheets().get(1);
// 遍历所有文本框
for (int i = 0; i < sheet.getTextBoxes().getCount(); i++) {
XlsTextBoxShape shape = (XlsTextBoxShape) sheet.getTextBoxes().get(i);
String text = shape.getText();
System.out.println("文本框" + (i + 1) + ": " + text);
}
}
}

当然,获取文本框内容的目的往往不仅仅是为了查看,还可能需要对其进行后续处理,比如提炼关键信息用于撰写报告,或者将内容整合到其他文件中。因此,将提取到的 Excel 文本框内容保存为文本文件,会更加方便后续使用和管理。
关键步骤:
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;
import java.io.FileWriter;
import java.io.IOException;
public class ExtractAndSaveText {
public static void main(String[] args) {
Workbook workbook = new Workbook();
workbook.loadFromFile("/示例文档.xlsx");
Worksheet sheet = workbook.getWorksheets().get(1);
// 遍历所有文本框
for (int i = 0; i < sheet.getTextBoxes().getCount(); i++) {
XlsTextBoxShape shape = (XlsTextBoxShape) sheet.getTextBoxes().get(i);
String text = shape.getText();
// 保存到文件
saveTextToFile("文本框 " + (i + 1) + ": " + text);
}
}
public static void saveTextToFile(String text) {
try {
FileWriter writer = new FileWriter("/提取文本.txt", true);
writer.write(text + "\n");
writer.close();
System.out.println("文本已保存");
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过以上步骤,我们成功实现了从 Excel 中批量提取文本框内容,并将其保存到文本文件中。这不仅提高了处理效率,也为后续的数据整理和报告编制打下了良好基础。如果你对本文使用的 Spire.XLS for Java 感兴趣,也可以联系我们申请30天免费试用!
Spire.PDF 6.5.6已发布。该版本支持添加和转换SVG图片到PDF,并修复了将PDF转换为Word、提取文本以及填充TextBoxField时出现的一些问题。新功能及问题修复详情见下文。
新功能:
PdfDocument doc1 = new PdfDocument();
doc1.LoadFromSvg(inputFile_svg);
PdfDocument doc2 = new PdfDocument();
doc2.LoadFromFile(inputFile_pdf);
PdfTemplate template = doc1.Pages[0].CreateTemplate();
template.Draw(doc2.Pages[0].Canvas, new PointF());
doc2.SaveToFile(outputFile, FileFormat.PDF);
PdfDocument doc = new PdfDocument();
doc.LoadFromSvg("https://cdn.e-iceblue.cn/test.svg");
doc.SaveToFile("result.pdf", FileFormat.PDF);
问题修复:
HTML(超文本标记语言)主要用于构建网页内容。虽然 HTML 在网页上呈现信息方面表现出色,但在数据分析功能和数据操作特性方面却不如 Excel 这类电子表格软件。通过将 HTML 数据转换为 Excel,用户可以利用 Excel 的高级功能,例如公式、图表、表格和宏等,进行高效地组织和分析数据。本文将介绍如何使用 Spire.XLS for .NET 在 C# 中将 HTML 转为 Excel。
首先,您需要将 Spire.XLS for .NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.XLS
Spire.XLS for .NET 提供了 Workbook.LoadFromHtml() 方法来加载 HTML 文件。加载 HTML 文件后,你可以使用 Workbook.SaveToFile() 方法轻松将其保存为 各种 Excel 格式,例如 Version97to2003、Version2007、Version2010、Version2013、Version2016、Xlsb2007、Xlsb2010 等。详细步骤如下:
using Spire.Xls;
namespace HtmlToExcel
{
internal class Program
{
static void Main(string[] args)
{
// 指定输入的 HTML 文件路径
string filePath = @"test.html";
// 创建 Workbook 类的对象
Workbook workbook = new Workbook();
// 加载 HTML 文件
workbook.LoadFromHtml(filePath);
// 将 HTML 文件保存为 Excel 2013(XLSX)格式
string result = @"HtmlToExcel.xlsx";
workbook.SaveToFile(result, ExcelVersion.Version2013);
workbook.Dispose();
}
}
}

除了将 HTML 文件转换为 Excel 外,Spire.XLS for .NET 还支持通过 CellRange.HtmlString 属性将 HTML 字符串插入到 Excel 单元格中。详细步骤如下:
using Spire.Xls;
namespace InsertHtmlStringInExcel
{
internal class Program
{
static void Main(string[] args)
{
// 创建 Workbook 类的对象
Workbook workbook = new Workbook();
// 获取第一个工作表
Worksheet sheet = workbook.Worksheets[0];
// 指定 HTML 字符串
string htmlCode = "<p><font size='12'>这是一段包含<b>粗体文本</b>和<span style='color: red;'>彩色文本</span>的段落。</font></p>";
// 获取要添加 HTML 字符串的单元格
CellRange range = sheet.Range["A1"];
// 将 HTML 字符串添加到单元格
range.HtmlString = htmlCode;
// 设置第一列的列宽
sheet.SetColumnWidth(1, 45);
// 将生成的工作簿保存为新文件
string result = @"InsertHtmlStringInCell.xlsx";
workbook.SaveToFile(result, ExcelVersion.Version2013);
workbook.Dispose();
}
}
}

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
本文介绍使用Spire.Doc for Java来复制Word中的水印效果,支持复制文本水印或图片水印。
测试文档如下,包含图片水印:

import com.spire.doc.*;
public class CopyWatermark {
public static void main(String[] args) {
//加载文档1
Document doc1 = new Document();
doc1.loadFromFile("input.docx");
//加载文档2
Document doc2 = new Document();
doc2.loadFromFile("target.docx");
//获取文档1的水印效果,设置到文档2
doc2.setWatermark(doc1.getWatermark());
//保存文档2
doc2.saveToFile("CopyWatermark.docx",FileFormat.Docx_2013);
doc2.dispose();
}
}
水印复制效果:

本文将介绍如何使用Spire.Presentation for Java设置和获取PowerPoint幻灯片的标题。
设置幻灯片标题
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
public class SetSlideTitle {
public static void main(String[] args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//给幻灯片设置标题
slide.setTitle("幻灯片标题");
//保存文档
ppt.saveToFile("SetTitle.pptx", FileFormat.PPTX_2013);
}
}

获取幻灯片标题
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
public class GetSlideTitle {
public static void main(String[] args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.loadFromFile("SetTitle.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//打印幻灯片标题
String tile = slide.getTitle();
System.out.println(tile);
}
}

本文介绍如何使用Spire.XLS for Java隐藏或显示Excel中的网格线。
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class HideOrShowGridlines {
public static void main(String[] args) {
//创建Workbook对象
Workbook workbook = new Workbook();
//加载Excel文档
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//获取第一个工作表
Worksheet worksheet = workbook.getWorksheets().get(0);
//隐藏网格线
worksheet.setGridLinesVisible(false);
////显示网格线
//worksheet.setGridLinesVisible(true);
//保存文档
workbook.saveToFile("HideGridlines.xlsx", ExcelVersion.Version2016);
}
}
隐藏网格线

该文将介绍如何使用Spire.Presentation for Java来提取PPT中SmartArt图形的文本内容。
首先, 准备一个文档中含有SmartArt图形的幻灯片文件:

提取PPT中SmartArt图形的文本内容代码如下:
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.ISmartArt;
import java.io.*;
public class extractTextFromSmartArt {
public static void main(String[] args) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
//新建txt文档
String result = "output/extractTextFromSmartArt.txt";
File file=new File(result);
if(file.exists()){
file.delete();
}
file.createNewFile();
FileWriter fw =new FileWriter(file,true);
BufferedWriter bw =new BufferedWriter(fw);
bw.write("Below is extracted text from SmartArt:" + "\r\n");
//遍历所有幻灯片并获取SmartArt图形.
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
for (int j = 0; j < presentation.getSlides().get(i).getShapes().getCount(); j++)
{
if (presentation.getSlides().get(i).getShapes().get(j) instanceof ISmartArt)
{
ISmartArt smartArt = (ISmartArt)presentation.getSlides().get(i).getShapes().get(j);
//提取SmartArt中的文本
for (int k = 0; k < smartArt.getNodes().getCount(); k++)
{
bw.write(smartArt.getNodes().get(k).getTextFrame().getText() + "\r\n");
}
}
}
}
bw.flush();
bw.close();
fw.close();
}
}
效果图:

本文介绍通过使用Spire.Doc for Java来复制Word文档中的页眉和页脚。
两个用于测试的Word文档如下,将左边Word文档中的页眉页脚复制到右边的文档:

import com.spire.doc.*;
public class CopyHeaderFooter {
public static void main(String[] args) {
//加载文档1
Document doc1 = new Document();
doc1.loadFromFile("input.docx");
//获取section
Section section1 = doc1.getSections().get(0);
//获取文档1的页眉页脚
HeaderFooter header = section1.getHeadersFooters().getHeader();
HeaderFooter footer = section1.getHeadersFooters().getFooter();
//加载文档2
Document doc2 = new Document();
doc2.loadFromFile("target.docx");
//遍历文档2的section
for (int i = 0; i< doc2.getSections().getCount();i++)
{
Section section2 = doc2.getSections().get(i);
//遍历页眉中的对象
for(int j = 0 ; j< header.getChildObjects().getCount();j++)
{
//获取页眉中的所有子对象
Object object1 = header.getChildObjects().get(j);
//复制文档1的页眉添加到文档2
section2.getHeadersFooters().getHeader().getChildObjects().add(((DocumentObject) object1).deepClone());
}
//同理复制页脚
for(int z = 0 ; z< footer.getChildObjects().getCount();z++)
{
Object object2 = footer.getChildObjects().get(z);
section2.getHeadersFooters().getFooter().getChildObjects().add(((DocumentObject) object2).deepClone());
}
}
//保存文档2
doc2.saveToFile("CopyHeaderFooter.docx",FileFormat.Docx_2013);
doc2.dispose();
}
}
页眉页脚复制结果:
