Spire.Doc for .NET (332)
Children categories
Capitals are quite suitable for stressing texts in Word. A paragraph written with capitalized letters is easy to notice, and capitalized letters imply the importance of the paragraph. This article teaches you the method of changing the case of existing text to all capitals with Spire.Doc for .NET by programming.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Change the Case of a Specified Paragraph to All Capitals
The detailed steps are as follows:
- Create an object of Document and load a sample Word document from file using Document.LoadFromFile() method.
- Get the second paragraph using Document.Sections[].Paragraph[] property and set its characters to AllCaps through TextRange.CharacterFormat.AllCaps property.
- Get the third paragraph using Document.Sections[].Paragraph[] property and set its characters to IsSmallCaps by TextRange.CharacterFormat.IsSmallCaps property.
- Save the document to a new Word file using Document.SaveToFile() method.
Note: AllCaps means to capitalize all the letters and set them to the same size, and IsSmallCaps means to capitalize all the letters but set the original majuscules bigger than the minuscules.
- C#
- VB.NET
using System; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace changecase { class Program { static void Main(string[] args) { //Create a new document and load from file string input = @"D:\testp\test.docx"; ; Document doc = new Document(); doc.LoadFromFile(input); TextRange textRange; //Get the second paragraph and set its characters to AllCaps Paragraph para1 = doc.Sections[0].Paragraphs[2]; foreach (DocumentObject obj in para1.ChildObjects) { if (obj is TextRange) { textRange = obj as TextRange; textRange.CharacterFormat.AllCaps = true; } } //Get the third paragraph and set its characters to IsSmallCaps Paragraph para2 = doc.Sections[0].Paragraphs[3]; foreach (DocumentObject obj in para2.ChildObjects) { if (obj is TextRange) { textRange = obj as TextRange; textRange.CharacterFormat.IsSmallCaps = true; } } //Save the document to a new Word file string output = "ChangeCase.docx"; doc.SaveToFile(output, FileFormat.Docx2013); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Usually there are three kinds of alignment style for a word table: left aligned, centered and right aligned. On Microsoft word, we can go to table properties to set the alignment for the whole table. Spire.Doc also offers a property table.TableFormat.HorizontalAlignment to enable developers to set the table alignment style easily in C#. This article will demonstrate how to align a table in C#.
Firstly, view the how to align a table for Microsoft word:
Here come to the code snippet of how Spire.Doc align a table.
Step 1: Create a word document and load from file.
Document doc = new Document(); doc.LoadFromFile("sample.docx");
Step 2: Get the first section and two tables from the word document.
Section section = doc.Sections[0]; Table table = section.Tables[0] as Table; Table table1 = section.Tables[1] as Table;
Step 3: Set the different alignment properties for each table.
table.TableFormat.HorizontalAlignment = RowAlignment.Right; table.TableFormat.LeftIndent = 34; table1.TableFormat.HorizontalAlignment = RowAlignment.Left; table1.TableFormat.LeftIndent = 34;
Step 4: Save the document to file:
doc.SaveToFile("result.docx", FileFormat.Docx);
Effective screenshots after align the table format:
Full codes:
using Spire.Doc; using Spire.Doc.Documents; namespace AlignTable { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("sample.docx"); Section section = doc.Sections[0]; Table table = section.Tables[0] as Table; Table table1 = section.Tables[1] as Table; table.TableFormat.HorizontalAlignment = RowAlignment.Right; table.TableFormat.LeftIndent = 34; table1.TableFormat.HorizontalAlignment = RowAlignment.Left; table1.TableFormat.LeftIndent = 34; doc.SaveToFile("result.docx", FileFormat.Docx); } } }
Change the color or remove underline from hyperlink in Word with C#
2016-05-12 08:14:23 Written by support iceblueBy default, hyperlink in Word shows up as blue and underlined. In some cases, users may want to modify the hyperlink style so as to get better looking with the whole document. This article is going to introduce how we can remove the underline or change the color of hyperlinks using Spire.Doc in C#.
Code Snippets:
Step 1: Create a new object of Document class, add a section to it.
Document document = new Document(); Section section = document.AddSection();
Step 2: Add a paragraph and append a hyperlink to the paragraph. In order to format the hyperlink, we return the value of hyperlink in a TextRange.
Paragraph para= section.AddParagraph(); TextRange txtRange = para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
Step 3: Format the hyperlink with the specified the font name, font size, color and underline style.
txtRange.CharacterFormat.FontName = "Times New Roman"; txtRange.CharacterFormat.FontSize = 12; txtRange.CharacterFormat.TextColor = System.Drawing.Color.Red; txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
Step 4: Save the file.
document.SaveToFile("result.docx", FileFormat.Docx2013);
Output:
Full Code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace FormatHyperlink { class Program { static void Main(string[] args) { Document document = new Document(); Section section = document.AddSection(); Paragraph para1= section.AddParagraph(); para1.AppendText("Regular Link: "); TextRange txtRange1 = para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink); txtRange1.CharacterFormat.FontName = "Times New Roman"; txtRange1.CharacterFormat.FontSize = 12; Paragraph blankPara1 = section.AddParagraph(); Paragraph para2 = section.AddParagraph(); para2.AppendText("Change Color: "); TextRange txtRange2 = para2.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink); txtRange2.CharacterFormat.FontName = "Times New Roman"; txtRange2.CharacterFormat.FontSize = 12; txtRange2.CharacterFormat.TextColor = System.Drawing.Color.Red; Paragraph blankPara2 = section.AddParagraph(); Paragraph para3 = section.AddParagraph(); para3.AppendText("Remove Underline: "); TextRange txtRange3 = para3.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink); txtRange3.CharacterFormat.FontName = "Times New Roman"; txtRange3.CharacterFormat.FontSize = 12; txtRange3.CharacterFormat.UnderlineStyle = UnderlineStyle.None; document.SaveToFile("result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result.docx"); } } }