Tom Clarkson 的个人资料Tom Clarkson's Blog照片日志列表 工具 帮助
11月26日

Convert Word HTML to Infopath XHTML

Sometimes 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(

"&nbsp;", " ");

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)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

没有名字发表:
http://www.bestlaptopbattery.com.au/dell/1501.htm Dell 1501 battery
http://www.bestlaptopbattery.com.au/dell/1520.htm Dell 1520 battery
http://www.bestlaptopbattery.com.au/dell/1691p.htm Dell 1691p battery
http://www.bestlaptopbattery.com.au/dell/1700-.htm Dell 1700 battery
http://www.bestlaptopbattery.com.au/dell/1720.htm Dell 1720 battery
http://www.bestlaptopbattery.com.au/dell/1721.htm Dell 1721 battery
http://www.bestlaptopbattery.com.au/dell/312-0058.htm Dell 312-0058 battery
http://www.bestlaptopbattery.com.au/dell/312-0273.htm Dell 312-0273 battery
http://www.bestlaptopbattery.com.au/dell/312-038.htm Dell 312-038 battery
http://www.bestlaptopbattery.com.au/dell/312-039.htm Dell 312-039 battery
http://www.bestlaptopbattery.com.au/dell/312-0417.htm Dell 312-0417 battery
http://www.bestlaptopbattery.com.au/dell/312-0543.htm Dell 312-0543 battery
http://www.bestlaptopbattery.com.au/dell/312-0566.htm Dell 312-0566 battery
http://www.bestlaptopbattery.com.au/dell/312-0567.htm Dell 312-0567 battery
http://www.bestlaptopbattery.com.au/dell/312-0584.htm Dell 312-0584 battery
http://www.bestlaptopbattery.com.au/dell/312-0585.htm Dell 312-0585 battery
http://www.bestlaptopbattery.com.au/dell/312-0625.htm Dell 312-0625 battery
http://www.bestlaptopbattery.com.au/dell/312-0633.htm Dell 312-0633 battery
http://www.bestlaptopbattery.com.au/dell/312-0831.htm Dell 312-0831 battery
http://www.bestlaptopbattery.com.au/dell/451-10180.htm Dell 451-10180 battery
http://www.bestlaptopbattery.com.au/dell/451-10473.htm Dell 451-10473 battery
http://www.bestlaptopbattery.com.au/dell/451-10474.htm Dell 451-10474 battery
http://www.bestlaptopbattery.com.au/dell/4e369.htm Dell 4e369 battery
http://www.bestlaptopbattery.com.au/dell/7012p.htm Dell 7012p battery
http://www.bestlaptopbattery.com.au/dell/7k330.htm Dell 7k330 battery
http://www.bestlaptopbattery.com.au/dell/8u443.htm Dell 8u443 battery
http://www.bestlaptopbattery.com.au/dell/9200.htm Dell 9200 battery
http://www.bestlaptopbattery.com.au/dell/9300.htm Dell 9300 battery
http://www.bestlaptopbattery.com.au/dell/9400.htm Dell 9400 battery
http://www.bestlaptopbattery.com.au/dell/batft00l4.htm Dell batft00l4 battery
http://www.bestlaptopbattery.com.au/dell/batft00l6.htm Dell batft00l6 battery
http://www.bestlaptopbattery.com.au/dell/c2174.htm Dell c2174 battery
http://www.bestlaptopbattery.com.au/dell/cg036.htm Dell cg036 battery
http://www.bestlaptopbattery.com.au/dell/d044h.htm Dell d044h battery
http://www.bestlaptopbattery.com.au/dell/d400.htm Dell d400 battery
http://www.bestlaptopbattery.com.au/dell/d5318.htm Dell d5318 battery
http://www.bestlaptopbattery.com.au/dell/e1405.htm Dell e1405 battery
http://www.bestlaptopbattery.com.au/dell/e1505.htm Dell e1505 battery
http://www.bestlaptopbattery.com.au/dell/e1705.htm Dell e1705 battery
http://www.bestlaptopbattery.com.au/dell/f1244.htm Dell f1244 battery
http://www.bestlaptopbattery.com.au/dell/fk890.htm Dell fk890 battery
http://www.bestlaptopbattery.com.au/dell/g1947.htm Dell g1947 battery
http://www.bestlaptopbattery.com.au/dell/g5260.htm Dell g5260 battery
http://www.bestlaptopbattery.com.au/dell/gd761.htm Dell gd761 battery
http://www.bestlaptopbattery.com.au/laptop-ac-adapter/liteon/liteon-20V-6A-120w-4-pin.php Liteon 20V 6A 120W 4 PIN adapter
http://www.bestlaptopbattery.com.au/laptop-ac-adapter/samsung/samsung-19V-3.16A-60w-5.5mm-3.4mm-pin-inside.php Samsung SPA-P30 AD-6019 19V 3.16A 60W 5.5MM*3.4MM PIN INSIDE adapter
http://www.bestlaptopbattery.com.au/laptop-ac-adapter/samsung/samsung-19V-4.74A-90w-5.5mm-3.0mm.php Samsung PCGA-AC19V3 19V 4.74A 90W 5.5MM*3.0MM adapter
http://www.bestlaptopbattery.com.au/laptop-ac-adapter/sony/sony-19.5V-4.7A-90w-6.5mm-4.4mm.php Sony PCGA-AC19V 19.5V 4.7A 90W 6.5MM*4.4MM adapter
http://www.bestlaptopbattery.com.au/laptop-ac-adapter/sony/sony-16V-4A-65w-6.5mm-4.4mm.php Sony 16V 4A 65W 6.5MM*4.4MM PCGA-AC16V4 adapter
11 月 21 日
没有名字发表:
您需要二手液晶显示屏废旧液晶屏么?我们是不折不扣的二手液晶屏、旧液晶屏大批发商,长期大量供应可再利用的旧液晶屏。我公司提供的各种尺寸的二手液晶屏, 不同厚薄如笔记本屏,均已经过我们严格的分类,检验,测试流程。请访问协力液晶屏www.sceondhandlcd.com[dcjfdfcafaeeggcc]
12 月 2 日
11 月 1 日
10 月 26 日
没有名字发表:

Hi,Do you need digital signages, advertising displays, digital sign, advertisement displays and advertising players? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

amberdigital Contact Us

website:www.amberdigital.com.hk
alibaba:amberdigital.en.alibaba.com[cedih

10 月 18 日
没有名字发表:

Hi,Do you need advertising displays, advertisement screens, LCD digital signage and LCD signages? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

amberdigital Contact Us

website:www.amberdigital.com.hk
alibaba:amberdigital.en.alibaba.com[fhfdhhdaedegbi]

9 月 23 日
没有名字发表:
To the global wow gold the cheapest wow power leveling under the cheapest single-site! -20374317785779
9 月 16 日
没有名字发表:
A friend to buy wow goldTo wow power leveling?On the world's most concessions to the most reputable sites under the single!
9 月 16 日
没有名字发表:

Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of advertising displays, advertising player and LCD displays. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

amberdigital Contact Us
Southern Stars Enterprises Co (Hong Kong Office)
Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
Tel:+852 2681 4099
Fax:+852 2681 4586

Southern Stars Enterprises Co (Shenzhen Office)
Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
Tel:+86 755 2592 9100
Fax:+86 755 2592 7171

E-mail:sstar@netvigator.com
website:www.amberdigital.com.hk
alibaba:amberdigital.en.alibaba.com[ccbbbaighe

8 月 27 日
10 月 7 日
10 月 3 日
没有名字发表:
Great article.

Thanks!
9 月 7 日
ChiversIan发表:
Hi,
 
I'm trying to write an image to an Infopath RTB in c#.  I used your example above, but the image doesn't display in IP 2003 SP2.  I noticed that when .NET saved the source GIF image as PNG it's file size was bigger that when I used Paint Shop Pro to save it as a PNG.
 
Also, when I looked at the source HTML it looked like it was padded with 'A' characters.  I read the memory stream instead of using GetBuffer() but still didn't display in InfoPath.
 
When I paste a GIF image in InfoPath and look at the source XML the xd:inline begins with R0lG which indicates its a GIF.
 
I'd be very grateful if you can offer any advise.  This is the only useful information I've found on the web regarding this subject.
 
Cheers,
Ian.
12 月 22 日
匿名 的图片
Kylie 发表:
You mention that cut and paste from Word to an Infopath Rich Text Box works for you. However, when I do it, and then try to convert the XML file generated by the Infopath form (with xslt and C#) I get a bunch of weird characters in the resulting html that don't display if I open the xml file in Infopath or notepad. The characters are A's with accent marks (so that it looks like hats). From what I've read, this is because the xml itself claims to be encoded in UTF-8, while that part is really iso-8859-1. Do you know of a way to get rid of this encoding difference?

I have found that if you open the html file in notepad, hit save, and close it, they are gone. However, I'd like to have a programmatic solution as we have many of these xml files with cut and paste from Word, and that update regularly through Infopath.

Hope you don't mind me asking on here... I've had no luck with the internet on any ideas for this so far. And feel free to email me: kyillee at hotmail.
2 月 10 日
匿名 的图片
Tom Clarkson 发表:
No, never been there. I'm also not a 19th century abolitionist or a director of supply chain technology.
12 月 1 日
匿名 的图片
Caroline Mallette 发表:
Would this happen to be the Tom Clarkson that was an exchange student to Kirkland Lake?
11 月 30 日

引用通告 (5)

此日志的引用通告 URL 是:
http://tomclarkson.spaces.live.com/blog/cns!4EC8811DD2D16631!179.trak
引用此项的网络日志