该文章将详细介绍如何使用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 {
        //加载示例文档
        Document document= new Document("Sample.docx");
        //添加新段落并设置段落文本和字体样式
        Paragraph para = new Paragraph(document);
        TextRange textRange1 = para.appendText("新加段落,设置行间距和字间距");
        textRange1.getCharacterFormat().setTextColor(Color.BLUE);
        textRange1.getCharacterFormat().setFontSize(15);
        //设置段前段后间距
        para.getFormat().setBeforeAutoSpacing(false);
        para.getFormat().setBeforeSpacing(10);
        para.getFormat().setAfterAutoSpacing(false);
        para.getFormat().setAfterSpacing(10);
        //设置字间距
        for (DocumentObject object :(Iterable<DocumentObject>)para.getChildObjects())
        {
            TextRange textRange= (TextRange) object;
            textRange.getCharacterFormat().setCharacterSpacing(3f);
        }
        //插入新加段落
        document.getSections().get(0).getParagraphs().insert(1, para);
        //保存文档
        document.saveToFile("result.docx", FileFormat.Docx);
    }
}效果图:

 



 
					



