autofit column width to contents in word automation - c#

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();

Related

MigraDoc - Table Border but no Cell/Row/Column Borders

I am struggling to implement what seems should be something straight forward, but having not much luck. I need a MigraDoc table to render with just the Table border, excluding all cells in between:
I have followed the remarks on this post:
How do you add a border around a table in MigraDoc?
Useful information but I havent been able to implement a full fix from it?
I have the following code run just before the table is added to the section:
table.Borders.Visible = true;
for (int i = 0; i < table.Rows.Count - 2; i++)
{
table.Rows[i].Borders.Bottom.Visible = false;
}
Which at first seemed like it did the job... until I come across a table that follows onto the next page... The bottom row border is obviously only rendered for the very bottom row and does not account for PageBreaks mid-table.
Surely there must be a better way of doing this?
EDIT: I appreciate this is somewhat of an old question, but just incase anyone ends up here looking for an answer...
Try using the SetEdge option. There's two ways you could do it, depending on whether you know how many table rows or columns you're going to have (static content), or you don't know yet (dynamic content).
Option 1: Static table content
Set your table up first, so all the columns, cells and rows exist, then add an edge border to your table with
table.SetEdge(a, b, x, y, Edge.Box, BorderStyle.Single, 1, Colors.Black);
The first four numbers a, b, x, y indicate which of the table cells you want to add a borders to, the first two numbers are the top left column then row (in your case to border the whole table, this should be 0, 0) and the second two numbers are the bottom right corner column then row (as per your example this is 3, 4, assuming the heading is a heading row).
After Edge.Box, the options are border style, border width, border color.
You can then add any extra individual borders per cell or row after as normal, so to add a border at the bottom of your header row as per your example...
headerRow.Borders.Bottom.Width = 0.2;
headerRow.Borders.Bottom.Color = Colors.Black;
Option 2: Dynamic table content
If you don't know how many rows or columns are in your table becuase the content is dynamic, the first four numbers in SetEdge could be set with this.table.Columns.Countand this.table.Rows.Count - for example :
table.SetEdge(0, 0, this.table.Columns.Count, this.table.Rows.Count, Edge.Box, BorderStyle.Single, 1, Colors.Black);
References
For more info, see this post:
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3598
And it's also here in the MigraDoc Example (search for SetEdge):
http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

How to automatically resize columns in C1flexGrid?

If I choose one of the WF which have C1flexGrid, and try to maximize that form, columns in my grid have the same width. What I am trying to do is have the columns auto fit to the width of the content, and have the last column fill the remaining space. Is this possible?
For anyone still looking for the answer:
You can use AutoSizeCols method and ExtendLastCol property
c1FlexGrid1.ExtendLastCol = true;
c1FlexGrid1.AutoSizeCols(c1FlexGrid1.Cols.Fixed, c1FlexGrid1.Cols.Count - 1)
Reference.

Telerik Report auto centre table

I have created one report which hides some of the columns of table at end when some criteria don't match. For example, there are total 7 columns and I am hiding 2 columns when criteria don't match. This leads to so much empty space at right side as table don't centre automatically.
I have tried VerticalAlign = Middle but it is not working.
Finally after all efforts I didn't find exact solution but found one alternative.
I have applied Anchoring = Left, Right on table and that helps me to make my table looks better.
What it actually does is, it keeps width as it was specified and based on no. of columns, columns width either shrink or grows.

Detect Printable Area Width in OpenXml.Wordprocessing

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.

(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