We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting cell width according to text size in ASPOSE WORD JAVA APIs

I am working on creating tables in word document using ASPOSE WORD JAVA API.

I faced a problem while setting column width for table in word using ASPOSE API.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

In my scenario I have to set column width according to the header text. So I need some way to get the column width by characters in the text.

Currently , I used to get the character count and to get the width multiply it with some constant (assuming the point width of single character).

e.g. total number of character in text is ‘charCount’ then my column width will be

‘charCount * 5.8’.

If I reduce my constants value from 5.8 to some smaller value the text starts wrapped or

If I tried to increase this constant the column width got increased more significantly.

to set the column width I used following API

builder.getCellFormat().setWidth(value);

for small text this technique is working fine but when text exceeds more then 40-50 character the extra space got added.

This space keep increasing propositionally bigger with increase in characters. Please find the document attached for the problem.

Is there any way to set the width of cell to best fit the text?

Hi Katy,

Thanks for your inquiry.

If the destination format of your document is a flow format (e.g DOC, DOCX and not PDF, Image etc) then I think the suggestions on this post here may be helpful to you.

We are currently looking into supporting fitting table cells to content. You request has been linked to the appropriate issue. We will keep you informed of any developments.

Thanks,

Hi,


I have posted this issue some time ago. Thanks for your help on this.
The link provided by you does not solve my problem. Let me just re-iterate my problem.
I want to create a table with some columns. The number of columns and the label of column is user input.
I want to turn on the auto fit option if page width allows this, else i want to turn off the auto fit option.
If there is any way i can determine the width for column for given column label, then i can calculate the total width in advance and i can compare it with the page width.

Thanks



Hi

Thanks for your request. I think, the following article could help you to achieve what you need:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-autofit-a-table-to-page-width.html

Regarding calculating the width for column for given column label, there is no direct way to calculate width of text. However, nothing is impossible. You can approximately calculate width of each character as half of font size. Also, you can try using System.Drawing to measure text. For instance, the following code approximately calculate width of text inside cell:

///

/// Method calculate an aproximate width of content inside cell.

///

private double CalculateWidhtOfCellContent(Cell cell)

{

double width = 0;

foreach (Paragraph paragraph in cell.Paragraphs)

{

double intermWidth = 0;

foreach (Run run in paragraph.Runs)

{

using (Bitmap bmp = new Bitmap(1,1))

{

bmp.SetResolution(96, 96);

using (Graphics g = Graphics.FromImage(bmp))

{

using(System.Drawing.Font f = new System.Drawing.Font(run.Font.Name, (float)run.Font.Size))

{

SizeF textSize = g.MeasureString(run.Text, f);

intermWidth += textSize.Width;

}

}

}

}

if (intermWidth > width)

width = intermWidth;

}

return width;

}

Hope this code could be useful for you.

Best regards,

The issues you have found earlier (filed as WORDSNET-2044) have been fixed in this .NET update and in this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(10)