| Tom Clarkson 的个人资料Tom Clarkson's Blog照片日志列表 | 帮助 |
|
11月26日 Convert Word HTML to Infopath XHTMLSometimes making a program do something is a lot harder than doing it by hand, but you have to do it hundreds of times so you don't have much choice. For moving formatted text between Word and an InfoPath rich text box, copy and paste works quite well. Unfortunately, pasting to an InfoPath control from managed code is both difficult to get working and completely useless for a process that needs to run on a server. After trying several different approaches that didn't quite work, I came up with this code.
It uses the clipboard to obtain Word HTML for a range, uses HTML Tidy and some string replacement to get standard XHTML, then loads the XHTML as an XmlDocument and changes any image elements to the inline format used by InfoPath.
The code is C# .NET 2.0, and although I've only tested it with InfoPath 12, it should be ok for 2003 also.
public XhtmlString WordToInfoPath(Range range)
{ range.Copy(); string s = (string)Clipboard.GetData("HTML Format");s = s.Replace( "o:", "");s = s.Substring(s.IndexOf( "<body"));s = s.Substring(0, s.IndexOf( "</body>") + "</body>".Length);Tidy. Document tdoc = new Tidy.Document();tdoc.ParseString(s); tdoc.SetOptBool(Tidy. TidyOptionId.TidyXhtmlOut, 1);tdoc.SetOptInt(Tidy. TidyOptionId.TidyHideComments, 1);tdoc.SetOptInt(Tidy. TidyOptionId.TidyDropPropAttrs, 1);tdoc.CleanAndRepair(); s = tdoc.SaveString(); s = s.Substring(s.IndexOf( "<body") + "<body".Length);s = s.Substring(s.IndexOf( ">") + ">".Length);s = s.Substring(0, s.IndexOf( "</body>"));s = s.Replace( " ", " ");s = s.Replace( "<br />", "");s = s.Replace( "<![if !vml]>", "");s = s.Replace( "<![endif]>", "");s = "<span xmlns=\"http://www.w3.org/1999/xhtml\">" + s + "</span>";XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(s); XPathNavigator xpn = xdoc.CreateNavigator(); XPathNodeIterator xpni = xpn.SelectDescendants("img", "http://www.w3.org/1999/xhtml", false); foreach (XPathNavigator imgel in xpni) { string oldsrc = imgel.GetAttribute("src", "");System.Net. WebRequest wreq = System.Net.WebRequest.Create(oldsrc);System.Net. WebResponse wres = wreq.GetResponse();System.IO. Stream stream = wres.GetResponseStream(); Image img = Image.FromStream(stream);System.IO. MemoryStream ms = new System.IO.MemoryStream();img.Save(ms, System.Drawing.Imaging. ImageFormat.Png); Guid guid = Guid.NewGuid(); string newsrc = "msoinline/" + guid.ToString("N"); string imgdata = Convert.ToBase64String(ms.GetBuffer(), Base64FormattingOptions.None);ms.Close(); stream.Close(); wres.Close(); imgel.CreateAttribute( "xd", "inline", "http://schemas.microsoft.com/office/infopath/2003", imgdata);imgel.MoveToAttribute( "src", "");imgel.SetValue(newsrc); } return new XhtmlString(xdoc.InnerXml);} 评论 (16)
引用通告 (5)此日志的引用通告 URL 是: http://tomclarkson.spaces.live.com/blog/cns!4EC8811DD2D16631!179.trak 引用此项的网络日志
|
|
|