Shrink To Fit in Words

I have a requirement where I need the text in a table cell to shrink to fit one line, if it is too big to sit on one line initially.


We got this working in Cells using ShrinkToFit. I can’t work out a way of getting the same functionality in Words.Tables.

I’ve read a couple of posts about estimating the width of the text based on font and then calculating if it will be wider than the cell and adjusting the size of the text accordingly. But this seems a very long way around to achieve something that is relatively trivial.

Code is as follows:

private static void Main(string[] args)
{
Console.WriteLine(“Start”);
var document = new Document(“TitleOnlyTemplateWord.docx”);

var builder = new DocumentBuilder(document);
builder.Font.Name = “Arial”;

var title1 = “A very long piece of text. A very long piece of text. A very long pice of text. A very long piece of text”;
var title2 = “A shorter piece of text”;
var docTitleRun1 = document.GetChildNodes(NodeType.Run, true).ToArray().SingleOrDefault(c => c.GetText().Contains(“DocumentTitle1”));

var paragraph = (Paragraph)docTitleRun1.GetAncestor(NodeType.Paragraph);
paragraph.Runs.Clear();
builder.MoveTo(paragraph);

builder.Font.Size = 18;
builder.Font.Color = Color.Gray;
builder.Font.Name = “Arial”;
builder.Font.Underline = Underline.Single;

builder.StartTable();
builder.CellFormat.WrapText = false;

var cell = builder.InsertCell();
builder.Write(title1);
builder.EndRow();

builder.InsertCell();
builder.Write(title2);
builder.EndRow();

builder.EndTable();

document.Save(@“C:\src\titleTest1.docx”, SaveFormat.Docx);
}

Now I can add the line “builder.CellFormat.FitText = true”, but this causes problems as the top line text wraps and the bottom line is spaced to fit the line. (Strangely, when I open the saved file, double click into the header and place the cursor in the to cell, the formatting changes at the text fits to cell, without reshaping the cell). I only want the FitText to apply to the cells if the text exceeds the width of the cell.

Also I need to add, that if I put a break point on the save function (after builder.EndTable() is called) and inspect the cell variable, cell.CellFormat.PreferredWidth is Auto, 0.0 and cell.CellFormat.Width is 0.0. SO how can I compare the width of the text to width of the cell anyway if I can;t get the cell width?

Any ideas?

Cheers

Hi there,

Thanks for your inquiry. Please note that MS Word document is flow document and does not contain
any information about its layout into lines and pages.

Aspose.Words uses our own Rendering Engine to layout documents into pages. The Aspose.Words.Layout namespace provides
classes that allow to access information such as on what page and where
on a page particular document elements are positioned, when the document
is formatted into pages. Please read about LayoutCollector and
LayoutEnumerator from here:
http://www.aspose.com/docs/display/wordsnet/LayoutCollector+class
http://www.aspose.com/docs/display/wordsnet/LayoutEnumerator+class

Please check DocumentLayoutHelper project in Aspose.Words for .NET examples repository at GitHub. This sample demonstrates how to easily work with the layout elements of a document and access the pages, lines, spans etc.

Please read about specifying table and cell width from here:
http://www.aspose.com/docs/display/wordsnet/Specifying+Table+and+Cell+Widths

Please let us know if we can be of any further assistance.

A post was split to a new topic: 书签元素在文档中的位置