Hi
Thank you for additional information. We will use SharpZipLib to extract content.xml from ODT package. You can download the most recent version of SharpZipLib from here:
http://www.sharpziplib.com/
Then we should build map where key is table name and value is table index. Here is sample code how to achieve this:
[Test]
public void Test001()
{
Hashtable nameIndexMap = GetNameindexMap(@"Test001\in.odt");
// Open document using Aspose.Words.
Document doc = new Document(@"Test001\in.odt");
// Get all tables.
Node[] tables = doc.GetChildNodes(NodeType.Table, true).ToArray();
// For example wee need to get table with name "tabAcq".
Table tabAcq = (Table)tables[(int)nameIndexMap["tabAcq"]];
// Lets set background of this table.
foreach (Row row in tabAcq.Rows)
{
foreach (Cell cell in row.Cells)
cell.CellFormat.Shading.BackgroundPatternColor = Color.Red;
}
// Save output.
doc.Save(@"Test001\out.doc");
}
/// <summary>
/// Returns tables name/index map.
/// </summary>
private Hashtable GetNameindexMap(string fileName)
{
XmlDocument contentXml = GetContentXml(fileName);
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(contentXml.NameTable);
nsmgr.AddNamespace("table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0");
// Get all tables from the content.xml.
XmlNodeList tables = contentXml.SelectNodes("//table:table", nsmgr);
// Create map where key is name of table and value is index of the table.
Hashtable nameIndexMap = new Hashtable();
for (int tableIndex = 0; tableIndex < tables.Count; tableIndex++)
{
string tableName = tables[tableIndex].Attributes["table:name"].Value;
if (!string.IsNullOrEmpty(tableName))
nameIndexMap[tableName] = tableIndex;
}
return nameIndexMap;
}
/// <summary>
/// Extracts content.xml from ODT file.
/// </summary>
private XmlDocument GetContentXml(string fileName)
{
XmlDocument contentXml = new XmlDocument();
using (FileStream odtFile = File.OpenRead(@"Test001\in.odt"))
{
// Read ODT package.
ZipFile odtPackage = new ZipFile(odtFile);
// Look for content.xml entry.
foreach (ZipEntry zipEntry in odtPackage)
{
if (zipEntry.Name.ToLower() == "content.xml")
{
// Extract content.xml entry.
byte[] buffer = new byte[4096];
Stream zipStream = odtPackage.GetInputStream(zipEntry);
using (MemoryStream contentStream = new MemoryStream())
{
StreamUtils.Copy(zipStream, contentStream, buffer);
contentStream.Position = 0;
using (StreamReader contentReader = new StreamReader(contentStream))
contentXml.Load(contentReader);
}
break;
}
}
}
return contentXml;
}
Hope this could help you. Please let me know if you need more assistance, I will be glad to help you.
Best regards,