Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
1  
Are you looking for a docx or doc format? The OpenXML SDK and will help you inserting text into docx documents. microsoft.com/download/en/details.aspx?id=5124 and msdn.microsoft.com/en-us/library/aa982683(v=office.12).aspx might be good places to start. –  Robert Kaucher Mar 10 '12 at 14:58
    
Thanks , I am looking for a docx format.Can I use another method other than OpenXML SDK ؟ –  ar.gorgin Mar 11 '12 at 4:47

1 Answer 1

up vote 0 down vote accepted

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.