word中腳註的編輯方法(在Word文檔中插入上下標並設置文字效果)
2023-08-11 08:31:04 1
Spire.Doc for .NET是一個專業的Word .NET庫,設計用於幫助開發人員高效地開發創建、閱讀、編寫、轉換和列印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。以下示例將詳細講述如何使用Spire.Doc在Word文檔中插入上下標並設置文字效果。
C# Word 插入上下標上下標常見於數學公式、化學表達式等科學公式中。使用Spire.Doc時,用戶可以通過CharacterFormat類中SubSuperScript屬性來將字符(串)設置為上標或下標。
//初始化Document對象,添加section和段落Document doc = new Document;Section sec = doc.AddSection;Paragraph para = sec.AddParagraph;//寫入鈉與水反應的化學反應方程式para.AppendText("2Na 2H");para.AppendText("2").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;//2作為下標para.AppendText("=2NaOH H");para.AppendText("2").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;//2作為下標para.AppendBreak(BreakType.LineBreak);//換行//寫入公式a^2 b^2=c^2para.AppendText("a");para.AppendText("2").CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;//2作為上標para.AppendText(" b"); para.AppendText("2").CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;//2作為上標para.AppendText("=c");para.AppendText("2").CharacterFormat.SubSuperScript = SubSuperScript.SuperScript;//2作為上標para.AppendBreak(BreakType.LineBreak);//換行//寫入公式an=Sn-Sn-1para.AppendText("an");para.AppendText("=S");para.AppendText("n").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;//n作為下標para.AppendText("-S");para.AppendText("n-1").CharacterFormat.SubSuperScript = SubSuperScript.SubScript;//n-1作為下標 //設置字體大小foreach (var i in para.Items){ if (i is TextRange) { (i as TextRange).CharacterFormat.FontSize = 20; }}//保存文檔doc.SaveToFile("output.docx");
效果圖如下:
下面這段示例將介紹如何使用Spire.Doc來設置文字效果,包括文本邊框,文本填充,文字陰影等。
//實例化一個Document對象Document doc = new Document;//向文檔中添加一個Section對象Section sec = doc.AddSection;//在這個section上添加一個段落並給文字加邊框Paragraph p1 = sec.AddParagraph; TextRange tr1 = p1.AppendText("加粉色邊框的文字");tr1.CharacterFormat.Border.BorderType = BorderStyle.DashDotStroker;tr1.CharacterFormat.Border.Color = Color.Pink;p1.AppendBreak(BreakType.LineBreak);//添加一個新段落並設置文字填充效果Paragraph p2 = sec.AddParagraph;TextRange tr2 = p2.AppendText("設置填充效果的文字");//設置文字前景色tr2.CharacterFormat.TextColor = Color.Orange;//設置文字背景色tr2.CharacterFormat.TextBackgroundColor = Color.LightGray;//設置文字縮放比例tr2.CharacterFormat.TextScale = 150;p2.AppendBreak(BreakType.LineBreak);//添加一個新段落並設置陰影Paragraph p3 = sec.AddParagraph;TextRange tr3 = p3.AppendText("設置陰影效果的文字");tr3.CharacterFormat.TextColor = Color.LightSeaGreen;tr3.CharacterFormat.IsShadow = true;p3.AppendBreak(BreakType.LineBreak);//添加一個新段落並設置簡單的文字樣式Paragraph p4 = sec.AddParagraph;TextRange tr4 = p4.AppendText("設置刪除線效果的文字");tr4.CharacterFormat.IsStrikeout = true;p4.AppendBreak(BreakType.LineBreak);TextRange tr5 = p4.AppendText("設置文字大寫: hello, e-iceblue.");tr5.CharacterFormat.IsSmallCaps = true;//使用ClearFormatting來刪除某個TextRange的文字效果//tr5.CharacterFormat.ClearFormatting;//保存文檔 doc.SaveToFile("文字效果.docx");
效果圖如下:
如果你有任何問題或意見,或者想要獲得更多教程資源,可在下方評論區留言哦~
,