I'm adding an image to a Word document using OpenXml.
I am able to tell the size of the image being added, and I can resize it if I want to. But what I really want is to only resize it if it's wider than the current column. (In my case, there is only one column but perhaps that could change.)
Is there any way to know how wide the current column is and, therefore, be able to ensure that the image fits within the column? As it is, large images are extending off the page.
I've updated my answer, because it was an answer for a case when table columns are involved, but the word column in the question mean a column or text rather than a column in a table.
In the document, You should be able to get the following values:
// ...
var sectionProperties = body.GetFirstChild<SectionProperties>();
// pageSize contains Width and Height properties
var pageSize = sectionProperties.GetFirstChild<PageSize>();
// this contains information about surrounding margins
var pageMargin = sectionProperties.GetFirstChild<PageMargin>();
// this contains information about spacing between neighbouring columns of text
// this can be useful if You use page layout with multiple text columns
var columns = sectionProperties.GetFirstChild<Columns>();
var spaceBetweenColumns = columns.Space.Value;
var columnsCount = columns.ColumnCount.Value;
I haven't tested this, but I suppose You may be able to calculate the actual width of a text column using those values.
Related
Using Microsoft Interop for word, after adding a table to the document, how can the column width be set for all columns so that it fits the largest item there? For example, if the column header is only two letters, and each cell underneath is only one digit, the column should only be about a centimeter wide.
If you only have one table in your document, you would use something like this (where oDoc is your active document.)
oDoc.Tables(0).AllowAutoFit = True;
oDoc.Tables(0).AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
If you have more than one table, you'd want to choose the index of the one you wish to update or loop through them.
In the Word object model AutoFit is what the feature is called that allows (or doesn't allow) table columns to resize to fit the content, the width of the window/page or prevents them from resizing automatically.
To force the table columns to resize to fit their content:
tbl.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
To change the width of a single column to fit to the content:
tbl.Columns[index].AutoFit();
This can also be done for all columns:
tlb.Columns.AutoFit();
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
My project needs to display a list of contacts in a grid. Normal data - First Name, Last, City, State, Zip, Email, Phone, Company Name.
1 - We need to support wide variation in screen widths 800px -> 2,000px.
2 - We want to display as much information as possible, with as little white space as possible.
3 - As the grid gets wider, we want some fields to expand (Organization Name), others to stay at a maximum Width (State - 2 char).
None of the standard column resize modes seem to work since there is no Max Width column. The State column ends up with a ton of space, and the organziation is still being chopped off.
Has anyone resolved this problem?
I had similar problems with our Gridview and I have raised a ticket with Devexpress team for the same reasonsDevexpress ticket. It seems SetWidth() client side JS method does not work properly when you have fixed width columns.
In your case, I would suggest using percentage size for your columns which needs to be dynamically expanded and static size for the other ones.Also set the text wrap true for cells that you want texts tightly wrapped inside.
settings.Columns.Add(column =>
{
var commmonHeaderStyle = column.HeaderStyle as GridViewHeaderStyle;
commmonHeaderStyle.Font.Bold = true;
column.CellStyle.Wrap = DefaultBoolean.True;
column.FieldName = "Test";
column.Width = System.Web.UI.WebControls.Unit.Percentage(30);
});
You might also want to take a look at this example : Full Screen mode (100% browser Width and Height)
My question is very simple - after data binding I want to set all columns width so that it will not be horizontal scrolling. Something like this:
columnWidth = grid.width/grid.Columns.Count
But this expression does not consider the "left part of gridView" with which I can select row, which displays current row. How can I calculate its width? [(grid.width - X)/grid.Columns.Count]
Very easy to do this. Just set the DataGridView.AutoSizeColumnMode property to Fill.
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
And you may need to explicitly ask the grid to resize column spacing. I can't remember for sure. But that is simple to do:
grid.AutoResizeColumns();
According to the documentation, Fill will do the following:
The column widths adjust so that the widths of all columns exactly
fill the display area of the control, requiring horizontal scrolling
only to keep column widths above the DataGridViewColumn.MinimumWidth
property values. Relative column widths are determined by the relative
DataGridViewColumn.FillWeight property values.
So basically, as long as you don't have any columns with a MinimumWidth that will prevent it, this property does exactly what you are looking for.
EDIT: Addressing the comment of the OP:
You can always create a simple property to report that information to you. Something like...
public int DataGridViewDeadSpaceWidth
{
get
{
int x = grid.Width;
foreach (DataGridViewColumn column in grid.Columns)
x -= column.Width;
return x;
}
}
You could also use this to inherit from a DataGridView and add the property yourself to the control. Note that there is probably some other dead space from the borders of the DataGridView that you need to account for.
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). "