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

Free Support Forum - aspose.com

Calculating text length in table cell based on table styles

Hi there,

Is there any way to get the background color of the whole table from TableStyle class?
I need to know the color defined for background in TableStyle in my scenario.
The only thing I could find was accessing Font and ParagraphFormat information through TableStyle API.

Thanks

@behrouz,

Please ZIP and upload your sample Word document here for testing. Also, please provide a screenshot highlighting the background color of the Table, that you want to access by using Aspose.Words, and attach it here for our reference. We will then investigate the scenario on our end and provide you more information.

Hi @awais.hafeez

thanks for your prompt reply,
please let me change a little bit my question.
is there any way to calculate the text length in a table based on font size and family getting from table style?
I just wanted to know if one line in a table cell will be broken due to different table style font.

thanks.

@behrouz,

Please ZIP and attach your sample Word document here for testing. Please also elaborate your query with the help of screenshots. We will then be in better position to address your concerns accordingly. Thanks for your cooperation.

@awais.hafeez

I attache the sample code including Word document for your test scenario.
As you can see from the code there is a table containing two cells where the first cell shows line numbers and the second is content. Each cell is filled by Html fragments generated by our software.
I’m going to find a way to generate and add the missing line numbers caused by different table style for the first cell. For example, in this example, we have a code block with 21 lines but when it’s exported with “Scroll Code” table style, the lines of table cell increase due to the font style.

aspose-html-table.zip (58.4 KB)

@behrouz,

We have produced an output document by using the code you provided (see output.zip (19.3 KB)): There are 25 lines in second cell while the first cell shows numbers 1~20. You can use the following code to calculate number of lines in each Paragraph and then adjust numbering in first cell:

Document doc = new Document("E:\\temp\\output.docx");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator it = new LayoutEnumerator(doc);

Table tab = doc.getFirstSection().getBody().getTables().get(0);

for (Paragraph paragraph : (Iterable<Paragraph>) tab.getFirstRow().getLastCell().getChildNodes(NodeType.PARAGRAPH, true)) {
    Object paraBreak = collector.getEntity(paragraph);

    Object stop = null;
    com.aspose.words.Node prevItem = paragraph.getPreviousSibling();
    if (prevItem != null) {
        Object prevBreak = collector.getEntity(prevItem);
        if (prevItem.getNodeType() == NodeType.PARAGRAPH) {
            it.setCurrent(collector.getEntity(prevItem)); // para break
            it.moveParent();    // last line
            stop = it.getCurrent();
        } else if (prevItem.getNodeType() == NodeType.TABLE) {
            Table table = (Table) prevItem;
            it.setCurrent(collector.getEntity(table.getLastRow().getLastCell().getLastParagraph())); // cell break
            it.moveParent();    // cell
            it.moveParent();    // row
            stop = it.getCurrent();
        } else {
            throw new Exception();
        }
    }

    it.setCurrent(paraBreak);
    it.moveParent();

    // We move from line to line in a paragraph.
    // When paragraph spans multiple pages the we will follow across them.
    int count = 1;
    while (it.getCurrent() != stop) {
        if (!it.movePreviousLogical())
            break;
        count++;
    }

    int MAX_CHARS = 16;
    String paraText = paragraph.getText();
    if (paraText.length() > MAX_CHARS)
        paraText = paraText.substring(0, MAX_CHARS) + "...";

    System.out.println("Paragraph '" + paraText + "' has " + count + " line(-s).");
}

Hope, this helps.

@awais.hafeez

Thanks for your prompt reply,

I just tested this code with the attached document and realized that it wouldn’t work correctly.
For the first table, I get 6 and for the second one 47 while the correct numbers are 4 and 28.

CodeBlock.docx.zip (20.5 KB)

@behrouz,

Please try using the following code:

Document doc = new Document("E:\\CodeBlock\\CodeBlock.docx");

doc.updateTableLayout();
int totalLines = 0;

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator it = new LayoutEnumerator(doc);

Table tab = doc.getFirstSection().getBody().getTables().get(1);

for (Paragraph paragraph : (Iterable<Paragraph>) tab.getFirstRow().getLastCell().getChildNodes(NodeType.PARAGRAPH, true)) {
    Object paraBreak = collector.getEntity(paragraph);

    Object stop = null;
    com.aspose.words.Node prevItem = paragraph.getPreviousSibling();
    if (prevItem != null) {
        Object prevBreak = collector.getEntity(prevItem);
        if (prevItem.getNodeType() == NodeType.PARAGRAPH) {
            it.setCurrent(collector.getEntity(prevItem)); // para break
            it.moveParent();    // last line
            stop = it.getCurrent();
        } else if (prevItem.getNodeType() == NodeType.TABLE) {
            Table table = (Table) prevItem;
            it.setCurrent(collector.getEntity(table.getLastRow().getLastCell().getLastParagraph())); // cell break
            it.moveParent();    // cell
            it.moveParent();    // row
            stop = it.getCurrent();
        } else {
            throw new Exception();
        }
    }

    it.setCurrent(paraBreak);
    it.moveParent();

    // We move from line to line in a paragraph.
    // When paragraph spans multiple pages the we will follow across them.
    int count = 1;
    while (it.getCurrent() != stop) {
        if (!it.movePreviousLogical())
            break;
        count++;
    }

    int MAX_CHARS = 16;
    String paraText = paragraph.getText();
    if (paraText.length() > MAX_CHARS)
        paraText = paraText.substring(0, MAX_CHARS) + "...";

    System.out.println("Paragraph '" + paraText + "' has " + count + " line(-s).");

    totalLines += count;
}

System.out.println("Total Lines = " + totalLines);

@awais.hafeez

thanks for your reply,

Do you know why it still doesn’t work for the first table in the document (with 4 lines)? It returns number 3 for that which is not the correct line count.
For example in the attached document, there are 3 tables in which the line counting isn’t correct for the first table.

And If I have several tables in documents, should I call updateTableLayout() for each calculation or once is enough? and this calculation should be done once the document is created or is it possible to run this code during document construction (after each table creation step)?

CodeBlock01.docx.zip (21.5 KB)

Regards

@behrouz,

It is not required to call updateTableLayout method all the time. You can load these DOCX files and then save them to PDF format with and without calling updateTableLayout method to visualize which output is better. If none of the PDF outputs are acceptable for you, then we will log the issue in our issue tracking. Please check the PDF files on your end and we will wait for your further input on this topic.

@awais.hafeez

We need to fix this issue in Word not PDF. Are there any other ways to calculate the number of lines in Word (Specifically in table cells)?

Thanks,

@behrouz,

Please make sure that the “Ayuthaya” font is installed on your machine. If the problem still remains, please ZIP and attach this “Ayuthaya” font file here for further testing.

Please also check: True Type Fonts

@awais.hafeez

I attache the font here and it’s already installed on my machine.
Ayuthaya.zip (103.8 KB)

Regards

@behrouz,

We have logged your problem in our issue tracking system. Your ticket number is WORDSNET-18207. We will further look into the details of this problem and will keep you updated on the status of the linked issue.

@behrouz,

It appears that the font file you attached is not valid. We see the following error upon installation of this font file:

Could you please check if ‘Ayuthaya’ font you attached works for you? Please provide the correct/valid font file here for further testing. Thanks for your cooperation.

@awais.hafeez

could you please check this font file?
Ayuthaya.ttf.zip (104.6 KB)

Regards

@behrouz,

I am afraid, the new attachment is still causing the same problem on our end i.e. the font file is not valid. Please provide the correct/valid font file here for further testing. Thanks for your cooperation.

@awais.hafeez

I’m wondering why it’s not working on your machine.
Could you please download the compatible ones directly from the Internet and see if this issue still exists.

Regards,

@behrouz,

The font file does not work over 64-bit Windows 10 machine on our end. What OS are you using on your end for this scenario?

@awais.hafeez

I’m using Mac Os for this scenario.

Regards