Aspose Word and Microsoft Word Table type conflict


#1

I want to check that all tables in a document have a table title. Please see the image below for the error details: unable to cast object of type Aspose.Words.Tables.Table to type Microsoft.Office.Interop.Word.Table. How else can I achieve this please?

table title.PNG (33.5 KB)


#2

@SCDGLC

Please change the Table in foreach loop with Aspose.Words.Tables.Table. Hope this helps you.


#3

I had already tried that and found that I couldn’t access the title property. How do I access it from an Aspose table please?

Also I need to be able to insert an entire file into the row of a table and was unsure the best way to go about, i.e. do I copy the contents of one file to insert into the table row or can I simply insert the whole file? Also what happens if the file to be inserted is a single table row (i.e. a table with one row), how do I insert that entire row into a table in another document with the same columns?

Many thanks


#4

@SCDGLC

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Please move the cursor to the desired table’s cell and insert the document using DocumentBuilder.InsertDocument method.


#5

Apologies for the delayed reply, I have been waiting on someone to provide me with proper sample data but it just hasn’t happened, so I’ve created a tiny sample of how I’m presuming it will be.

Attached are 2 documents. I want to add the entire contents of the insertion document into the first empty row of the table in the main document that has the title “Test Table 2”.

Many thanks for your assistance.

TableInsertionTest.zip (19.4 KB)


#6

@SCDGLC

Please use the following code example to add table’s row from one document into another document.

Document dstDoc = new Document(MyDir + "Test_Main Doc.docx");
Document srcDoc = new Document(MyDir + "Test_Insertion Doc.docx");

//Get the first table 
Table srcTable = srcDoc.FirstSection.Body.Tables[0];

//Get the second table from destination document
Table dstTable = dstDoc.FirstSection.Body.Tables[1];

NodeImporter imp = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);
Node impNode = imp.ImportNode(srcTable.FirstRow, true);
dstTable.Rows.Add(impNode);

dstDoc.Save(MyDir + "output.docx");

#7

Thanks so much that is very helpful, however I will not know at runtime how many tables are in the destination document nor which order the tables are in, I will only know the title of the table so I will need to be able to identify it via the title not the order number.


#8

@SCDGLC

You can implement IReplacingCallback interface and find the matched node (Run node) of table’s title. Once you have node of table’s title, you can get the table using Node.NextSibling property. We suggest you please read the following articles.
Aspose.Words Document Object Model
Find and Replace


#9

Thank you however I’m still unsure how to implement this. I have attached 2 documents as examples to show you what I am trying to achieve.

For the purposes of this exercise let’s say I want to insert the contents of the Row Sample document into the table with the title ‘Table Two Title’ in the Table Sample document.

Table Sample.zip (25.4 KB)


#10

@SCDGLC

Following code example shows how to find the text and insert the table’s row from one document into another. Hope this helps you.

Document doc1 = new Document(MyDir + "Table Sample.docx");
Document doc2 = new Document(MyDir + "Row Sample.docx");

FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.ReplacingCallback = new FindAndInsertRow(doc2.FirstSection.Body.Tables[0].Rows[0]);
doc1.Range.Replace("Blah, blah, blah", "", findReplaceOptions);

doc1.Save(MyDir + "output.docx");

public class FindAndInsertRow : IReplacingCallback
{
    Row row;
    public FindAndInsertRow(Row row)
    {
        this.row = row;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Node node = e.MatchNode;
        //Get the parent paragrpah of matached node.
        node = e.MatchNode.ParentNode;

        if (node.NextSibling.NodeType == NodeType.Paragraph && node.NextSibling.ToString(SaveFormat.Text).Trim() == "")
            node = node.NextSibling.NextSibling;

        if (node.NodeType == NodeType.Table)
        {
            Table table = (Table)node;
            //NodeImporter imp = new NodeImporter((Document)table.Document, (Document)this.row.Document, ImportFormatMode.KeepSourceFormatting);
            NodeImporter imp = new NodeImporter((Document)this.row.Document, (Document)table.Document, ImportFormatMode.KeepSourceFormatting);
            Node impNode = imp.ImportNode(this.row, true);
            table.Rows.Add(impNode);
        }   
        return ReplaceAction.Skip;
    }
}

#11

Many thanks, however I don’t have Aspose.Words.Replacing available to me as Replacing doesn’t exist under Words in my application?


#12

@SCDGLC

We suggest you please use the latest version Aspose.Words for .NET 19.7.


#13

I will find out if that can be done. But if not, is it not possible to achieve this on an older licence then?


#14

@SCDGLC

Please note that we do not provide support for older released versions of Aspose.Words. Moreover, we do not provide any fixes or patches for old versions of Aspose products either. All fixes and new features are always added into new versions of our products.


#15

Ok, thanks for clarifying and your help, hopefully we can upgrade.


#16

@SCDGLC

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.


#17

Just one more query, regarding your last code sample, you don’t appear to be checking for the table title as per my requirements. Please can you clarify how that works? Thanks


#18

@SCDGLC

In your document, the table title is the paragraph before the table. The code example finds the table’s title and insert the table’s row. Please refer to the following article.
Find and Replace

Could you please share some more detail about your requirement and expected output document? We will then provide you more information about your query along with code example.


#19

No my table title is not the paragraph before the table. Title is actually a property of a table within Word. If you right click and look at the properties of the table you will see the title.


#20

@SCDGLC

Thanks for sharing the detail. In this case, we suggest you following solution.

  1. Iterate over table nodes.
  2. Get the table’s title using Table.Title property.
  3. If the table’s title matches your desired title, import the table’s row from one document to another using NodeImporter.ImportNode.

Hope this helps you.