I am populating a content control in word 2010 with some text. I can't get my text to go on a new line. I have tried '\n' ,'\r', Environment.NewLine and it still appears on one line.
Any ideas what I should be using
I believe you may need to enable MultiLine = True for the CC. See: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.contentcontrol.multiline(v=VS.90).aspx
If you've already done that, see Microsoft Word Document Controls not accepting carriage returns for a possible solution
You could use something like this:
private static Run InsertFormatRun(Run run, string[] formatText)
{
foreach (string text in formatText)
{
run.AppendChild(new Text(text));
RunProperties runProps = run.AppendChild(new RunProperties());
Break linebreak = new Break();
runProps.AppendChild(linebreak);
}
return run;
}
If you are using VBA, what you are searching for is
vbNewLine
which will insert a full line break.
Related
I am trying to create an OpenXml word document that looks as follows:
2.1.1 This is a paragraph
2.1.2 This is also a paragraph
2.1.2.1 This paragraph relates to 2.1.2
The initial plan is to have the number in its own paragraph, and have the text in its own paragraph as well but the problem is that there a line breaks the interfere with the structure of the list items.
I have tried using OpenXml numbering part but the problem there is that I couldn't figure out how to write custom numbers for the numbering.
I have tried to use Justification and Indentation classes but I can't seem to get the desired result, due to the line breaks that come with each paragraph causing the paragraphs to print one below the other.
Is there an alternative way to achieve this result or a way to remove the line breaks for a paragraph?
Apologies for not being clear enough when posting the question, but for those who are interested I have managed to solve the problem as follows.
I downloaded OpenXml Productivity tools which as available here
I then created the paragraphs that I wanted in MS Word, which looks like this
I then used the tool to generate the code needed, and here it is.`Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "005C6DC7", RsidParagraphProperties = "005C6DC7", RsidRunAdditionDefault = "005C6DC7", ParagraphId = "5F7449EC", TextId = "6B3C1D9C" };
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "2.1.2";
run1.Append(text1);
Run run2 = new Run();
TabChar tabChar1 = new TabChar();
Text text2 = new Text();
text2.Text = "More text";
run2.Append(tabChar1);
run2.Append(text2);
paragraph1.Append(run1);
paragraph1.Append(run2);
return paragraph1;`
I have this string read from a table in sql:
Yderligere tillidshverv:
Medlem af bestyrelsen for Dansk XXSW, region Ã…rhus.
Andre aktiviteter:
Opfinder af 123 trapper.
What I want to do is replace occurences of \r\n with just a single \n. However it is not working. The problem is that notepad++ still shows CRLF. It should only show LF. What am I missing? Here is my code:
string x = returnValue.Replace("\r\n", "\n");
When you paste a string from a Windows clipboard into Notepad++, the linebreaks get converted to the line ending style set for the current document (see the bottom of the screen).
If you right click and select UNIX, you will be able to paste the string with \n only. This will also change all line endings to LF.
If you do not want to change all line endings in the document to LF, you can use EDIT > Paste Special -> Paste Binary Content
However, from C#, you can just write the variable content into a text file:
var s = "I know\r\nit should\r\n";
var b = s.Replace("\r\n", "\n");
using (var sw = new StreamWriter(#"PATH_TO_FILE", false, Encoding.UTF8))
{
sw.Write(b);
}
And enjoy \n linebreaks.
Using the Regex class will work. I used linqpad to quickly test code which has the Dump() extension method
Regex.Replace("test\r\ntest","\r\n","\n").Select(x => x).Dump();
Update : A regular replace also works BTW. You state that it doesn't?
"test\r\ntest".Replace("\r\n","\n").Select(x => x).Dump();
I have this bit of code below which saves perfectly fine to a text file, however there is no formatting that is transferred, for example, The below words would save as THISISATESTLAYOUT. I would like the desired output displayed below:
THIS
IS
A
TEST
LAYOUT
I know there's something obvious im missing here so thanks for any help
if (entity.Switch == true)
{
string path = #"filepath....\\Test.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.Write(entity.comments);
}
}
}
this string is saved in one entity called comments and displays fine within the C# application.
sw.Write and sw.WriteLine both give the same single line output with no spaces
and also entity.comments references a column in an SQL Server Table, and is a VARCHAR(MAX)
you may be using '\n' for newlines. If so use
sw.Write(entity.comments.Replace("\n", "\r\n"));
Change sw.Write to sw.WriteLine
how do i read the second line of text in visual studio 2012 c#
the txt file
user123
**12345**
asdfd
i want to get the second line in one button1_click and show it to textblock2
i did try learn from here
How do I read a specified line in a text file?
and here
How to skip first line and start reading file from second line in C#
but no one of these works because theres difference i couldnt apply in my code
any help?
=================================================================================
sorry to confusing you all
actually im really lacking experience in programming and i hardly know how to use it
right now im using vs2012 in windows8 , is that mean i was coding in winrt?
btw , i appreciate all your help and successfully applying answer to my code
this is the actual code
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(tb1.Text+".txt");
var line = await FileIO.ReadLinesAsync(file);
if (tb2.Text == line[2])
{
tb3.Text = (line[1]);
}
var desiredText = File.ReadLines("C:\myfile.txt").ElementAt(1);
File.ReadLines() returns an String[] of the lines in a file. The index of the second line is 1. See this.
try
var desiredText = File.ReadLines("C:\myfile.txt");
textbox1.text = desiredText[1];
Just call a .ReadLine() before you start capturing the content of the file.
Essentially this will make the reader skip the first line of the file and take only the 2nd line and all lines that follow it.
// Try this to take the second line.
string line;
using (var file_read = new StreamReader(your_file))
{
file_read.ReadLine();
line = file_read.ReadLine();
}
textBox1.Text = line.ToString();
I am writing a text into a file. I am getting a column from database and stored it in a string and writing it in a text file. That column contains c# code and it is writing like a single line with small squares for next line(space). I need to write it line by line. Here is my code.
using (var dest = File.AppendText(Path.Combine(_logFolderPath, "a.txt")))
{
dest.WriteLine(line.TrimStart());
}
Any suggestion?
Does Notepad show small squares for new lines, but when you look at the file in Visual Studio it's OK? If so, my guess is that this will fix it:
dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine));
try this
File.AppendAllText(Path.Combine(_logFolderPath, "a.txt",
line.Trim() + Environment.NewLine));
dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine));