Knowledgebase (2330)
Children categories
Sometimes in word files, we type another language rather than default, and need spellers and other proofing tools adjust to the language we typed.
This article is talking about how to alter language dictionary as non-default language via Spire.Doc. Here take English as default language and alter to Spanish in Peru as an example.
As for more language information, refer this Link to Microsoft Locale ID Values.
Here are the steps:
Step 1: Create a new word document.
Document document = new Document();
Step 2: Add new section and paragraph to the document.
Section sec = document.AddSection(); Paragraph para = sec.AddParagraph();
Step 3: Add a textRange for the paragraph and append some Peru Spanish words.
TextRange txtRange = para.AppendText("corrige según diccionario en inglés");
txtRange.CharacterFormat.LocaleIdASCII = 10250;
Step 4: Save and review.
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
Here is the result screenshot.

Full Code:
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AlterLang
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section sec = document.AddSection();
Paragraph para = sec.AddParagraph();
TextRange txtRange = para.AppendText("corrige según diccionario en inglés");
txtRange.CharacterFormat.LocaleIdASCII = 10250;
document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");
}
}
}
How to Mix Font Styles within a Single TextRange in C#, VB.NET
2015-05-12 05:10:51 Written by KoohjiTo emphasize some words or phrases in a sentence, most probably you will set different formats such as font type, color, and size to these parts. When we programmatically insert a sentence with various kinds of font styles into a PowerPoint slide, it is an easy task if we format the text at the first place and then append the text to a paragraph. In this article, we attach more importance to introduce how to mix font styles within a single TextRange on an existing PowerPoint slide.
Test File:
As is shown in test file, all words in this TextRange are in the same font style. Now we would like to make some changes.

Code Snippet:
Step 1: Create a new PowerPoint document and load the test file.
Presentation ppt = new Presentation("Sample.pptx", FileFormat.Pptx2010);
Step 2: Get the shape from PowerPoint slide, get the text from shape and save to a new string variable.
IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape; string originalText = shape.TextFrame.Text;
Step 3: Split the string by specified words and return substrings to a string array.
string[] splitArray = originalText.Split(new string[] { "bold", "red","underlined","bigger font size" }, StringSplitOptions.None);
Step 4: Remove the paragraph from TextRange.
TextParagraph tp = shape.TextFrame.TextRange.Paragraph; tp.TextRanges.Clear();
Step 5: Append normal text that is in front of ‘bold’ to the paragraph.
TextRange tr = new TextRange(splitArray[0]); tp.TextRanges.Append(tr);
Step 6: Set font style of the text ‘bold’ as bold, and append it to the paragraph.
tr = new TextRange("bold");
tr.IsBold = TriState.True;
tp.TextRanges.Append(tr);
Step 7: Repeat step 5 and step 6 to append the rest normal texts and formatted texts to the paragraph.
//normal text
tr = new TextRange(splitArray[1]);
tp.TextRanges.Append(tr);
//red text
tr = new TextRange("red");
tr.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
tr.Format.Fill.SolidColor.Color = Color.Red;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[2]);
tp.TextRanges.Append(tr);
//underline text
tr = new TextRange("underlined");
tr.TextUnderlineType = TextUnderlineType.Single;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[3]);
tp.TextRanges.Append(tr);
//bigger size text
tr = new TextRange("bigger font size");
tr.FontHeight = 35;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[4]);
tp.TextRanges.Append(tr);
Step 8: Save the file.
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Output:

Full Code:
using Spire.Presentation;
using System;
using System.Drawing;
namespace MixFontStyle
{
class Program
{
static void Main(string[] args)
{
Presentation ppt = new Presentation("Sample.pptx", FileFormat.Pptx2010);
IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;
string originalText = shape.TextFrame.Text;
string[] splitArray = originalText.Split(new string[] { "bold", "red", "underlined", "bigger font size" }, StringSplitOptions.None);
TextParagraph tp = shape.TextFrame.TextRange.Paragraph;
tp.TextRanges.Clear();
//normal text
TextRange tr = new TextRange(splitArray[0]);
tp.TextRanges.Append(tr);
//bold text
tr = new TextRange("bold");
tr.IsBold = TriState.True;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[1]);
tp.TextRanges.Append(tr);
//red text
tr = new TextRange("red");
tr.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
tr.Format.Fill.SolidColor.Color = Color.Red;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[2]);
tp.TextRanges.Append(tr);
//underline text
tr = new TextRange("underlined");
tr.TextUnderlineType = TextUnderlineType.Single;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[3]);
tp.TextRanges.Append(tr);
//bigger size text
tr = new TextRange("bigger font size");
tr.FontHeight = 35;
tp.TextRanges.Append(tr);
//normal text
tr = new TextRange(splitArray[4]);
tp.TextRanges.Append(tr);
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
}
}
}
Imports Spire.Presentation
Imports System.Drawing
Namespace MixFontStyle
Class Program
Private Shared Sub Main(args As String())
Dim ppt As New Presentation("Sample.pptx", FileFormat.Pptx2010)
Dim shape As IAutoShape = TryCast(ppt.Slides(0).Shapes(0), IAutoShape)
Dim originalText As String = shape.TextFrame.Text
Dim splitArray As String() = originalText.Split(New String() {"bold", "red", "underlined", "bigger font size"}, StringSplitOptions.None)
Dim tp As TextParagraph = shape.TextFrame.TextRange.Paragraph
tp.TextRanges.Clear()
'normal text
Dim tr As New TextRange(splitArray(0))
tp.TextRanges.Append(tr)
'bold text
tr = New TextRange("bold")
tr.IsBold = TriState.[True]
tp.TextRanges.Append(tr)
'normal text
tr = New TextRange(splitArray(1))
tp.TextRanges.Append(tr)
'red text
tr = New TextRange("red")
tr.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid
tr.Format.Fill.SolidColor.Color = Color.Red
tp.TextRanges.Append(tr)
'normal text
tr = New TextRange(splitArray(2))
tp.TextRanges.Append(tr)
'underline text
tr = New TextRange("underlined")
tr.TextUnderlineType = TextUnderlineType.[Single]
tp.TextRanges.Append(tr)
'normal text
tr = New TextRange(splitArray(3))
tp.TextRanges.Append(tr)
'bigger size text
tr = New TextRange("bigger font size")
tr.FontHeight = 35
tp.TextRanges.Append(tr)
'normal text
tr = New TextRange(splitArray(4))
tp.TextRanges.Append(tr)
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010)
End Sub
End Class
End Namespace
Spire.Doc has a powerful ability to operate RTF file formats in C# and VB.NET. By using Spire.Doc, developers can convert RTF to PDF, HTML and word documents in .doc, .docx. This article will show you how to convert RTF into image and then reset the image resolution.
Download and install Spire.Doc for .NET and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to convert RTF into PNG and reset image resolution in C#.
Step 1: Create a new document and load from file.
Document doc = new Document();
doc.LoadFromFile("sample.rtf", FileFormat.Rtf);
Step 2: Save the RTF to image
Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
Step 3: Traverse the elements in the list of images and save them into .Png format.
for (int i = 0; i < images.Length; i++)
{
Metafile mf = images[i] as Metafile;
Image newimage = ResetResolution(mf, 200);
string outputfile = String.Format("image-{0}.png", i);
newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png);
}
Step 4: Set the image resolution call the method: ResetResolution.
public static Image ResetResolution(Metafile mf, float resolution)
{
int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
int height = (int)(mf.Height * resolution / mf.VerticalResolution);
Bitmap bmp = new Bitmap(width, height);
bmp.SetResolution(resolution, resolution);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(mf, Point.Empty);
}
return bmp;
}
Effective screenshot of the image before reset the image resolution:

The image after reset the image resolution:

Full codes:
using Spire.Doc;
using System.Drawing;
using System.Drawing.Imaging;
namespace RTFtoImage
{
class Program
{
static void Main(string[] args)
{
//Create a new document and load from file.
Document doc = new Document();
doc.LoadFromFile("sample.rtf", FileFormat.Rtf);
// save the RTF to image
Image[] images = doc.SaveToImages(Spire.Doc.Documents.ImageType.Metafile);
for (int i = 0; i < images.Length; i++)
{
Metafile mf = images[i] as Metafile;
Image newimage = ResetResolution(mf, 200);
string outputfile = String.Format("image-{0}.png", i);
newimage.Save(outputfile, System.Drawing.Imaging.ImageFormat.Png);
}
}
//set the image resolution by the ResetResolution() method
public static Image ResetResolution(Metafile mf, float resolution)
{
int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
int height = (int)(mf.Height * resolution / mf.VerticalResolution);
Bitmap bmp = new Bitmap(width, height);
bmp.SetResolution(resolution, resolution);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(mf, Point.Empty);
}
return bmp;
}
}
}