Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top