Column text overflowing out of column width - c#

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

Related

How to not skip empty cell in Excel when creating .csv file

hope you guys can help!
I am trying to create .csv out of Excel which would have columns A - H filled with some data I want to read but data is optional. So some times the first column A can be empty. Now, when I save the .csv, unless I entered one space in the A column, Excel automatically trims A column, and the result is 7 cells to read A - G instead 8 cells A - H (with A column) being "empty". I would like to save client the trouble of needing to enter one space or -> ="" in the Excel formula to make first or last column "empty" but not trimmed in the resulting .csv.
Thank you!
There is no option in Excel to preserve empty columns, when exporting to .csv. But you could introduce a header text in the first row, so you will always have the columns filled atleast with the header text.
Plus it would make your data probably more readable, depending on what data this is.
Another possibility would be to implement a makro and embedd a button in your excel file:
see
how not to skip empty first cell when saving as .CSV?

FlowDocumentReader - carriage return line feed characters are added to the end of text that has none

I'm adding text to a FlowDocumentReader within a single paragraph like this
FlowDocumentReader fdr = new FlowDocumentReader();
fdr.Document = new FlowDocument();
Paragraph p = new Paragraph();
Run r = new Run("some text");
p.Inlines.Add(r);
fdr.Document.BLocks.Add(p);
NOTE: I am actually combining multiple runs of text into one paragraph.
However, when I display the text in the FlowDocumentReader, it always shows some extra characters after the very last character of the text I inserted. When I try to drag and select the entire text, and copy, it goes beyond the last character. And when I paste the entire selection to Notepad++ it shows there is a CR LF characters at the end.
So if I start with this text
some text
I end up with
some text\r\n
Is there anyway to eliminate these from becoming part of the text in the FlowDocumentReader itself? I actually don't want them to appear in the FlowDocumentReader. I require that the text not contain any additional characters beyond those that were part of the original text I added to the Runs.
The closest issue I can find similar to this is for RichTextBox
Why does RichTextBox implicitly add newline?

Removing line spacing between paragraphs in a Word Document in 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.

How can i use white spaces in bindingsource.filter?

I have:
a datagridview (datagridview1).
a column in datagridview1 named Phone Number.
a bindingsource named (bindingsource1).
This code
bindingsource1.Filter="Phone Number = 123";
does not work because there's white space between phone and number!
What to do?
Use square brackets
bindingsource1.Filter="[Phone Number] = 123";
or, preferably, don't use spaces in field-names.

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