文档星空最火知名网站>>Spire.Doc系列教程>>Spire.Doc功能演示:使用Java在Word文档上设置字符间距和段落间距
Spire.Doc功能演示:使用Java在Word文档上设置字符间距和段落间距
Spire.Doc for Java 是一款专业的Java Word组件,开发人员使用它可以轻松地将Word文档创建、读取、编辑、转换和打印等功能集成到自己的Java应用程序中。
本文将向您展示如何使用Spire.Doc for Java在Word上设置字符间距和段落间距。可点击此处下载最新版测试。
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;
import java.io.*;
public class setSpacing {
public static void main(String[] args)throws IOException {
//Load the sample document
Document document= new Document("Sample1.docx");
//Add a new paragraph and append the text
Paragraph para = new Paragraph(document);
TextRange textRange1 = para.appendText("Newly added paragraph and set the paragraph spacing and character spacing");
textRange1.getCharacterFormat().setTextColor(Color.blue);
textRange1.getCharacterFormat().setFontSize(14);
//Set the spacing before and after paragraph
para.getFormat().setBeforeAutoSpacing(false);
para.getFormat().setBeforeSpacing(10);
para.getFormat().setAfterAutoSpacing(false);
para.getFormat().setAfterSpacing(10);
//Set the character spacing
for (DocumentObject object :(Iterable) para.getChildObjects())
{
TextRange textRange= (TextRange) object;
textRange.getCharacterFormat().setCharacterSpacing(3f);
}
//Insert the paragraph
document.getSections().get(0).getParagraphs().insert(2, para);
//Save the document to file
document.saveToFile("Result.docx", FileFormat.Docx);
}
}
输出结果

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线