Formatting text using word.interop in outlook 2013 add-in - c#

i'm trying to format text that i'm inserting at the beginning of a new email when a user presses a button in the ribbon add-in
the code is simple in that it first checks if the type of text was previously inserted, removes it if it was, and then inserts a new bracket of text using Word.Range.InsertBefore(.....);
the text will be:
CLASSIFICATION: .......
CLASSIFICATION: .......
the problem that i am running into is after the insert i'm formatting the font using Range.Font,Text etc...
i need the CLASSIFICATION: ...... formatted and not the space between them
the space between the "CLASSIFICATION......" is being formatted when typing begins to the same size, color, etc.... as the "CLASSIFICATION"
the code i'm using is:
private void setClassificationText(string classificationText, Word.WdColorIndex color)
{
//current mail item document
Word.Document document = (Word.Document)mailItem.GetInspector.WordEditor;
Word.Range rng = document.Paragraphs[1].Range;
//check if a classification level was already supplied
if (checkIfClassificationExists(rng))
{
//get the first 2 Paragraphs and remove the text from them
int paraCount = 2;
for (int i = 1; i <= paraCount; i++)
{
Word.Range classificationRange = document.Paragraphs[i].Range;
removeTextFromParagraph(classificationRange);
}
rng.Collapse();
rng = document.Paragraphs[1].Range;
}
rng.InsertBefore(classificationText + CT.lineFeedStr5 + CT.classification + classificationText + CT.lineFeedStr3);
//sets the color and text
rng.Font.ColorIndex = color;
rng.Text = CT.classification + rng.Text;
rng.Font.Bold = 1;
}
private void removeTextFromParagraph(Word.Range rng)
{
int wordCount = rng.Words.Count;
rng.Delete(Count: wordCount);
}

solved the issue i was having.
i used the following:
private void setClassificationText(string classificationText, Word.WdColorIndex color)
{
//current mail item document
Word.Document document = (Word.Document)mailItem.GetInspector.WordEditor;
Word.Range rng = document.Paragraphs[1].Range;
//check if a classification level was already supplied
if (checkIfClassificationExists(rng))
{
//get the first 2 Paragraphs and remove the text from them
int paraCount = 2;
for (int i = 1; i <= paraCount; i++)
{
Word.Range classificationRange = document.Paragraphs[i].Range;
removeTextFromParagraph(classificationRange);
}
rng.Collapse();
rng = document.Paragraphs[1].Range;
}
//insert the text
rng.InsertBefore(classificationText + CT.lineFeedStr5 + CT.classification + classificationText);
//sets the color and text
rng.Font.ColorIndex = color;
rng.Text = CT.classification + rng.Text;
rng.Font.Bold = 1;
//get the beginning and ending positions of the blank space
startPosition = rng.Start + getClassificationLength(classificationText);
endPosition = (int)startPosition + CT.lineFeedStr5.Length-1;
//get the Word.Range for the blank space
Word.Range rngNormal = document.Range(ref startPosition, endPosition);
//set blank space to be normal font (not bold and black)
rngNormal.Bold = 0;
rngNormal.Font.ColorIndex = Word.WdColorIndex.wdBlack;
}
private int getClassificationLength(string classificationText)
{
int returnValue = 0;
returnValue = CT.classification.Length + classificationText.Length;
return returnValue;
}
hope this can help someone else out.

Related

Set caret at an index in RichTextBox WPF

I am trying to set the position of caret in richtextbox based on index position of a word. Even though I am able to change the caret position, the caret does not move to the correct location.
Here is my sample code:
private void Button_Click(object sender, RoutedEventArgs e)
{
RTB_Main.Document.Blocks.Clear();
for (int i = 0; i < 10; i++)
{
Paragraph para = new Paragraph(new Run(i + ""));
RTB_Main.Document.Blocks.Add(para);
}
TextRange richText = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd);
string searchText = tb_Search.Text; // 1 to 9
int position = Regex.Match(richText.Text, searchText).Index;
RTB_Main.CaretPosition = RTB_Main.Document.ContentStart;
RTB_Main.CaretPosition = RTB_Main.CaretPosition.GetPositionAtOffset(position);
RTB_Main.Focus();
}
What is wrong with this approach?
Also, Please let me know if there is a better way to set the caret position to an index?
The problem in my case was caused by new line characters \r\n. I just replaced these with another characters and it worked for me. Note that I am replacing them with not 2 characters but 4.
private void Button_Click(object sender, RoutedEventArgs e)
{
RTB_Main.Document.Blocks.Clear();
for (int i = 0; i < 10; i++)
{
Paragraph para = new Paragraph(new Run(i + ""));
RTB_Main.Document.Blocks.Add(para);
}
TextRange richText = new TextRange(RTB_Main.Document.ContentStart, RTB_Main.Document.ContentEnd);
string searchText = tb_Search.Text; // 1 to 9
string tmpStr = richText.Text.Replace("\r\n", "....");
int position = Regex.Match(tmpStr, searchText).Index;
RTB_Main.CaretPosition = RTB_Main.Document.ContentStart;
RTB_Main.CaretPosition = RTB_Main.CaretPosition.GetPositionAtOffset(position);
RTB_Main.Focus();
}
As Maciek noted, there are invisible formatting items that affects the count. My code adds a feedback loop because we are able to ask what the true caret position is. It feels hacky but I could not find anything better.
public static void SetCaretPositionOfRichTextBoxToCharIndex(
System.Windows.Controls.RichTextBox box, int charIndex)
{
// RichTextBox contains many formattings, and they, although invisible, count
// when setting CaretPosition. Calling GetPositionAtOffset with charIndex from
// DocumentStart can be less than the necessary CaretPosition. This code
// therefore has a feedback loop to see how much more offset is necessary.
box.CaretPosition = box.CaretPosition.DocumentStart;
int attemptedCharIndex = 0;
int fixerInc = 0;
while (attemptedCharIndex < charIndex)
{
box.CaretPosition = box.CaretPosition.GetPositionAtOffset(charIndex - attemptedCharIndex + fixerInc);
int temp = new TextRange(box.Document.ContentStart, box.CaretPosition).Text.Length;
if (attemptedCharIndex == temp)
{
fixerInc++;
}
else
{
fixerInc = 0
}
attemptedCharIndex = temp;
}
}

Richtextbox prepend new text with color

I have used a richtextbox to display logs in my WinForms.
Language used is C#.
The software is used for inserting data of Bank Branches and after start of new Branch I want to display a text with new color.
I have seen the link Color different parts of a RichTextBox string and implemeted it successfully.
My problem is I want to prepend the new line instead of append. That is the new line will be displayed on top.
I am able to do this by changing the code to box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.Text
But the color is changing for the entire text.
This is the procedure used for append
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text);
box.SelectionColor = box.ForeColor;
This is what I have done:
box.Text=DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ": " + text + box.text;
box.SelectionStart = 0;
box.SelectionLength = text.length;
box.SelectionColor = color;
But this is not working.
1) Never directly change the Text property of an already formatted RichtTextBox
2) To append use the RTB.AppendText function
3) To insert at any other position p, including the beginning use this:
rtb.SelectionStart = s; // set the cursor to the target position
rtb.Selection.Length = 0; // nothing selected, yet
rtb.SelectedText = yourNewText; // this inserts the new text
Now you can add the formatting you want:
rtb.SelectionStart = s; // now we prepare the new formatting..
rtb.SelectionLength = yourNewText.Length; //.. by selecting the text
rtb.SelectionColor = Color.Blue; // and/or whatever you want to do..
...
// Prepend, normal on first line, rest of lines gray italic
private void PrependToRichTextbox(RichTextBox rt, string msg)
{
rt.SelectionStart = 0;
rt.SelectionLength = 0;
rt.SelectedText = msg + Environment.NewLine;
int linecount = 0;
foreach (var line in rt.Lines)
{
rt.Select(rt.GetFirstCharIndexFromLine(linecount), line.Length);
rt.SelectionColor = linecount == 0 ? Color.Black : Color.Gray;
Font currentFont = rt.SelectionFont;
FontStyle newFontStyle;
newFontStyle = linecount == 0 ? FontStyle.Regular : FontStyle.Italic;
rt.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
linecount++;
}
}

Why does turning bold off not work when adding new text from C#?

I want to add text from C# to a Winword document and turn bold on an off.
Using this, abc is displayed bold. Bold then is programmatically turned off, so the following text appears unbold when typed in Winword directly.
{
objWinWordControl.document.Application.Selection.Font.Bold = 1;
objWinWordControl.document.Application.Selection.Text = "abc";
objWinWordControl.document.Application.Selection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove);
objWinWordControl.document.Application.Selection.Font.Bold = 0;
// continue typing text in Winword now, the text isn't bold
}
Here I get bold abcxyz, although bold has been turned off.
{
objWinWordControl.document.Application.Selection.Font.Bold = 1;
objWinWordControl.document.Application.Selection.Text = "abc";
objWinWordControl.document.Application.Selection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove);
objWinWordControl.document.Application.Selection.Font.Bold = 0;
// this text is bold, although bold had been turned off
objWinWordControl.document.Application.Selection.Text = "xyz";
}
Why is "xyz" bold when added programmatically?
I found a workaround like this now:
{
object start = 0;
object end = 0;
Word.Range rng = objWinWordControl.document.Range(ref start, ref end);
rng.Text = "abc";
rng.Font.Bold = 1;
start = rng.Text.Length;
end = rng.Text.Length;
rng = objWinWordControl.document.Range(ref start, ref end);
rng.Text = "xyz";
rng.Font.Bold = 0;
}
but compared to:
objWinWordControl.document.Application.Selection.Text = "abc";
it has a very poor performance.

How to get text from slide in C# using Aspose

i am getting all shapes in slides of ppt file now i want to get text from those shapes how can i do this
here is my method where i am getting shapes of all slides in ppt file
public void Main(string[] args)
{
// The path to the documents directory.
string dataDir = Path.GetFullPath(#"C:\Users\Vipin\Desktop\");
//Load the desired the presentation
Presentation pres = new Presentation(dataDir + "Android.ppt");
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Accessing a slide using its slide index
int slideCount = prestg.Slides.Count();
for (int i = 0; i <= slideCount - 1; i++)
{
ISlide slide = pres.Slides[i];
foreach (IShape shap in slide.Shapes)
{
int slideCountNumber = i + 1;
float shapeHeight = shap.Frame.Height;
float shapeWidth = shap.Frame.Width;
Debug.Write("slide Number: " + slideCountNumber + " shape width = " + shapeWidth + " shapeHeight = " + shapeHeight);
}
}
}
}
now ho can i get the text from it
aspose will give u truncated text if u don't have the license of it. so it will be better for you if you will use Microsoft.Office.Interop.PowerPoint
use as below
public void ReadSlide(){
string filePath= #"C:\Users\UserName\Slide.pptx";
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
string presentation_textforParent = "";
foreach (var item in presentation.Slides[1].Shapes)
{
var shape = (Microsoft.Office.Interop.PowerPoint.Shape)item;
if (shape.HasTextFrame == MsoTriState.msoTrue)
{
if (shape.TextFrame.HasText == MsoTriState.msoTrue)
{
var textRange = shape.TextFrame.TextRange;
var text = textRange.Text;
presentation_textforParent += text + " ";
}
}
}
}
You may want to extract text not from all shapes, but from text frames instead. In order to do this use the GetAllTextFrames static method exposed by the PresentationScanner class
using (Presentation prestg = new Presentation(dataDir + "Android.ppt"))
{
//Get an Array of ITextFrame objects from all slides in the PPTX
ITextFrame[] textFramesPPTX = Aspose.Slides.Util.SlideUtil.GetAllTextFrames(pptxPresentation, true);
//Loop through the Array of TextFrames
for (int i = 0; i < textFramesPPTX.Length; i++)
//Loop through paragraphs in current ITextFrame
foreach (IParagraph para in textFramesPPTX[i].Paragraphs)
//Loop through portions in the current IParagraph
foreach (IPortion port in para.Portions)
{
//Display text in the current portion
Console.WriteLine(port.Text);
//Display font height of the text
Console.WriteLine(port.PortionFormat.FontHeight);
//Display font name of the text
if (port.PortionFormat.LatinFont != null)
Console.WriteLine(port.PortionFormat.LatinFont.FontName);
}
See documentation

Highlight parts of text in FlowDocument

I want to highlight some parts of text in a FlowDocument based on the search results. What I am doing is getting the indexes where the searched word occures in the text of the FlowDocument and then apply background color on the text range starting at the found index, ending at the found index + search word length.
TextRange content = new TextRange(myFlowDocument.ContentStart,
myFlowDocument.ContentEnd);
List<int> highlights = GetHighlights(content.Text, search);
foreach (int index in highlights)
{
var start = myFlowDocument.ContentStart;
var startPos = start.GetPositionAtOffset(index);
var endPos = start.GetPositionAtOffset(index + search.Length);
var textRange = new TextRange(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty,
new SolidColorBrush(Colors.Yellow));
}
TextRange newRange = new TextRange(myFlowDocument.ContentStart,
newDocument.ContentEnd);
FlowDocument fd = (FlowDocument)XamlReader.Parse(newRange.Text);
The problem is, that I am searching indexes in the text of the document but when I'm returning the FlowDocument the xaml tags are added and I see the highlights moved.
How can I fix it?
Finaly, inspired by the answer of user007, after making some modifications I managed to highlight all the occurences of a string in a FlowDocument. Here is my code:
for (TextPointer position = newDocument.ContentStart;
position != null && position.CompareTo(newDocument.ContentEnd) <= 0;
position = position.GetNextContextPosition(LogicalDirection.Forward))
{
if (position.CompareTo(newDocument.ContentEnd) == 0)
{
return newDocument;
}
String textRun = position.GetTextInRun(LogicalDirection.Forward);
StringComparison stringComparison = StringComparison.CurrentCulture;
Int32 indexInRun = textRun.IndexOf(search, stringComparison);
if (indexInRun >= 0)
{
position = position.GetPositionAtOffset(indexInRun);
if (position != null)
{
TextPointer nextPointer = position.GetPositionAtOffset(search.Length);
TextRange textRange = new TextRange(position, nextPointer);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty,
new SolidColorBrush(Colors.Yellow));
}
}
}
You need to iterate using GetNextContextPosition(LogicalDirection.Forward) and get TextPointer, use this one with previous TextPointer to construct TextRange. On this TextRange you can apply your logic.
What you can't do is use single TextRange from FlowDocument for searching text. FlowDocument is not only text:
private void Button_Click(object sender, RoutedEventArgs e)
{
String search = this.content.Text;
TextPointer text = doc.ContentStart;
while (true)
{
TextPointer next = text.GetNextContextPosition(LogicalDirection.Forward);
if (next == null)
{
break;
}
TextRange txt = new TextRange(text, next);
int indx = txt.Text.IndexOf(search);
if (indx > 0)
{
TextPointer sta = text.GetPositionAtOffset(indx);
TextPointer end = text.GetPositionAtOffset(indx + search.Length);
TextRange textR = new TextRange(sta, end);
textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
}
text = next;
}
}
UPDATE:
it does not work allways, for example if you have list, special characters (\t) are counted by IndexOf, but GetPositionAtOffset doesn't count them in:
• ListItem 1
• ListItem 2
• ListItem 3
this line:
int indx = txt.Text.IndexOf(search);
could be replaced with:
int indx = Regex.Replace(txt.Text,
"[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled).IndexOf(search);
kzub's answer did not seem to work for me so I also created a solution expanding on user007 to highlight all instances of a substring in TextPointer text. It also ignores case and will highlight all matches:
public static void HighlightWords(TextPointer text, string searchWord, string stringText)
{
int instancesOfSearchKey = Regex.Matches(stringText.ToLower(), searchWord.ToLower()).Count;
for (int i = 0; i < instancesOfSearchKey; i++)
{
int lastInstanceIndex = HighlightNextInstance(text, searchWord);
if (lastInstanceIndex == -1)
{
break;
}
text = text.GetPositionAtOffset(lastInstanceIndex);
}
}
private static int HighlightNextInstance(TextPointer text, string searchWord)
{
int indexOfLastInstance = -1;
while (true)
{
TextPointer next = text.GetNextContextPosition(LogicalDirection.Forward);
if (next == null)
{
break;
}
TextRange newText = new TextRange(text, next);
int index = newText.Text.ToLower().IndexOf(searchWord.ToLower());
if (index != -1)
{
indexOfLastInstance = index;
}
if (index > 0)
{
TextPointer start = text.GetPositionAtOffset(index);
TextPointer end = text.GetPositionAtOffset(index + searchWord.Length);
TextRange textRange = new TextRange(start, end);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
}
text = next;
}
return indexOfLastInstance;
}

Categories