I have a webpart . I insert InputFormTextBox into webpart.
I want save InputFormTextBox.text into word file with formating.
How do insert HTML into word document?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI have a webpart . I insert InputFormTextBox into webpart.
I want save InputFormTextBox.text into word file with formating.
How do insert HTML into word document?
The altChunk markup tells the consuming application to import content into the document.
I use this code:
XNamespace w =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace r =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships";
File.Copy("E:\\Test.docx", "E:\\Test3.docx");
using (WordprocessingDocument myDoc =
WordprocessingDocument.Open("E:\\Test3.docx", true))
{
string html =
@"<html>
<head/>
<body>
<p> To<em> learn</em> more a<em>bout<strong> </strong> </em><strong>ASP.</strong>NET vi<span >sit jh kj</span> h</p> <strong>,j jlkjlj;lk;</strong>
</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 (Stream str = myDoc.MainDocumentPart.GetStream(
FileMode.Create, FileAccess.Write))
using (XmlWriter xw = XmlWriter.Create(str))
mainDocumentXDoc.Save(xw);
}
private static XDocument GetXDocument(WordprocessingDocument myDoc)
{
// Load the main document part into an XDocument
XDocument mainDocumentXDoc;
using (Stream str = myDoc.MainDocumentPart.GetStream())
using (XmlReader xr = XmlReader.Create(str))
mainDocumentXDoc = XDocument.Load(xr);
return mainDocumentXDoc;
}