在Word转成的XML指定书签中插入图像
在Word转成的XML指定书签中插入图像
在XML中图像的格式如下
其中图像以 ToBase64String 编码存在
在Word转成的XML指定书签中插入图像
在XML中图像的格式如下
其中图像以 ToBase64String 编码存在
完整代码
我做成了一个dll,可以直接调用
https://files.cnblogs.com/4color/HoYaiWordXml.dll.zip
在XML中图像的格式如下
其中图像以 ToBase64String 编码存在
Code
<w:r>
<w:pict>
<v:shapetype id="_x0000_t75" >
</v:shapetype>
<w:binData w:name="wordml://03000001.png">ToBase64String</w:binData>
<v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:138pt;height:28.5pt">
<v:imagedata src="wordml://03000001.png" />
</v:shape>
</w:pict>
</w:r>
<w:r>
<w:pict>
<v:shapetype id="_x0000_t75" >
</v:shapetype>
<w:binData w:name="wordml://03000001.png">ToBase64String</w:binData>
<v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:138pt;height:28.5pt">
<v:imagedata src="wordml://03000001.png" />
</v:shape>
</w:pict>
</w:r>
完整代码
Code
#region 在XML文档中插入图像
/// <summary>
/// 在XML文档中插入图像
/// </summary>
/// <param name="ImagePath"></param>
/// <param name="WordPath"></param>
/// <returns></returns>
public bool InsertImage(string ImagePath,string WordPath,string SourceBookMarkName)
{
//打开XML文档
XmlDocument xml = new XmlDocument();
xml.Load(WordPath);
XmlNameTable t = xml.NameTable;
//查找书签
XmlNamespaceManager ns = new XmlNamespaceManager(t);
ns.AddNamespace("aml", "http://schemas.microsoft.com/aml/2001/core");
ns.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml");
//得到 书签标志
XmlNodeList xnodelist = xml.DocumentElement.SelectNodes("//aml:annotation", ns);
//书签名称
string BookMarkName = "";
foreach (XmlNode xnode in xnodelist)
{
if (xnode.Attributes["w:type"] != null && xnode.Attributes["w:type"].Value == "Word.Bookmark.Start")
{
BookMarkName = xnode.Attributes["w:name"].Value;
if (BookMarkName == SourceBookMarkName)
{
//内容部分 XmlNode xNodeContent
XmlNode xNodeContent = xnode.NextSibling;
//即将去掉 XmlNode
XmlNode xOldChild = xnode.NextSibling.LastChild;
string strURI = xOldChild.NamespaceURI;
string strflag = xnode.NextSibling.LastChild.Name;
XmlNode xNewChild = xNodeContent.OwnerDocument.CreateNode(XmlNodeType.Element, strflag, strURI);
xNewChild = CreateBarCode(xNewChild, ImagePath, strURI);
xNodeContent.ReplaceChild(xNewChild, xOldChild);
}
}
if (xnode.Attributes["w:type"] != null && xnode.Attributes["w:type"].Value == "Word.Bookmark.End")
{
xnode.RemoveAll();
}
}
xml.Save(WordPath);
return true;
}
private XmlNode CreateBarCode(XmlNode node, string spath, string strURI)
{
#region 格式说明 在xml中图像格式如下
//<w:r>
// <w:pict>
// <v:shapetype id="_x0000_t75" >
// </v:shapetype>
// <w:binData w:name="wordml://03000001.png">二进制内容</w:binData>
// <v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:138pt;height:28.5pt">
// <v:imagedata src="wordml://03000001.png" />
// </v:shape>
// </w:pict>
//</w:r>
#endregion
if (spath != "")
{
string ImageCode = Module64String(spath);
Image img = Bitmap.FromFile(spath);
int w = img.Width;
int h = img.Height;
img.Dispose();
XmlElement pict = node.OwnerDocument.CreateElement("w:pict", strURI);
XmlElement shapetype = node.OwnerDocument.CreateElement("v", "shapetype", "urn:schemas-microsoft-com:vml");
XmlAttribute id = node.OwnerDocument.CreateAttribute("id");
id.Value = "_x0000_t75";
shapetype.Attributes.Append(id);
XmlElement binData = node.OwnerDocument.CreateElement("w:binData", strURI);
XmlAttribute wName = node.OwnerDocument.CreateAttribute("w:name", strURI);
wName.Value = "wordml://03000001.png";
binData.Attributes.Append(wName);
binData.InnerText = ImageCode;
XmlElement shape = node.OwnerDocument.CreateElement("v", "shape", "urn:schemas-microsoft-com:vml");
XmlAttribute style = node.OwnerDocument.CreateAttribute("style");
style.Value = "width:" + w + "px;height:" + h + "px";
XmlElement imagedata = node.OwnerDocument.CreateElement("v", "imagedata", "urn:schemas-microsoft-com:vml");
XmlAttribute src = node.OwnerDocument.CreateAttribute("src");
src.Value = "wordml://03000001.png";
imagedata.Attributes.Append(src);
XmlAttribute shapeid = node.OwnerDocument.CreateAttribute("id");
shapeid.Value = "_x0000_t75";
shape.Attributes.Append(shapeid);
shape.Attributes.Append(style);
shape.AppendChild(imagedata);
pict.AppendChild(shapetype);
pict.AppendChild(binData);
pict.AppendChild(shape);
//加载完成后删除图片
try
{
//File.Delete(spath);
}
catch
{
}
return pict;
}
else
{
return null;
}
}
/// <summary>
/// 对图像进制编码
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
private string Module64String(string strPath)
{
FileStream fs = new FileStream(strPath, FileMode.Open);
byte[] b = new Byte[fs.Length];
fs.Position = 0;
long l = fs.Read(b, 0, (int)fs.Length);
fs.Close();
String strbaser64 = Convert.ToBase64String(b);
fs.Close();
fs.Dispose();
return strbaser64.ToString();
}
#endregion
#region 在XML文档中插入图像
/// <summary>
/// 在XML文档中插入图像
/// </summary>
/// <param name="ImagePath"></param>
/// <param name="WordPath"></param>
/// <returns></returns>
public bool InsertImage(string ImagePath,string WordPath,string SourceBookMarkName)
{
//打开XML文档
XmlDocument xml = new XmlDocument();
xml.Load(WordPath);
XmlNameTable t = xml.NameTable;
//查找书签
XmlNamespaceManager ns = new XmlNamespaceManager(t);
ns.AddNamespace("aml", "http://schemas.microsoft.com/aml/2001/core");
ns.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml");
//得到 书签标志
XmlNodeList xnodelist = xml.DocumentElement.SelectNodes("//aml:annotation", ns);
//书签名称
string BookMarkName = "";
foreach (XmlNode xnode in xnodelist)
{
if (xnode.Attributes["w:type"] != null && xnode.Attributes["w:type"].Value == "Word.Bookmark.Start")
{
BookMarkName = xnode.Attributes["w:name"].Value;
if (BookMarkName == SourceBookMarkName)
{
//内容部分 XmlNode xNodeContent
XmlNode xNodeContent = xnode.NextSibling;
//即将去掉 XmlNode
XmlNode xOldChild = xnode.NextSibling.LastChild;
string strURI = xOldChild.NamespaceURI;
string strflag = xnode.NextSibling.LastChild.Name;
XmlNode xNewChild = xNodeContent.OwnerDocument.CreateNode(XmlNodeType.Element, strflag, strURI);
xNewChild = CreateBarCode(xNewChild, ImagePath, strURI);
xNodeContent.ReplaceChild(xNewChild, xOldChild);
}
}
if (xnode.Attributes["w:type"] != null && xnode.Attributes["w:type"].Value == "Word.Bookmark.End")
{
xnode.RemoveAll();
}
}
xml.Save(WordPath);
return true;
}
private XmlNode CreateBarCode(XmlNode node, string spath, string strURI)
{
#region 格式说明 在xml中图像格式如下
//<w:r>
// <w:pict>
// <v:shapetype id="_x0000_t75" >
// </v:shapetype>
// <w:binData w:name="wordml://03000001.png">二进制内容</w:binData>
// <v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:138pt;height:28.5pt">
// <v:imagedata src="wordml://03000001.png" />
// </v:shape>
// </w:pict>
//</w:r>
#endregion
if (spath != "")
{
string ImageCode = Module64String(spath);
Image img = Bitmap.FromFile(spath);
int w = img.Width;
int h = img.Height;
img.Dispose();
XmlElement pict = node.OwnerDocument.CreateElement("w:pict", strURI);
XmlElement shapetype = node.OwnerDocument.CreateElement("v", "shapetype", "urn:schemas-microsoft-com:vml");
XmlAttribute id = node.OwnerDocument.CreateAttribute("id");
id.Value = "_x0000_t75";
shapetype.Attributes.Append(id);
XmlElement binData = node.OwnerDocument.CreateElement("w:binData", strURI);
XmlAttribute wName = node.OwnerDocument.CreateAttribute("w:name", strURI);
wName.Value = "wordml://03000001.png";
binData.Attributes.Append(wName);
binData.InnerText = ImageCode;
XmlElement shape = node.OwnerDocument.CreateElement("v", "shape", "urn:schemas-microsoft-com:vml");
XmlAttribute style = node.OwnerDocument.CreateAttribute("style");
style.Value = "width:" + w + "px;height:" + h + "px";
XmlElement imagedata = node.OwnerDocument.CreateElement("v", "imagedata", "urn:schemas-microsoft-com:vml");
XmlAttribute src = node.OwnerDocument.CreateAttribute("src");
src.Value = "wordml://03000001.png";
imagedata.Attributes.Append(src);
XmlAttribute shapeid = node.OwnerDocument.CreateAttribute("id");
shapeid.Value = "_x0000_t75";
shape.Attributes.Append(shapeid);
shape.Attributes.Append(style);
shape.AppendChild(imagedata);
pict.AppendChild(shapetype);
pict.AppendChild(binData);
pict.AppendChild(shape);
//加载完成后删除图片
try
{
//File.Delete(spath);
}
catch
{
}
return pict;
}
else
{
return null;
}
}
/// <summary>
/// 对图像进制编码
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
private string Module64String(string strPath)
{
FileStream fs = new FileStream(strPath, FileMode.Open);
byte[] b = new Byte[fs.Length];
fs.Position = 0;
long l = fs.Read(b, 0, (int)fs.Length);
fs.Close();
String strbaser64 = Convert.ToBase64String(b);
fs.Close();
fs.Dispose();
return strbaser64.ToString();
}
#endregion
我做成了一个dll,可以直接调用
Code
//在XML格式的Word指定书签中插入图像
string ImgPath = Application.StartupPath + "\\custom.jpg";
string OldXmlPath = Application.StartupPath + "\\template.xml";
string XmlPath = Application.StartupPath + "\\word.xml";
File.Delete(XmlPath);
File.Copy(OldXmlPath, XmlPath);
HoYaiWordXml.XmlEdit xmledit = new HoYaiWordXml.XmlEdit();
xmledit.InsertImage(ImgPath, XmlPath, "BM_IMG");
//在XML格式的Word指定书签中插入图像
string ImgPath = Application.StartupPath + "\\custom.jpg";
string OldXmlPath = Application.StartupPath + "\\template.xml";
string XmlPath = Application.StartupPath + "\\word.xml";
File.Delete(XmlPath);
File.Copy(OldXmlPath, XmlPath);
HoYaiWordXml.XmlEdit xmledit = new HoYaiWordXml.XmlEdit();
xmledit.InsertImage(ImgPath, XmlPath, "BM_IMG");
https://files.cnblogs.com/4color/HoYaiWordXml.dll.zip