在书签中插入图片 (OpenXML)
我正在疯狂地尝试寻找如何在书签中插入图片...
目前我在插入文本或表格方面没有任何问题:我找到书签并像约翰的方式一样插入到该位置: 使用 Open XML SDK 替换 Word 文件中的书签文本
现在我想发送图像到此书签。我正在阅读以下文章:
http:// /msdn.microsoft.com/en-us/library/bb497430(office.14).aspx
http://social.msdn。 microsoft.com/Forums/en-US/oxmlsdk/thread/6d9066db-a154-475d-9731-944c8ce13e67/
...但我无法使用我的模板 dotx 和书签。一些想法?
这是我用来在书签中插入段落的代码:
Run runImg = new Run();
runImg.Append(element);
Paragraph parImg = new Paragraph();
parImg.Append(runImg);
foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
{
if (bookmarkStart.Name.Value == _nomBM)
{
bookmarkStart.FirstChild.PrependChild(parImg);
}
}
谢谢!
I'm going crazy trying to find how to insert pictures in my bookmarks...
For the moment I have no problems with insert text or tables: I find bookmarks and insert in that position like John's way: Replace bookmark text in Word file using Open XML SDK
Now I want to send images to this bookmarks. I'm reading articles like:
http://msdn.microsoft.com/en-us/library/bb497430(office.14).aspx
http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/6d9066db-a154-475d-9731-944c8ce13e67/
...but I can't do it work with my template dotx and my bookmarks. Some ideas?
Here is the code I am using to insert the paragraph in my bookmark:
Run runImg = new Run();
runImg.Append(element);
Paragraph parImg = new Paragraph();
parImg.Append(runImg);
foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
{
if (bookmarkStart.Name.Value == _nomBM)
{
bookmarkStart.FirstChild.PrependChild(parImg);
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在书签中插入图片应该就像在 Word 文档本身中插入图片一样。上述任何链接都应该向您展示如何正确插入图片。关键是找到要插入的书签,并确保在
和之间插入包含图片的段落。
元素。如果这就是您正在做的事情并且仍然遇到问题,请发布您的代码,以便我们查看。编辑
查看代码后,问题是
元素是
元素的子元素。您想要找到
的父级(即
元素),然后使用以下命令插入图像段落作为下一个元素像这样的东西:Inserting a picture in a bookmark should work as if you are inserting a picture into the word document itself. Any of those above links should show you how to insert the picture correctly. The key is to find the bookmark you want to insert it in and making sure you insert the paragraph that contains the picture in between the
<w:bookmarkStart>
and<w:bookmarkEnd>
elements. If this is what you are doing and you are still having issues, post your code so we can take a look.EDIT
After seeing your code the problem is the
<w:bookmarkStart>
element is a child of the<w:p>
element. You want to find the parent of the<w:bookmarkStart>
which will be the<w:p>
element and then insert your image paragraph as the next element using something like this:我知道为时已晚,但尝试下面的操作,您可能会更接近书签位置
bookmarkStart.Parent.InsertBeforeSelf(parImg);
I know it is too late but try the below you may get bit closer to the bookmark position
bookmarkStart.Parent.InsertBeforeSelf<Paragraph>(parImg);