美文网首页程序员
Java 在Word文档中添加艺术字

Java 在Word文档中添加艺术字

作者: 欧子有话说_ | 来源:发表于2022-09-03 09:43 被阅读0次

艺术字是以普通文字为基础,经过专业的字体设计师艺术加工的变形字体。字体特点符合文字含义、具有美观有趣、易认易识、醒目张扬等特性,是一种有图案意味或装饰意味的字体变形,常用来创建旗帜鲜明的标志或标题。 本文将详细介绍 在 Java应用程序中 **为W ord文档添加艺术字 **并设置样式和效果。

程序环境:

方法1:手动引入。将 Free Spire.Doc for Java 下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序

image.png

方法 2: 如果您想通过 Maven 安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。

<repositories>

<repository>

<id>com.e-iceblue</id>

<url>https://repo.e-iceblue.cn/repository/maven-public/</url>

</repository>

</repositories>

<dependencies>

<dependency>

<groupId>e-iceblue</groupId>

<artifactId>spire.doc.free</artifactId>

<version>5.2.0</version>

</dependency>

</dependencies>

具体步骤:

  • 创建 Document 类的对象 。
  • 使用 **Document.addSection() **方法向文档添加一个 section,然后使用 **Section.addParagraph() **方法向 section添加一个段落。
  • 将形状附加到段落并使用 **Paragraph.appendShape() **方法指定形状大小和类型。
  • 使用 **ShapeObject.setVerticalPosition() **和 **ShapeObject.setHorizontalPosition() **方法设置形状的位置。
  • 使用 **ShapeObject.getWordArt().setText() **方法设置艺术字的文本。
  • 使用 **ShapeObject.setFillColor() **和 **ShapeObject.setStrokeColor() **方法设置艺术字的填充颜色和描边颜色。
  • 使用 **Document.saveToFile() **方法将文档保存到另一个文件。

完整代码:

【Java】

<pre class="prettyprint hljs swift" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import com.spire.doc.;
import com.spire.doc.documents.
;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

public class WordArt{
public static void main(String[] args) throws Exception {
//创建Document类的对象
Document doc = new Document();

    //添加一个section
    Section section = doc.addSection();

    //添加一个段落.
    Paragraph paragraph = section.addParagraph();

    //向段落添加形状并指定形状大小和类型
    ShapeObject shape = paragraph.appendShape(400, 150, ShapeType.Text_Deflate_Bottom);

    //设置形状的位置
    shape.setVerticalPosition(60);
    shape.setHorizontalPosition(60);

    //设置艺术字的文本内容
    shape.getWordArt().setText("艺术字效果");

    //设置艺术字的填充颜色和描边颜色
    shape.setFillColor(Color.CYAN);
    shape.setStrokeColor(Color.BLUE);

    //将文档保存到文件.
    doc.saveToFile("WordArt.docx", FileFormat.Docx_2013);
}

}
</pre>

效果图:

image.png

相关文章

网友评论

    本文标题:Java 在Word文档中添加艺术字

    本文链接:https://www.haomeiwen.com/subject/wdrunrtx.html