Showing XHTML text in Word 2007 with the formatting via webservices

D

Darsin

What i am doing is to pull the data from a CMS and import it to Word
2007 Beta and i also have to export the data from Word 2007 Beta back
to that CMS. We have with us two Web Services of the CMS. The Web
Services are explained as follows:



IMPORT WEB SERVICE:



1) This web service pulls the entire data from a particular page
of CMS and returns an XHTML string.

2) I use this web service to grab this XHTML content in a string.
The code to achieve this is given below:

String newEntireRichContent =
objwsRichContent.GetRichTextContent(frmServer.authenticationToken,
node);

"objwsRichContent.GetRichTextContent()" is a web service method
which takes 2 parameters i.e. the "authenticationToken" and
"node". Here "node" is the page number. Based on this page
number the web service returns the entire text on that page in the form
of XHTML string. I take this content in a "string" variable
(newEntireRichContent).

3) Now I create a Word Range object as follows:

Word.Range currentRange =
Globals.ThisAddIn.Application.Selection.Range;

4) After making the word range object, I assign the XHTML string
to it as follows:

currentRange.Text = newEntireRichContent;

5) Now,what I get on the word document is the origninal
text,something like this:

<p align="left"><strong><span style="COLOR: #ff0000">Synkron Via CMS<br
/></span><span style="COLOR: #008000">This is a new text for services
page.<br /></span><span style="COLOR: #0000ff"><em><u>The main idea
behind developing this text is to test the web services.<br /><br
/></u></em></span></strong></p><h1>FORMULA<strong><span style="COLOR:
#0000ff"><br />(a+b)<sup>2</sup>=(a<sup>2</sup>+2ab+b<

and so on.....
6) But what I want is that instead of giving pure XHTML string. It
should be able to present the data in a formatted way based on the tags
present in the XHTML string. For example the heading tag should present
the data in a form of a heading and not with <h1> tags. the texts
between the <b></b> tags should be bold and so on.

7) Once import is implemented properly I want the same to happen
with the EXPORT functionality. In the export functionality it should
take the data from Word 2007 and then convert it into properly formed
XHTML string.

Could anybody please provide some help that how this can be achieved ?
I have posted this into multiple groups as i dont really know which
group would be the right one for this.
the complete code is provided below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using mshtml;
using Word = Microsoft.Office.Interop.Word;
using ContentWebService = global::Synkronv10.ContentWebService;
//Content WebService
using SiteStructureWebService =
global::Synkronv10.SiteStructureWebService; //Site Structure WebService

namespace Synkronv10
{
public partial class frmImport : Form
{
public static string entireRichContent;
public static string selectedNode;
public static int node;

public frmImport()
{
InitializeComponent();
}

#region PopulatingTree

#region Form Load Function
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmImport_Load(object sender, EventArgs e)
{
try
{
// SECTION 1. Create a DOM Document and load the
XML data into it.
XmlDocument dom = new XmlDocument();
SiteStructureWebService.SiteStructure
objwsStructure = new
Synkronv10.SiteStructureWebService.SiteStructure();

string xml =
objwsStructure.GetStructureXML(frmServer.authenticationToken);
dom.LoadXml(xml);

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new
TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM
nodes.
AddNode(dom.DocumentElement, tNode);
treeView1.ExpandAll();

}
catch { }
}
#endregion

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping
process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes;
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes;

AddNode(xNode, tNode);
if (xNode.Name.ToLower() == "page")
{
tNode.Tag =
xNode.Attributes["ElementId"].Value.ToString();
tNode.Text =
xNode.Attributes["Name"].Value.ToString();
}
if (tNode.Text.StartsWith("<![CDATA"))
{
int sIndex = ((tNode.Text.IndexOf("[", 3))+ 1);
int eIndex = tNode.Text.IndexOf("]", sIndex);
tNode.Text = tNode.Text.Substring(sIndex,
(eIndex - sIndex)); //Showing the Inner text of CDATA tag

}
}
}
else
{
// Here you need to pull the data from the XmlNode
based on the
// type of node, whether attribute values are required,
and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}

#endregion

private void btnImport_Click(object sender, EventArgs e)
{
try
{
selectedNode = treeView1.SelectedNode.Tag.ToString();

//Populating the Word document with text through
WebService
ContentWebService.Content objwsRichContent = new
Synkronv10.ContentWebService.Content();
entireRichContent =
objwsRichContent.GetPageContentXML(frmServer.authenticationToken,
Convert.ToInt32(selectedNode));
XmlDocument objXML = new XmlDocument();
objXML.LoadXml(entireRichContent);
string newEntireRichContent = string.Empty;

//Taking out the first element from the document
XmlNodeList objNode =
objXML.GetElementsByTagName("ContentElement");
node =
Convert.ToInt32(objNode.Item(0).Attributes["ElementId"].Value);
newEntireRichContent =
objwsRichContent.GetRichTextContent(frmServer.authenticationToken,
node);

Word.Range currentRange =
Globals.ThisAddIn.Application.Selection.Range;
currentRange.Text = newEntireRichContent;

frmImport.ActiveForm.Visible= false;
}

catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void btnCancel_Click(object sender, EventArgs e)
{
frmImport.ActiveForm.Visible = false;
}

private void treeView1_AfterSelect(object sender,
TreeViewEventArgs e)
{

}

}
}
 
R

RobinS

I'd try posting this to microsoft.public.dotnet.languages.csharp.
It's a much more active newsgroup, and you'll probably get some help
there.

Robin S.
---------------------------
Darsin said:
What i am doing is to pull the data from a CMS and import it to Word
2007 Beta and i also have to export the data from Word 2007 Beta back
to that CMS. We have with us two Web Services of the CMS. The Web
Services are explained as follows:



IMPORT WEB SERVICE:



1) This web service pulls the entire data from a particular page
of CMS and returns an XHTML string.

2) I use this web service to grab this XHTML content in a string.
The code to achieve this is given below:

String newEntireRichContent =
objwsRichContent.GetRichTextContent(frmServer.authenticationToken,
node);

"objwsRichContent.GetRichTextContent()" is a web service method
which takes 2 parameters i.e. the "authenticationToken" and
"node". Here "node" is the page number. Based on this page
number the web service returns the entire text on that page in the
form
of XHTML string. I take this content in a "string" variable
(newEntireRichContent).

3) Now I create a Word Range object as follows:

Word.Range currentRange =
Globals.ThisAddIn.Application.Selection.Range;

4) After making the word range object, I assign the XHTML string
to it as follows:

currentRange.Text = newEntireRichContent;

5) Now,what I get on the word document is the origninal
text,something like this:

<p align="left"><strong><span style="COLOR: #ff0000">Synkron Via
CMS<br
/></span><span style="COLOR: #008000">This is a new text for services
page.<br /></span><span style="COLOR: #0000ff"><em><u>The main idea
behind developing this text is to test the web services.<br /><br
/></u></em></span></strong></p><h1>FORMULA<strong><span style="COLOR:
#0000ff"><br />(a+b)<sup>2</sup>=(a<sup>2</sup>+2ab+b<

and so on.....
6) But what I want is that instead of giving pure XHTML string.
It
should be able to present the data in a formatted way based on the
tags
present in the XHTML string. For example the heading tag should
present
the data in a form of a heading and not with <h1> tags. the texts
between the <b></b> tags should be bold and so on.

7) Once import is implemented properly I want the same to happen
with the EXPORT functionality. In the export functionality it should
take the data from Word 2007 and then convert it into properly formed
XHTML string.

Could anybody please provide some help that how this can be achieved ?
I have posted this into multiple groups as i dont really know which
group would be the right one for this.
the complete code is provided below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using mshtml;
using Word = Microsoft.Office.Interop.Word;
using ContentWebService = global::Synkronv10.ContentWebService;
//Content WebService
using SiteStructureWebService =
global::Synkronv10.SiteStructureWebService; //Site Structure
WebService

namespace Synkronv10
{
public partial class frmImport : Form
{
public static string entireRichContent;
public static string selectedNode;
public static int node;

public frmImport()
{
InitializeComponent();
}

#region PopulatingTree

#region Form Load Function
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmImport_Load(object sender, EventArgs e)
{
try
{
// SECTION 1. Create a DOM Document and load the
XML data into it.
XmlDocument dom = new XmlDocument();
SiteStructureWebService.SiteStructure
objwsStructure = new
Synkronv10.SiteStructureWebService.SiteStructure();

string xml =
objwsStructure.GetStructureXML(frmServer.authenticationToken);
dom.LoadXml(xml);

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new
TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM
nodes.
AddNode(dom.DocumentElement, tNode);
treeView1.ExpandAll();

}
catch { }
}
#endregion

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping
process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes;
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes;

AddNode(xNode, tNode);
if (xNode.Name.ToLower() == "page")
{
tNode.Tag =
xNode.Attributes["ElementId"].Value.ToString();
tNode.Text =
xNode.Attributes["Name"].Value.ToString();
}
if (tNode.Text.StartsWith("<![CDATA"))
{
int sIndex = ((tNode.Text.IndexOf("[", 3))+ 1);
int eIndex = tNode.Text.IndexOf("]", sIndex);
tNode.Text = tNode.Text.Substring(sIndex,
(eIndex - sIndex)); //Showing the Inner text of CDATA tag

}
}
}
else
{
// Here you need to pull the data from the XmlNode
based on the
// type of node, whether attribute values are required,
and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}

#endregion

private void btnImport_Click(object sender, EventArgs e)
{
try
{
selectedNode = treeView1.SelectedNode.Tag.ToString();

//Populating the Word document with text through
WebService
ContentWebService.Content objwsRichContent = new
Synkronv10.ContentWebService.Content();
entireRichContent =
objwsRichContent.GetPageContentXML(frmServer.authenticationToken,
Convert.ToInt32(selectedNode));
XmlDocument objXML = new XmlDocument();
objXML.LoadXml(entireRichContent);
string newEntireRichContent = string.Empty;

//Taking out the first element from the document
XmlNodeList objNode =
objXML.GetElementsByTagName("ContentElement");
node =
Convert.ToInt32(objNode.Item(0).Attributes["ElementId"].Value);
newEntireRichContent =
objwsRichContent.GetRichTextContent(frmServer.authenticationToken,
node);

Word.Range currentRange =
Globals.ThisAddIn.Application.Selection.Range;
currentRange.Text = newEntireRichContent;

frmImport.ActiveForm.Visible= false;
}

catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void btnCancel_Click(object sender, EventArgs e)
{
frmImport.ActiveForm.Visible = false;
}

private void treeView1_AfterSelect(object sender,
TreeViewEventArgs e)
{

}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top