Removing line spacing between paragraphs in a Word Document in c# - c#

I'm filling a table cell line by line with a richTextBox's content with this code :
int length = richTextBoxes[0].Lines.Length;
for (int index = 1; index < length; index++) {
table.Cell(3, 2).Range.Text += richTextBoxes[0].Lines[index].ToString();
}
But it automatically passes a weird line before pasting the text, and there is this huge spacing between paragraphs between each lines and I wonder if it's possible to remove it.
Thank you

To change the line spacing of the paragraphs in a text cell:
table.Cell(3,2).Range.Paragraphs.LineSpacing
If this does not have the desired effect you should make sure that line spacing is the problem and not some character code that Word isn't dealing with the way you expect. Type lines in the table cell, without pressing Enter and see if they exhibit the same behavior as you're seeing with your code. If not, then your code is importing something you'll need to "cut off"...
The problem you're seeing may be due to the style applied to the table. If that's the case, then it would be more efficient (and correct from the Word POV) to apply the correct style to the table/cells than to apply the formatting directly.

I believe you are using Microsoft.Office.Interop.Word assembly?
How about setting the property LineSpacing of Paragraph interface? It is a float type.
It can be set after LineSpacingRule property is set to one of the following values: wdLineSpaceAtLeast, wdLineSpaceExactly or wdLineSpaceMultiple.
I hope it helps.

Related

Problems with ranges in Excel Interop

I'm writing a program in C# that accesses the Excel Interop interface to create a spreadsheet. My program creates a worksheet, then enters some text into the first row. I want to set the font to bold for the first row, as a title, but things aren't working as expected.
First I create a range object for the row, then I set the BOLD attribute
Range rng1 = ws.Rows[1];
rng1.Style.Font.Bold = true;
This works OK and afterwards I have a bold header line. However, I noticed that the rest of the spreadsheet was getting set to bold also. So I added some code to turn it OFF but it didn't work as expected. The code looks like this:
Range rng2 = ws.Rows[rownum];
rng2.Style.Font.Bold = false;
The "rownum" variable starts at 2 and increments through 12. It's never set to 1. So this should be creating a second range object that has zero overlap with the first one. Yet for some reason, this turns off the bold attribute for everything.
It seems like the entire spreadsheet is affected by turning BOLD on or off. Oh, and I can do these steps in any order.
Any ideas what's going on and how to fix it?

Column text overflowing out of column width

I am using Migradoc to generate a table and populating some dynamic data for few columns, I have defined column width while defining table structure as-
Table table = new Table();
Column column = table.AddColumn(Unit.FromCentimeter(6));
column.Format.Alignment = ParagraphAlignment.Left;
table.AddColumn(Unit.FromCentimeter(6));
table.AddColumn(Unit.FromCentimeter(8));
Now the third column is having data (acs800-07-1234a-5+asdf+asdf+qwer+wert+2345+rg+2345+ag+35+qwe1252rg+34tgh+24rg+253rg+23rgh+235rgh+#34gh+23rg-4s544) , but it's overflowing the column and getting truncated towards the right side of page. It's automatically wrapped but not correctly , some text is lost in the second line. See image:
Any pointers to fix this issue in wrapping text would be appreciated.
UPDATE- ( Snippet showing how table data is added)
Row row = table.AddRow();
Cell cell = row.Cells[0];
cell.AddParagraph("ACS880-104");
cell = row.Cells[1];
cell.AddParagraph("R1 – R10");
cell = row.Cells[2];
cell.AddParagraph("acs800-07-1234a-5+asdf+asdf+qwer+wert+2345+rg+2345+ag+35+qwe+125+2rg+34tgh+24rg+253rg+23rgh+235rgh+#34gh+23rg-4s544");
MigraDoc automatically breaks lines at spaces, hyphens, and soft hyphens.
You have a long text without spaces and without hyphens. The easy solution: insert soft hyphens where you want to allow line breaks to occur (e.g. a soft hyphen after each "+" sign).
Update: As of version 1.50 you can also use zero-width non-joiners to mark locations where line breaks are allowed. Use soft hyphens to get line breaks with a hyphen, use zero-width non-joiners for line breaks without hyphens.
As Migradoc has limitation of breaking lines at only spaces,hyphens and soft hyphens i have inserted space after everyy 45 chars(your choice as per column width) and hence the value is wrapped properly without having any effect of shown output(no extra chars visible)
Code Snippet -
String myString = "acs800-07-1234a-5+asdf+asdf+qwer+wert+2345+rg+2345+ag+35+qwe+125+2rg+34tgh+24rg+253rg+23rgh+235rgh+#34gh+23rg-4s544";
cell.AddParagraph(Regex.Replace(myString, ".{45}", "$0 "));
OUTPUT

add multiple items to a header Microsoft.Office.Interop.Word

I am building up a word document programatically and the final thing for me to do is to add a header which contains an image to the left a title in the middle.
I have tried various different things but nothing is working. I have tried adding a table to the header range (this throws an exception). I have tried adding the image and then the text but this just makes the text appear and not the image. I have tried adding new fields to the header range but this doesn't work neither.
Could someone point me in the correct direction?
//Add header into the document
foreach (Section section in document.Sections)
{
//Get the header range and add the header details.
Range headerRange = section.Headers
[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.InlineShapes.AddPicture(#"C:\Development\Tools\CommentParser\CommentParser\HeaderLogo.png");
headerRange.Font.ColorIndex = WdColorIndex.wdBlue;
headerRange.Font.Size = 10;
headerRange.Text = "Title";
}
I have found the solution to my problem. It is rather silly if you ask me but if you have headerRange.Text before the image then it will display them both and the text will be to the right of the image. If I have headerRange.Text second as it is in my sample text then it will just display the text.
Although I have solved it, if someone could please explain why this happens then it would be much appreciated.

Get current line's text (where the caret is blinking) of richtextbox

I really need to get the contents of the line where the caret is blinking in a rich text box control. Suppose I have the following text in a rich text box:
This
is
a
test
I would like to retrieve the text of line 2 by providing its index (for example 2 or 1 if the function is zero-based).
Thanks in advance,
Vali
I'm not sure if the question's title and what you really want to know are the same... If you want to get a line given its index, check the Lines property of the RichTextBox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.lines.aspx
Make sure you have the WordWrap for the text box set to false to make sure the index of the line corresponds to the line where the caret is.
Try this:
var pos = richTextBox1.GetLineFromCharIndex(this.richTextBox1.SelectionStart);
MessageBox.Show(richTextBox1.Lines[pos]);
you can do it
richTextBox1.Lines[1]

Determining if Text in Particular DataGridView Cell is Wrapping

I have a databound datagridview on a form that has a text column that is able to be wrapped. When the columns are refreshed the height of each row is reverted to a single line height even if the content of the row is wrapped (and therefore multiline).
I need to be able to determine programatically if the data in the column for a particular row is currently wrapping or not. Is there a property to check to see if the data is long enough to make it multiline?
I think you're asking a couple different questions; the answer to both is "yes".
First, the length or format of the text. Check the cell's value as a string to see if it contains newlines, or if it is longer than X characters (I leave the exercise of coming up with a good X to you):
if(gridView.Rows[i].Cells[j].Value.ToString().Contains(Environment.NewLine)
|| gridView.Rows[i].Cells[j].Value.ToString().Length > x)
...
Second, you can also determine if a row is currently displayed in word-wrap mode; if it is, it should be sized in a combination of horizontal and vertical:
if(gridView.Rows[i].Cells[j].Style.WrapMode = DataGridViewTriState.True)
...
Add the following to your code:
string nl = Environment.NewLine;
dataGridView.Rows.Add(str1 + nl + str2);

Categories