[英]Embed html within a table or at specific location in a docx document
I'm trying to add HTML content to DOCX file using OpenXML altchunk approach using C#. 我正在尝试使用C#使用OpenXML altchunk方法将HTML内容添加到DOCX文件中。 The below sample code works fine and appends the HTML content to the end of the document. 下面的示例代码可以正常工作,并将HTML内容附加到文档末尾。 My requirement is to add HTML content at a specific place in the document, like inside a table cell or inside a paragraph, or search and replace a specific string with an HTML string or placeholders marked using content controls. 我的要求是在文档的特定位置添加HTML内容,例如在表格单元格或段落内部,或者用HTML字符串或使用内容控件标记的占位符搜索和替换特定字符串。 Can you please point me to some sample example or share few suggestions. 能否请您指出一些示例或分享一些建议。 Please let me know if you need more info. 如果您需要更多信息,请告诉我。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using System.Xml;
namespace Docg2
{
class Program
{
static void Main(string[] args)
{
testaltchunk();
}
public static void testaltchunk()
{
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("../../Test3.docx", true))
{
string html =
@"<html>
<head/>
<body>
<h1>Html Heading</h1>
<p>This is an html document in a string literal.</p>
</body>
</html>";
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (StreamWriter stringStream = new StreamWriter(chunkStream))
stringStream.Write(html);
XElement altChunk = new XElement(w + "altChunk", new XAttribute(r + "id", altChunkId));
XDocument mainDocumentXDoc = GetXDocument(myDoc);
mainDocumentXDoc.Root
.Element(w + "body")
.Elements(w + "p")
.Last()
.AddAfterSelf(altChunk);
SaveXDocument(myDoc, mainDocumentXDoc);
}
}
private static void SaveXDocument(WordprocessingDocument myDoc, XDocument mainDocumentXDoc)
{
// Serialize the XDocument back into the part
using (var str = myDoc.MainDocumentPart.GetStream(FileMode.Create, FileAccess.Write))
using (var xw = XmlWriter.Create(str))
mainDocumentXDoc.Save(xw);
}
private static XDocument GetXDocument(WordprocessingDocument myDoc)
{
// Load the main document part into an XDocument
XDocument mainDocumentXDoc;
using (var str = myDoc.MainDocumentPart.GetStream())
using (var xr = XmlReader.Create(str))
mainDocumentXDoc = XDocument.Load(xr);
return mainDocumentXDoc;
}
}
}
To expand on my comment a little bit: You really shouldn't be manipulating the document XML yourself. 为了进一步说明我的观点:您实际上不应该自己操纵XML文档。 You lose all the benefits of using OpenXML in the first place. 首先,您会失去使用OpenXML的所有好处。 Thus, your code could be re-written like this: 因此,您的代码可以像这样重写:
static void Main(string[] args)
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("../../Test3.docx", true))
{
string html =
@"<html>
<head/>
<body>
<h1>Html Heading</h1>
<p>This is an html document in a string literal.</p>
</body>
</html>";
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (StreamWriter stringStream = new StreamWriter(chunkStream))
stringStream.Write(html);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
// this inserts altChunk after the last Paragraph
mainPart.Document.Body
.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
mainPart.Document.Save();
}
}
Now, it becomes clear that you can insert your AltChunk
after, or before, or inside any element in the document, as long as you can find the element. 现在,很明显,您可以在文档中的任何元素之后,之前或内部插入AltChunk
,只要可以找到该元素即可。 That part will depend on what you're searching for. 那部分将取决于您要搜索的内容。
If you're searching for a specific table, then search for a DocumentFormat.OpenXml.Wordprocessing.Table
etc. Here is one example of how to search for a specific table in a document: Find a specific Table (after a bookmark) in open xml 如果要搜索特定的表,请搜索DocumentFormat.OpenXml.Wordprocessing.Table
等。这是一个如何在文档中搜索特定表的示例: 在打开的目录中查找特定的表(在书签之后) XML
Here's an example of replacing a content control https://msdn.microsoft.com/en-us/library/cc197932(v=office.12).aspx 这是替换内容控件的示例https://msdn.microsoft.com/zh-cn/library/cc197932(v=office.12).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.