public static void RenameMasterStyles(XmlDocument styleDocument, XmlDocument contentDocument, XmlNamespaceManager namespaceMng)
{
try
{
// Rename style names used in inside the master page
// contents. This is necessary since OpenOffice create
// duplicate style names.
XmlNode automaticStyles = styleDocument.SelectSingleNode(
"//office:automatic-styles", namespaceMng);
XmlNode masterStyles = styleDocument.SelectSingleNode(
"//office:master-styles", namespaceMng);
if (automaticStyles != null && masterStyles != null && automaticStyles.HasChildNodes)
{
foreach(XmlNode styleNode in automaticStyles.ChildNodes)
{
if (styleNode.Attributes["style:name"] != null)
{
// Look for associated content inside header and footer
string styleName = styleNode.Attributes["style:name"].Value;
string family = "text"; // default text
XmlNode contentNode = null;
if (styleNode.Attributes["style:family"] != null && styleNode.Attributes["style:family"].Value.StartsWith("table"))
{
contentNode = masterStyles.SelectSingleNode(
"//*[@table:style-name = '" + styleName + "']", namespaceMng);
family = "table";
}
else
{
string xpath = "//*[@text:style-name = '" + styleName + "']";
contentNode = masterStyles.SelectSingleNode(
xpath, namespaceMng);
}
if (contentNode != null)
{
// This style name has to be changed
string master = "master";
styleNode.Attributes["style:name"].Value = master + styleName;
contentNode.Attributes[family + ":style-name"].Value = master + styleName;
}
}
}
// Now, we move this nodes to avoid xml owner document errors
MasterPageFactory.MoveMasterStyles(contentDocument, automaticStyles, masterStyles, namespaceMng);
}
}
catch(Exception)
{
throw;
}
}