Saving a RichTextBox to a RTF File - wpf

For a project I am working on I am trying to save the contents of a WPF RichTextBox to a RTF file as the title states. I have it working for the most part. However, the file does not preserve newlines. When I save the file ( as you will see below ) it will save everything to one line in the RTF. You can see how it is saved below.
private void butSaveHistory_Click( object sender, RoutedEventArgs e ) {
Microsoft.Win32.SaveFileDialog myDlg = new Microsoft.Win32.SaveFileDialog();
myDlg.DefaultExt = "*.rtf";
myDlg.Filter = "RTF Files|*.rtf";
Nullable<bool> myResult = myDlg.ShowDialog();
if ( myResult == true ) {
/*using ( FileStream myStream = new FileStream( myDlg.FileName, FileMode.OpenOrCreate, FileAccess.Write ) ) {
TextRange myRange = new TextRange( rtbTraffic.Document.ContentStart, rtbTraffic.Document.ContentEnd );
myRange.Save( myStream, DataFormats.Rtf );
myStream.Close();
}*/
rtbTraffic.SelectAll();
rtbTraffic.Selection.Save( new FileStream( myDlg.FileName, FileMode.OpenOrCreate, FileAccess.Write ), DataFormats.Rtf );
}
}
AS you can see I tried two different ways. ( One is commented out ) Neither of these work, they both just save everything to one line when everything in the RichTextBox is on multiple lines.
So how can I get the file to save to multiple lines? Any tips or suggestions would be greatly appreciated.
Note: When saving to a .txt file it saves correctly to multiple lines. However, I cannot save to .txt because the colors and fonts of the RichTextBox need to be preserved.
Edit: This is a sample of what the RichTextBox looks like.
After saving the file it looks like this when i open the RTF in word.
This is what I want it to output.
Edit 2: Adding RTF code
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 Arial;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red0\green128\blue0;\red0\green0\blue255;\red255\green0\blue0;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\b\cf0 \cf0\ql{\f3 {\b0\cf2\highlight1\ltrch 09:10:48 | Thing | STATUS Tube_Heat_Consumer,TUBE_HEAT,3.14209:11:47 | Thing | STATUS Tube_Heat_Consumer,TUBE_HEAT,2.718}{\b0\cf3\highlight1\ltrch 09:58:49 | Thing | STOP STOP}{\b0\cf2\highlight1\ltrch 09:58:49 | Thing | STOP STOP}{\b0\highlight1\ltrch 09:58:57 | Thing | DeRegistration Successful}{\b0\cf4\highlight1\ltrch 09:58:58 | Thing | DeRegistration Failed ( Application is not currently registered ) | 81270401}{\b0\highlight1\ltrch 09:58:58 | Thing | Registration Successful}\li0\ri0\sa0\sb0\fi0\ql\par}
}
} Thing | DeRegistration Failed ( Application is not currently registered ) | 81270401}{\b0\highlight1\ltrch 08:55:21 | Thing | Registration Successful08:55:22 | Thing | DeRegistration Successful08:55:22 | Thing | Registration Successful}{\b0\cf2\highlight1\ltrch 08:55:22 | Thing | Registration Failed ( Application Thing already registered ) | 8127040008:55:22 | Thing | Registration Failed ( Application Thing already registered ) | 81270400}{\b0\highlight1\ltrch 08:55:23 | Thing | DeRegistration Successful08:55:23 | Thing | Registration Successful08:55:24 | Thing | DeRegistration Successful08:55:24 | Thing | Registration Successful08:55:25 | Thing | DeRegistration Successful}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}
From the looks of it it is not encoding \par or \lines correctly.

It will be more simple to answer if you put some example of rtf code you try to save, the result you have and what you want.
I'm not sure of what you want to be in multiple line. The rtf in a more easiest "reading" format for human? Where one line in richtextbox is on one line in rtf format like this:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1036\fs18\f2\cf0 \cf0\ql
{\f2 {\ltrch aaaaa}\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\ltrch bbbb}\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\ltrch }\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\ltrch cccc}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}
in richtextbox I see this
aaaa
bbbb
cccc
I'm not sure of the interest to do that, because newline is encode by \par for end of paragraphs and \line for new line in the current paragraph then when you open the rtf file with a program (MS Word for example) newlines are respected.
And I obtain the same with this:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1036\fs18\f2\cf0 \cf0\ql {\f2 {\ltrch aaaaa}\li0\ri0\sa0\sb0\fi0\ql\par}{\f2 {\ltrch bbbb}\li0\ri0\sa0\sb0\fi0\ql\par}{\f2 {\ltrch }\li0\ri0\sa0\sb0\fi0\ql\par}{\f2 {\ltrch cccc}\li0\ri0\sa0\sb0\fi0\ql\par}}}

Don't add the rtf code directly, use Wpf Paragraph and so on.
When you add your new line to RichTextBox use
YourParagraph.Inlines.Add(new LineBreak());
instead of \r\n or something like this.

As you can see in this post, if you add text as plaintext programaticaly to the RichTextBox control, you may have to add this unicode line separator \u2028. And it will normaly converted to \line in the Rtf saved file.
And I recommend you to use the method with TextRange.

Related

Identifying parts within a link and reformat them in the embed

I am trying to create command that identifies the sitename and the username from the link. For example using !link https://en.wikipedia.org/wiki/User:Example, !link https://en.wikipedia.org/wiki/Special:Contributions/Example, or !link https://en.wikipedia.org/wiki/User_talk:Example would create an embed of
const embed = new Discord.MessageEmbed()
.setColor('#bf00ff')
.addTitle(`en.wikipedia.org`, [Wiki link](https://en.wikipedia.org/wiki/Main_Page))
.addField(`User info of Example`, `[Userpage](https://en.wikipedia.org/wiki/User:Example) | [User_talk](https://en.wikipedia.org/wiki/User_talk:Example) | [Logs](https://en.wikipedia.org/wiki/Special:Log/Example) | [User groups](https://en.wikipedia.org/wiki/Special:UserRights/Example) | [User contributions](https://en.wikipedia.org/wiki/Special:Contributions/Example) | [BlockLog](https://en.wikipedia.org/w/index.php?title=Special:Log/block&page=User:Example)
message.channel.send(embed)
Also, if the link doesn't have User:, Special:Contributions/, or User_talk:, as well as the sitename (e.g. en.wikipedia.org), and the username within it, then create an error message.
If the link contains ?useskin=monobook, ?useskin=vector, ?useskin=modern, or ?useskin=apex, which is after the username, ignore those part in the link. For example, https://en.wikipedia.org/wiki/User_talk:Example?useskin=vector should be interpreted as https://en.wikipedia.org/wiki/User_talk:Example, and the username should also be interpreted as Example, not Example?useskin=vector
How could I accomplish that?

API for getting contact info about College or University?

There are lots of resources for getting name, website, accreditation info, etc for higher education institutions such as the College Scorecard API or IPEDS. What I need for this project is to find a general contact information email address for my set of a few hundred colleges and universities. I plan on using Google Sheets and Apps Script to collect this data.
I have a spreadsheet such as
| Name | URL | City | State | Email |
|-----------------------|----------------------|-----------------|-------|-------|
| Prairie State College | www.prairiestate.edu | Chicago Heights | IL | |
| Langston University | www.langston.edu | Langston | OK | |
where I need to fill in the "email" column. Does anyone know any APIs that could help with that? A quick Google search tells me that ldickerson#prairiestate.edu is the contact at Prairie State College, whereas the admissions page of Langston University has luadmissions#langston.edu right on it. Does anyone have any ideas on where I can pull this data from automatically?
There is no direct API that can help you achieve that.
What you can do instead is to use UrlFetchApp from Apps Script and retrieve the page text from the admission/contact pages wanted. Afterwards you can retrieve the email address from the page text retrieved using regex.
The below code does the mentioned above but depending on the page, it may retrieve more than one email address. If you know exactly that the first email in the list is the email address you actually need, you can call email[0] instead.
function findMail() {
var url ="ADMISSION_CONTACT_PAGE_URL";
var pageText = UrlFetchApp.fetch(url).getContentText();
var email = pageText.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
Logger.log(email);
}
Reference
UrlFetchApp Class Apps Script;
Regex Tester.

Hide text lines/text patterns from a generated logfile

As my role as tester (among all the things), I need to review log files for errors. Unfortunatly, the generated output file (file-YYYY-MM-DD.log) displays all kinds of information, not just the errors (or "things that go bad").
My question is; how do I hide the lines I don't want and generate an output file (log) containing the deviations (errors, warnings etc)?
For example, I want to hide:
| I | variable text I do not want, location: /path/to/foo/bar
| W | service X stopped
| W | service X started
The most ideal outcome with the script I want is:
-> Ask question: "Do you want to hide information and warning messages? |
-> Answer: yes --> hide all | I | message (like: no new foo's found in: /location/to/foo.
-> Answer: no --> do nothing (closes script)
The data is from an Unix environment, however the script is going to run in an Windows environment.
Thanks

Is it an easy way to implement html like ancher in silverlight?

I want to implment a navigate panel with buttons to control the scrollbar position of a scroll viewer. The button effect will like an ancher in html page. If click one button then the content will scroll to relative position in the scroll viewer. Is it an easy way to implement this?
--------------------------------------------
|Button0 | Button 0 relative content | |
|Button1 | ... | |
|Button2 | ... |-|
|--------- ... |||
| ||| <--the scrollbar
| ... |-|
| Button 1 relative content | |
| ... | |
| ... | |
| Button 2 relative content | |
---------------------------------------------
Thanks in advance!
It's a bit tricky because there is no build in utility function for this (as far as I know).
At the moment I can think of two feasible solutions:
First Solution: Calculate the scroll positions for your Content via UIElement.TransformToVisual Method
Something like this (not tested, not done any brainwork):
private void GotoContent3()
{
var generaltransform = m_content3.TransformToVisual( m_scrollviewer);
Point offset = generaltransform.Transform( new Point( 0, 0 ) );
m_scrollviewer.ScrollToVerticalOffset( offset.Y );
}
Second Solution (not recommended): Use a ListBox to display your Content and call ListBox.ScrollIntoView Method

How to store an array returned by javascript function split in Selenium IDE

Not a developer, new to Selenium IDE, and yes, limited to sticking with IDE only. Appreciate any and all help.
Trying to grab a password from an email generated upon Password Reset so the script can then log in with the new password.
Thought I'd do a split on a delimiter in the email content, Trim as necessary to grab the password. Running into problems with how to store the returned array. In order to do what I'm thinking, I need to store it back into an array that Selenium can traverse.
storeText | css=body | emailText
getEval | storeResults = javascript{storedVars['emailText'].split("delimiter")}
The getEval throws an "missing ; before statement" exception. Using method store instead of getEval works (and moving storeResults to a target), but then the results are typecast as a string. I feel I'm missing something very basic here.
I think the keyword javascript must be omitted
For me the following code works:
storeText | //*[#id="_currentProduct"] | myText
getEval | alert(storedVars['myText'])
When run the alertbox has the value of myText.
Thanks for the response. I came to the same conclusion. Here's the working code:
getEval | storeResults = storedVars['emailText'].split("delimiter")
Was able to access the stored value this way:
LOG.info(storeResults[1])

Resources