Word檔案中可添加文本框,并設定文本框為橫向文本排列或是縱向文本排列,或者設定文本框中的文字旋轉方向等,通過Java程式代碼,也可以實作以上文本框的操作,下面以Java代碼示例展示具體的實作步驟,另外,可參考C#及VB.NET代碼的實作方法,
本次程式測驗環境如下:
Word測驗檔案版本:.docx 2013
Word Jar包工具:free spire.doc.jar 3.9.0
代碼編譯工具:IDEA
Jdk版本:1.8.0
匯入操作檔案所需的jar包工具,如圖結果:

Java
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.TextBox; import com.spire.doc.fields.TextRange; import java.awt.*; public class SetTextDirection { public static void main(String[] args) { //創建Word檔案 Document doc = new Document(); Section section = doc.addSection(); //設定頁面邊距 section.getPageSetup().getMargins().setLeft(90f); section.getPageSetup().getMargins().setRight(90f); Paragraph paragraph = section.addParagraph(); //添加第一個文本框 TextBox textBox1 = paragraph.appendTextBox(280, 250); //設定文本框為固定定位 textBox1.getFormat().setHorizontalOrigin(HorizontalOrigin.Page); textBox1.getFormat().setHorizontalPosition(150); textBox1.getFormat().setVerticalOrigin(VerticalOrigin.Page); textBox1.getFormat().setVerticalPosition(80); //設定文字旋轉方向 textBox1.getFormat().setTextAnchor(ShapeVerticalAlignment.Center); textBox1.getFormat().setLayoutFlowAlt(TextDirection.Left_To_Right);//旋轉文字(逆時針) //textBox1.getFormat().setLayoutFlowAlt(TextDirection.Left_To_Right_Rotated);//文字豎排顯示 //添加文字并設定字體 Paragraph textboxPara1 = textBox1.getBody().addParagraph(); TextRange txtrg = textboxPara1.appendText("姓名_______學號_________班級__________"); txtrg.getCharacterFormat().setFontName("等線"); txtrg.getCharacterFormat().setFontSize(10); txtrg.getCharacterFormat().setTextColor(Color.black); textboxPara1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //保存檔案 doc.saveToFile("Result.docx"); doc.dispose(); } }
執行程式后,生成Word檔案,打開該檔案后可查看文本框中的文字旋轉效果,通過設定不同旋轉效果,可查看文本框中的文字效果,如圖:
Left_To_Right旋轉效果:

Left_To_Right_Rotated豎排顯示效果:

延伸閱讀:
- C# 設定Word文本框中的文字旋轉方向
—End—
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288621.html
標籤:其他
上一篇:Java概論——JavaSE基礎
