我正在生成 Word(.docx) 文档,并遇到了如何在已有的文档中插入图表、图像而不是自定义标签的问题。但最初我使用“Altchunk”方法从多个 .RTF 文件创建文档,将 .RTF 插入模板文档中的内容控件中。这些 .RTF 文件包含我们的自定义标签,它们看起来像“”并出现在文档中,就像下面所示的新行一样。(而且我们事先不知道文档中会有多少个标签)
.......
…………
谷歌搜索但找不到如何用图像替换标签的好方法。
因为首先我需要读取标签才能识别图像的名称,所以我知道将使用什么图像代替标签,然后从文档中删除标签(仅文本)并在其位置上放置我刚刚获取的图像,知道名称的图像。所以这很复杂,但我希望有人有好的想法可以分享。谢谢,
我想更具体地描述这个问题:
1)关于altchunk方法,我只是找到内容控件,将其放入用作模板的word文档中,并且当我有rtf字符串时,我在 CC 之后插入 altchunk。
我的方法如下所示:
private void ReplaceContentControlWithRTF(MainDocumentPart mainPart, string tagName, string rtfString)
{
SdtBlock sb = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName).Single();
string altChunkId = "altChunkId" + altChunkIdNumber++;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Rtf, altChunkId);
MemoryStream s = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString));
chunk.FeedData(s);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
DocumentFormat.OpenXml.OpenXmlElement parent = sb.Parent;
parent.InsertAfter(altChunk, sb);
sb.Remove();
mainPart.Document.Save();
}
I'm generating Word(.docx) document and experiencing an issue of how to insert diagrams, images instead of custom tags in document that are already there. But initially I create document from several .RTF files, using "Altchunk" approach, inserting .RTFs into content controls in template doc. Those .RTF files contain our custom tags, they look like "<ElementType="Diagram" Name="Somename">" and appear in document just with the new line like shown below.(And we don't know, in advance, how many those tags will be in doc)
.......
<ElementType="Diagram" Name="Some name of the diagram">
.......
Googled it but can't find good approach of how to replace tags with images.
Because first I need to read tag in order to recognize the name of image, so I know what image will go instead of tag, then remove tag(just text) from document and on its place put an image I just fetched, knowing the name of the image. So it is pretty complicated, but I hope someone have good ideas to share. Thanks
I would like to describe this issue a little bit more specific:
1) Regarding altchunk method, I just find content controls which I put in word document that I use as template, and when I have rtf string, I insert altchunk after that CC.
my method looks like the following:
private void ReplaceContentControlWithRTF(MainDocumentPart mainPart, string tagName, string rtfString)
{
SdtBlock sb = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName).Single();
string altChunkId = "altChunkId" + altChunkIdNumber++;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Rtf, altChunkId);
MemoryStream s = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString));
chunk.FeedData(s);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
DocumentFormat.OpenXml.OpenXmlElement parent = sb.Parent;
parent.InsertAfter(altChunk, sb);
sb.Remove();
mainPart.Document.Save();
}
发布评论
评论(1)
我的答案很长,所以我将用一些标题分隔内容,以便于阅读:)。除非您已经找到了更好的方法,否则您应该考虑每个方法。
连接文档
我想,当您使用altchunk方法时,您将拥有第一个文档(原始文档)的xml,并且所有附加文档都被编码。
更好的方法是连接文档的内容,因为这样您就可以轻松访问整个文档的 xml 树。如果您不在串联文档中使用图像、超链接等,那么这很容易,因为它们需要以特殊方式处理,并且会使事情变得更加复杂。
或者,您可以尝试在连接图像之前将图像插入到每个文档中。
使用内容控件作为文档中的占位符
现在,假设您已经可以访问文档,您应该将内容控件放置在 Word 文档中您希望图像显示的位置并设置标签 将它们中的每一个设置为一个值,然后您应该在代码中搜索,以便找到插入图片的正确位置。您可以在此处找到有关内容控制的一些基本信息:http:// office.microsoft.com/en-us/help/content-controls-HA010030750.aspx。
插入 XML 标签和图像字节
为了将图像插入到文档中,您不仅要在希望图像出现的位置插入一些 XML 标记,还要添加包含图像的字节:一个
ImagePart
。我在这里找到了一个解释如何执行此操作的教程(尽管我自己没有遵循):http://msdn.microsoft.com/en -us/library/ee342530%28v=office.12%29.aspx。
考虑使用 OpenXML SDK 2.0 Productivity Tool
为了使您使用 OpenXML SDK 的工作更加简单,我建议您使用 Microsoft 的 SDK Productivity Tool。它可以作为 OpenXML SDK 的一部分进行下载,它允许您打开任何 MS Office OpenXML 文档,以 XML 方式显示其内容,甚至可以显示重新创建该文档所需的 C# 代码。您可以从此处下载:http://www.microsoft.com/下载/en/details.aspx?id=5124。
如果您还有其他问题,请随时询问:)。
My answer got quite long, so I'll separate the contents with some headings in order to make it easier to read :). You should consider each of them unless you already found a better approach.
Concatenating documents
I suppose that when you use altchunk method, you have then an xml of the first document (an original one) and all appended documents are encoded.
Better approach would be to concatenate contents of documents, because you have then easy access to the xml tree of the whole document. It's quite easy if you don't use images, hyperlinks etc. in concatenated documents, because they need to be handled in a special way and can make it more complicated.
Alternatively, you can try to insert images to each of the document BEFORE concatenating them.
Use Content Controls as placeholders in the document
Now, supposing you already can access document, you should place Content Controls in Word documents, in places you want your images to appear and set Tag of each of them to a value you should then search in your code in order to find the right place to insert the picture. You can find some basic information about Content Controls here: http://office.microsoft.com/en-us/help/content-controls-HA010030750.aspx.
Insert XML tags and image's bytes
In order to insert an image to a document, you have not only to insert some XML tags in a place you want the image to appear, but also add bytes containing the image as an
ImagePart
. I've found a tutorial explaning how to do it here (although I haven't followed it myself):http://msdn.microsoft.com/en-us/library/ee342530%28v=office.12%29.aspx.
Consider using OpenXML SDK 2.0 Productivity Tool
In order to make your work with OpenXML SDK simpler, I recommend you to use SDK Productivity Tool from Microsoft. It can be donwloaded as a part of the OpenXML SDK and it allows you to open any MS Office OpenXML document, show its contents in XML manner and can even display C# code required to recreate it. You can download it from here: http://www.microsoft.com/download/en/details.aspx?id=5124.
If you have any other questions, feel free to ask :).