NPOI how to get size of text in the cell - c#

I just started to learn NPOI. I think, that I have some idea how to use it, but I just need two more things to know how to do.
The first one is how to get the minimum size (height and width) of the cell with some text and style, so I will be able to set the correct cell size.
I can't use the auto size feature, because I have structured document and I need some different text in the same column to be on multiple lines.
An example:
I need the column "Name" to have size of "Total amount". Is there a way, how to get the width of the"Total amount" text for selected font?
Thank you.

Related

TextBoxGrid/Tabular View in winforms C#

Hi I need to make a text grid to display some data.
The requirement is as follows. The grid will always be n by n (a square).
Each cell in the grid needs to display 2 lines of data
1st line of each cell is a 3 char identifier like ABC Font size 8
2nd line of each cell needs to be 4 digit decimal number xx.yy Font size 6
Each cell background needs to be colour coded accoring to the xx.yy value Basically I need an excel style value based conditional formatting
I would like to be able to update each cell with a Control.Update (row,column, values) type function or a Control[row][column] = values type property.
So far I have looked at Gatagridviews but I cannot seem to find a way to implement multi line text entries with different font sizes
Before I decide to write my own control in C# and reinvent the wheel,is there a simpler way to do this. Please note I specifically need each cell to have 2 lines of text and each line of text has a different font size.
My approach will be to create NxNx2 text boxes and the play around with their formatting wrapping this entire mess in a class and then using it in a form with.
But as you can see if I am dealing with a 40X40 data grid this will result in 3200 text boxes. I don't think my approach is the best/correct way to go. Not to mention how difficult it will be to format this as a nice big Square.
PS. I have 0 knowledge of WPF and I have written most of my GUI in c# already

Dynamic sized multi line TextBox field in a PDF

I have created a document in an open office with a multi-line form field:
The issue I am having is when the dynamic content exceeds the initial size of the multi-line text box:
Sure I can re-size the Text Box in the original template but the dynamic content may be from 1 to 50 lines and I want the text after the Text Box to be close to the last line of dynamic content.
Can someone suggest a way to solve this?
I have once coded a solution to set the width and height of a textbox programmatically based on the characters supplied. I think this was a school assignment a long time ago.
This can be done with both VBA in a code behind or probably with a macro even. Or with VB.net.
I don't have the code I used way back when, but basically determine the maximum width you are able to provide in character width, the preferred width. Determine the pixel requirement per character for that width. This becomes the textbox width.
Divide your total string character count by your preferred width character count. Round up, calculate the pixel height per character. And use this value times the rounded result for the textbox height. Dirty but it should work.
Any chance you can change to a label and set AutoSize to True? You can fix the width and let the height auto adjust. This should be done before converting to a pdf. In fact all of the sizing should be resovled before pdf conversion.
Another Down voter without a comment, should not be allowed.

Remove extra padding in DataGridView column label?

I have a DataGridView that I am trying to reduce the width of so it displays how I wish in my form. I am having issues "removing" the extra padding in the column names when reducing the size of the DataGridView.
On the left is the table as I originally had it, I realized that I would like it smaller so I decreased its width. The center image represents the smallest the columns will go before the text wraps to the next line. The right image is the size I would like the table to be, I have shown that the text "(px)" will fit in the space.
I have gone through all of the settings I can find in the designer and have not found anything to help. Does anyone know how to fix this?
Here is my example. Although it might not be the best solution but worked.
I measured the exact width of HeaderCell's content ("Height (px)") and set the column's width with some additional padding (For example, 9).
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
Size textSize = TextRenderer.MeasureText(dataGridView1.Columns[0].HeaderText, dataGridView1.Font);
dataGridView1.Columns[0].Width = textSize.Width + 9; // Adding some padding

Limit text to width of TextBox

I'm using a TextBox to create an editable title on something that looks like a post-it note. I changed to a TextBox from a RichEditBox to see if that could solve my problem, which it didn't, so I'm willing to change back if that helps.
My problem is that I don't want the user to be able to enter more characters than fit in the set width of the TextBox, because I want the whole title to be visible. Setting a fixed limit to the amount of characters that can be entered doesn't really work since for example 10 large M's would fill the width of my TextBox, but other characters will only fill half of it. So I would like to compare the width of the text to the width of the TextBox, so I can restrict input beyond that point.
Edit: I'm using the Segoe UI font, and I don't really consider changing the font to one with characters of equal width as a solution.
Set the TextBox Font to a Courier Font, then all characters are equal width and calculating max becomes trivial.
I am suck in programming world. i m try help you..

(C#) Can I programmatically set an XLSX cell to a picture/image?

I am hoping to make spreadsheets that contain some pictures (embed pictures from files) and I started looking at EPPlus (looks like a great library)
However it seems that the images are not tied to a cell - rather to an x,y, coordinate.
Is there a way with EPPlus or other way to set a cell to a picture (and then manipulate the size of the cell?)
SetPosition
My misunderstanding...
Here is a comment I found when looking around:
No version of Excel allows you to insert a picture into a cell. Pictures are inserted into the worksheet and will always float.
One of the properties of a picture can be set to "move and size with cells" but that only moves or stretches the picture when the underlying rows and columns are inserted, deleted or sized. It does not confine a picture to a cell.
So perhaps I just need to set the properties appropriately.
If I can do this programmatically I will be all set
EDIT
The following code does pretty much what I want/need.
Note that before inserting the pics I set the width and height of the cell I was overlaying to appropriate sizes.
private static void AddImage(ExcelWorksheet ws, int rowIndex, String imageFile)
{
ExcelPicture picture = null;
Bitmap image = new Bitmap(imageFile);
if (image != null)
{
picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString(), image);
picture.From.Column = 0;
picture.From.Row = rowIndex-1;
picture.SetSize(320, 240);
}
}
You can insert the picture, then adjust its .Top and .Left so it aligns with the .Top and .Left of the appropriate cell. You can set the .RowHeight of the cell's row using the same units as the .height of the picture (though there's a maximum height). The .ColumnWidth of the column is in units of text characters wide, so what I do is something like:
myColumn.ColumnWidth = myColumn.ColumnWidth / myColumn.Width * myPicture.Width
and I run it twice because sometimes the first time you set .ColumnWidth, it isn't set precisely.
I don't think you can do that in Excel itself; when you add a picture to an Excel worksheet, it's a floating object, it's not fixed to a specific cell.
Found in the EPPLUS documentation it should be possible with the EditAs setting on value TwoCell.
picture.EditAs = eEditAs.TwoCell;
"Specifies that the current drawing shall move and resize to maintain its row and column anchors (i.e. the object is anchored to the actual from and to row and column). "

Categories

Resources