太实用了!在Java的Word 文档中插入或读取艺术字
简直不能太实用!
扫码关注《Java学研大本营》,加入读者群,分享更多精彩
艺术字是一组文本样式,允许您向文本添加设计元素,例如填充、轮廓和阴影。 插入艺术字是使您的文本突出和引人注目的好方法。 在 Microsoft Word 中创建文档时,有时可能需要插入艺术字。 本文将解释如何 在 Java 的 Word 文档中插入或读取艺术字 使用 Free Spire.Doc for Java 库(https://www.e-iceblue.com/Introduce/free-doc-for-java.html)
添加依赖项
在编码之前,您需要添加所需的依赖项,以便将 Free Spire.Doc for Java 包含到您的 Java 项目中。 有两种方法可以做到这一点。
方法一:
如果你使用maven,你可以很容易的将Free Spire.Doc for Java的JAR文件导入到你的应用程序中,只需在你项目的pom.xml文件中添加如下代码即可。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
方法二:
如果你不使用maven,可以从 这个链接 ,解压zip文件,然后将lib文件夹下的Spire.Doc.jar文件作为依赖导入到你的项目中。
使用 Java 将艺术字插入 Word
以下是将艺术字插入 Word 文档的主要步骤:
-
创建 Document 类的实例。
-
加载 Word 文档 Document.loadFromFile() 方法
-
文档的所需部分 Document.getSections().get(sectionIndex) 方法
-
向该部分添加一个段落 Section.addParagraph() 方法
-
将指定大小和类型的形状添加到段落 Paragraph.appendShape() 方法
-
设置形状的位置。
-
将具有指定文本的艺术字插入形状 ShapeObject.getWordArt().setText() 方法
-
为艺术字设置填充颜色和边框颜色。
-
保存结果文档 Document.saveToFile() 方法
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class InsertWordArtInWord {
public static void main(String[] args){
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.loadFromFile("input.docx");
//Get the first section
Section section = doc.getSections().get(0);
//Add a paragraph to the section
Paragraph paragraph = section.addParagraph();
//Add a shape to the paragraph
ShapeObject shape = paragraph.appendShape(250, 70, ShapeType.Text_Wave_3);
//Set the position of the shape
shape.setVerticalPosition(20);
shape.setHorizontalPosition(80);
//Set the text of WordArt
shape.getWordArt().setText("Happy Birthday");
//Set the fill color
shape.setFillColor(Color.orange);
//Set the border color of the text.
shape.setStrokeColor(Color.YELLOW);
//Save the result document
doc.saveToFile("InsertWordArt.docx", FileFormat.Docx);
}
}
使用 Java 在 Word 中读取艺术字
以下是在 Word 文档中读取艺术字的主要步骤:
-
创建 Document 类的实例。
-
加载 Word 文档 Document.loadFromFile() 方法
-
循环浏览文档中的所有部分。
-
循环浏览每个部分中的所有段落。
-
循环遍历每个段落中的所有子对象。
-
检测子对象是否为 ShapeObject 。
-
获取形状对象中的艺术字文本 ShapeObject.getWordArt().getText() 方法
-
如果文本不为空,则将其打印到控制台。
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
public class ReadWordArt {
public static void main(String[] args){
//Create a Document instance
Document doc = new Document();
//Load a word document
doc.loadFromFile("InsertWordArt.docx");
//Loop through all the sections in the document
for (Section section :(Iterable<? extends Section>) doc.getSections()) {
//Loop through all the paragraphs in each section
for (Paragraph paragraph : (Iterable<? extends Paragraph>) section.getBody().getParagraphs()) {
//Loop through all the child objects in each paragraph
for (DocumentObject documentObject : (Iterable<? extends DocumentObject>) paragraph.getChildObjects()) {
//Detect if the child object is a shape
if (documentObject instanceof ShapeObject) {
ShapeObject shapeObject = (ShapeObject) documentObject;
//Detect if the shape is a WordArt
String text = shapeObject.getWordArt().getText();
if (text != "") {
//Read the WordArt text
System.out.println("WordArt Text:" + text);
}
}
}
}
}
}
}
https://dev.to/alexis92/java-insert-or-read-wordart-in-word-documents-3iok
推荐书单
《项目驱动零起点学Java》
购买链接:https://item.jd.com/13607758.html
《项目驱动零起点学Java》贯穿6个完整项目,经过作者多年教学经验提炼而得,项目从小到大、从短到长,可以让读者在练习项目的过程中,快速掌握一系列知识点。
作者是国内知名Java教学者和传播者,一路披荆斩棘,兢兢业业20余年。积累了丰富的“培”“训”经验,也产出了很多优质的教学理论。
Java语言经过数十年的发展,体系逐渐变得庞大而复杂,本书芟繁就简,提炼出了最为重要的知识点,可以让读者轻松上手。本书配套有专栏课程,课程中提供了扩展内容。
《项目驱动零起点学Java》共分 13 章,围绕 6 个项目和 258 个代码示例,分别介绍了走进Java 的世界、变量与数据类型、运算符、流程控制、方法、数组、面向对象、异常、常用类、集合、I/O流、多线程、网络编程相关内容。《项目驱动零起点学Java》总结了马士兵老师从事Java培训十余年来经受了市场检验的教研成果,通过6 个项目以及每章的示例和习题,可以帮助读者快速掌握Java 编程的语法以及算法实现。扫描每章提供的二维码可观看相应章节内容的视频讲解。
精彩回顾
扫码关注《Java学研大本营》,加入读者群,分享更多精彩