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

Free Support Forum - aspose.com

Identify a Table in Word doc using Aspose words api

Hi,

Could you please help if is there any method in Aspose words to check if we can identify a Table in word document and give spacing on top?

I have a word document which can a paragraph and table or paragraph or image. Now My requirement is, I have a insert a line space between the paragraph and table or paragraph or image.

Please help in this. I have attached a sample document fyr.

Regards,
Dharmender

Hi Dharmender,

Thanks for your inquiry. In your case, I suggest you please insert bookmark into the table and access table by moving DocumentBuilder cursor to the bookmark. For example, see the following code snippet:

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
//Move DocumentBuilder cursor to the bookmark
builder.MoveToBookmark("myTable");
//Get table where this bookmark is placed
Table myTable = builder.CurrentNode.GetAncestor(NodeType.Table) as Table;
//Insert paragraph break before table
builder.MoveTo(myTable.PreviousSibling);
builder.InsertBreak(BreakType.ParagraphBreak);
doc.Save(MyDir + "Out.docx");

Moreover, you may use the Document.GetChildNodes Method (NodeType, Boolean) to get a live collection of child nodes that match the specified type. You may use this method and get the table by index.

Hope this helps you. Please let us know if you have any more queries.

Hi,

Thanx for your sample code.

I tried this code in my local machine which is on windows OS and it worked as expected, however when I am running the same code in Unix box i am not getting the desired output.

I have replaced the latest jar file “aspose-words-14.7.0-jdk16” but still no luck.

Please let me know if any changes need to done.

Hi Dharmender,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

share the code which you are using to generate the output document.

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi,

I am attaching the input document and the expected output document for your reference. Below is the sample code I have used for the same.

DocumentBuilder builder = new DocumentBuilder(doc);
System.out.println(doc.getChildNodes(NodeType.TABLE, true).getCount());
for (int i = 0; i < doc.getChildNodes(NodeType.TABLE, true).getCount(); i++)
{
    Table table = (Table)doc.getChild(NodeType.TABLE, i, true);
    if (table.getPreviousSibling() != null)
    {
        System.out.println("Table has paragraph above it " + table.nodeTypeToString(table.getPreviousSibling().getNodeType()));
        builder.moveTo(table.getPreviousSibling());
        //builder.insertBreak(BreakType.PARAGRAPH_BREAK);
        builder.insertBreak((int)0.16);
    }
}

I want to insert custom space between paragraph and table. In the input document, if you observe the space between paragraph and table is “3.16 inches” but I want reduce the space to “0.16 inches”. I tried with builder.insertBreak((int) 0.16) but it did not gave me the required output.

Can you please tell me if we can insert custom space like 0.16 inches between paragraph and table.

Regards,
Dharmender

Hi Dharmender,

Thanks for your inquiry.

reddydharmender:

In the input document, if you observe the space between paragraph and table is “3.16 inches”

I am afraid I can not find the “3.16 inches” space between table and paragraph in your input document. However, you can set the space between table and paragraph by using ParagraphFormat.SpaceAfter property. Please use the following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "Input_doc.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
System.out.println(doc.getChildNodes(NodeType.TABLE, true).getCount());
for (inti = 0; i < doc.getChildNodes(NodeType.TABLE, true).getCount(); i++)
{
    Table table = (Table)doc.getChild(NodeType.TABLE, i, true);
    if (table.getPreviousSibling() != null) {
        System.out.println("Table has paragraph above it " + table.nodeTypeToString(table.getPreviousSibling().getNodeType()));
        //builder.moveTo(table.getPreviousSibling());
        //builder.insertBreak(BreakType.PARAGRAPH_BREAK);
        //builder.insertBreak((int) 0.16);
        if (table.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH)
        {
            ((Paragraph)table.getPreviousSibling()).getParagraphFormat().setSpaceAfter(ConvertUtil.inchToPoint(0.16));
        }
    }
}

doc.save(*MyDir*+ "Out.docx");

Hi,

I tried with the above code sample but still I am facing some issues.

My input document has paragraph, a table and after there two paragraphs. When I am trying to append the line break with above code, it is working for the paragraph above the table. But the spacing for the text which is after the table is lost (PFA output file). Below is the code which i am using.

for (Paragraph para : (Iterable)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    System.out.println(para.getText() + "doc.getNodeType(); " + doc.getNodeType());
    para.getParagraphFormat().setSpaceBefore(0);
    para.getParagraphFormat().setSpaceAfter(0);
    if (para.isListItem())
    {
        com.aspose.words.List list0 = para.getListFormat().getList();
        ListLevel level0 = list0.getListLevels().get(para.getListFormat().getListLevelNumber());
        ECMUtils.levelNumber = para.getListFormat().getListLevelNumber();
        if (ECMUtils.levelNumber == 0)
        {
            ECMUtils.lineSpacingDecrement = 3.6D;
            if (!(isFirstPara))
            {
                para.getParagraphFormat().setSpaceBefore(ECMUtils.lineSpacingDecrement);
                isFirstPara = true;
            }
        }
        level0.setNumberPosition(ECMUtils.levelNumber \*10);
        level0.setTabPosition(11 + ECMUtils.levelNumber \*10);
        level0.setTrailingCharacter(3);
        level0.setTextPosition(level0.getTabPosition());
        if (ECMUtils.lineSpacingDecrement != -1.44D)
        {
            ECMUtils.lineSpacingDecrement = 3.6D - (ECMUtils.levelNumber \*1.26D);
        }
        para.getParagraphFormat().setSpaceAfter(ECMUtils.lineSpacingDecrement);
    } else {
        isFirstPara = true;
        para.getParagraphFormat().setSpaceBefore(ECMUtils.LINE_SPACING);
        para.getParagraphFormat().setSpaceAfter(ECMUtils.LINE_SPACING);
        //para.getParagraphFormat().setSpaceBefore(0.16);
        //para.getParagraphFormat().setSpaceAfter(0.16);
    }
    System.out.println("para text " + para.getText());
    if (doc.getChildNodes(NodeType.TABLE, true).getCount() > 0)
    {
        for (int i = 0; i < doc.getChildNodes(NodeType.TABLE, true).getCount(); i++)
        {
            Table table = (Table)doc.getChild(NodeType.TABLE, i, true);
            if (table.getPreviousSibling() != null && table.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH)
            {
                para.getParagraphFormat().setSpaceBefore(0);
                para.getParagraphFormat().setSpaceAfter(0);
                ((Paragraph)table.getPreviousSibling()).getParagraphFormat().setSpaceAfter(0.16);
                ((Paragraph)table.getPreviousSibling()).getParagraphFormat().setSpaceBefore(0.16);
            }
        }
    }
}
doc.save(fileNameandPath);

FYR – the values for ECMUtils.levelNumber is 0 and ECMUtils.LINE_SPACING = 3.6

I am creating the input document using html file, input.html

I have attached the input, output and desired output documents.

Please tell where i am going wrong

Hi,

Thanx for your respone.

Here is my requirement.

I am creating a document using html. However the created document is in proper alignment, means the paragraph line spacing is accurate as in the input html file. The html file has two

tags before a

tag which means the generated document also should have two para spaces between the Table. But this is not happening in the output document what i am getting from html. So modify the spaces between the para and table, I am using the above sample code. Even after using the above I am still not able to achieve the actual requirement.

I hope I am clear with my requirements. Please let me know if in case you require any further details on this.

Regards,
Dharmender

Hi Dharmender,

Thanks for sharing the detail. Please use the following code example to achieve your requirements. I have attached the output document with this post for your kind reference.

Document doc = new Document(MyDir + "input.html");
DocumentBuilder builder = new DocumentBuilder(doc);
if (doc.getChildNodes(NodeType.TABLE, true).getCount() > 0)
{
    for (int i = 0; i < doc.getChildNodes(NodeType.TABLE, true).getCount(); i++)
    {
        Table table = (Table)doc.getChild(NodeType.TABLE, i, true);
        if (table.getPreviousSibling() != null && table.getPreviousSibling().getNodeType() == NodeType.PARAGRAPH)
        {
            builder.moveToParagraph(doc.getChildNodes(NodeType.PARAGRAPH, true).indexOf(table.getPreviousSibling()), -1);
            Paragraph para = builder.insertParagraph();
            para.getParagraphFormat().setSpaceAfter(0.0);
            para.getParagraphFormat().setSpaceBefore(0.0);
            para = builder.insertParagraph();
            para.getParagraphFormat().setSpaceAfter(0.0);
            para.getParagraphFormat().setSpaceBefore(0.0);
        }
    }
}
doc.save(MyDir + "Out.docx");