I'm looking for a code-how that can bold selective words and/or lines of text within one of WPF's RichTextBoxes, so that, for example, I could have code that ensures the word bananabread would be automatically set to bold whenever it were typed. Another example of this could be setting an entire line to bold if it begins with the word "Therefore."
I'm aware that you can bold specific lines via XAML, and I know that pressing Ctrl + B can bold specific words while in the editor itself, but how can I go about doing this through C#?
Related
I have tried both the regular TextBox and RichTextBox in an app where I don't want any word wrap. Yes, of course I turned off word wrap in Properties. This is something that occurs at extremely long lines, like around 3000 chars or so in the RichTextBox (regular TextBox much sooner, but I can get by without it if RichTextBox works). I recognize that it is cruel and unusual punishment to subject the poor RichTextBox to such line lengths, but unavoidable. Still, it seems like abnormal behavior.
The text is just long continuous strings of repetitive ascii (genetic data), so nothing unusual there. In fact it's usually just A,C,T,G with no punctuation or spaces.
Is this known behavior?
Assuming you are using WinForms, both RichTextBox and TextBox have built-in limits for lengths of lines.
An alternative is to use a scroll viewer and manually add Labels. I do not believe there will be any limit in this scenario, although I could be wrong.
If you check Notepad (notepad.exe) in Windows, it also has a limit before it starts wrapping text, so this is standard behavior for Windows apps.
the Multiline property should be false in order for the lines not to wrap.
You could try a RichTextBox and extend the right margin to the width of the text.
richTextBox1.RightMargin = TextRenderer.MeasureText(yourString, this.richTextBox1.Font).Width;
This way you maintain the ability to have other lines.
I have two consecutive paragraphs, the second has some text formatted italics. I add the text of the second paragraph to the first by setting the range of the first paragraph then using this code:
Paragraph nextPar = firstPar.Next();
Range nextRange = nextPar.Range;
firstRng.InsertAfter(nextRange.Text.ToString());
This works well but it removes the italic from the text of the second paragraph. I want a way to keep formatting.
Italics (or bold or any other style) is applied to the whole range. With InsertAfter, you are just putting more text in the same range and thus implicitly accepting the given format. You have to rely on two different ranges to allow italics/non italics.
Just keep adding paragraphs and account for as many different ranges as different styles you want. Here you have examples showing how to include different styles (ranges) in the same line.
In my project I want to count lines displayed by richtextbox in C# with word wrap property set true. For example, in richtextbox, I write a text, and when text reaches the width of the richtextbox, then it automatically goes to second line. So what happens is the text contains actually one line but it is displayed in two lines. So I want to know how can I count this as two lines instead of one? Help will be really appreciated.
You may go for RichTextBox.GetLineFromCharIndex Method
Retrieves the line number from the
specified character position within
the text of the RichTextBox control. If WordWrap is set to false, no portion of the line wraps to the next, and the method returns 0 for the specified character index.
Also, check out this link.
I have to put the data from a pdf file in a certain database structure. This requires me to be able to get certain data out of the pdf file. Since pdf hasn't got any tags etc ... i was wondering if it is possible to get text based on a color. Say for example i want all the red text. Or i want all the italic text in the document. Is this possible in C# ? Or is there an other way to easily filter data in a pdf document ?
I've taken a different approach. I converted the pdf to an excel file. And this was very easy to search for the coloured text
By using this library http://www.codeproject.com/KB/files/xpdf_csharp.aspx?msg=3154408
you have an access to every word style (font, color...)
this.pdfDoc.Pages[4].WordList.ElementAt(143).ForeColor
iText's PdfTextExtractor (and all the code it rests on) DOES NOT track the current color. Ouch. It wouldn't be all that hard to add, so you could modify iText yourself:
Add stroke and fill color members to the GraphicState class (and update the various constructors appropriately).
You'd need to add ContentOperator classes for 'g', 'G', 'rg', 'RG', 'K', and 'k' (and maybe CS, cs, SC, sc, SCN, scn), to modify the stroke and fill colors.
Add methods to TextRenderInfo to get the current stroke and fill colors.
Try PdfLibTET http://www.pdflib.com/products/tet/
It should be able to get informations about text.
Basically, I was able to successfully generate a multiple page PDF (from a string read from a .txt file) using the MigraDoc sample here:
http://www.pdfsharp.net/wiki/MigraDocHelloWorld-sample.ashx
The problem is that in the original text there are tabs and whitespace that yields a document with centered text, as well as sections of text separated by spaces. It looks this way in notepad as well as in the string viewer in Visual Studio.
When the PDF is generated, everything is left justified and all of the tabs and extra whitespace has been removed.
Given the sample in the above link, how can I keep the originally white space? It seems like if notepad can render it correctly and the string reads in correctly in C#, the PDF generated should also look exactly the same.
MigraDoc is like HTML: multiple spaces get merged into one space.
To get centred text with MigraDoc, just set the paragraph to centre alignment. IMHO that is the best way.
To retain multiple spaces in pre-formatted text just replace the spaces with non-breaking spaces.
See also:
https://stackoverflow.com/a/19301602/1015447