1
votes

I am using this library to convert an html text to word format.

Everything works perfectly.

I need to style some of the text now. what I am using right now to generate document is that I have a list of heading and sub headings and heading text, I am using for each loop to get heading and subheading and its text and output them but I want these heading and subheading to assign heading1 to category and heading2 to sub category. here is what I got so far:

Foreach loop to get catagories and sub categories with its text

foreach (var category in ct)
            {
                strDocumentText.Append(category.ParentCat.CategoryName);
                strDocumentText.Append("<br />");
                if(category.DocumentText != null)
                {
                    strDocumentText.Append(category.DocumentText);
                }

                if (category.Children != null)
                {
                    foreach (var subCategoreis in category.Children)
                    {
                        strDocumentText.Append("<p />");
                        strDocumentText.Append(subCategoreis.ParentCat.CategoryName);
                        strDocumentText.Append("<br />");
                        if (category.DocumentText != null)
                        {

                            strDocumentText.Append(subCategoreis.DocumentText);
                        }
                    }
                }

            }

Create word document :

StringBuilder strDocumentText = new StringBuilder();

string html = strDocumentText.ToString();
 using (MemoryStream generatedDocument = new MemoryStream())
                {
                    BuildDocument(generatedDocument, html);
                    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
                    {
                         MainDocumentPart mainPart = wordDoc.MainDocumentPart;
                       if (mainPart == null)
                       {
                           mainPart = wordDoc.AddMainDocumentPart();
                           new DocumentFormat.OpenXml.Wordprocessing.Document(new Body()).Save(mainPart);
                       }

                       HtmlConverter converter = new HtmlConverter(mainPart);
                       Body body = mainPart.Document.Body;

                        var paragraphs = converter.Parse(html);
                        for (int i = 0; i < paragraphs.Count; i++)
                       {
                            body.Append(paragraphs[i]);
                       }

                        mainPart.Document.Save();
                    }

                    fs.Close();
                    File.WriteAllBytes(saveFileDialog1.FileName, generatedDocument.ToArray());
1
ok let me explain this in another way.. how do i set a paragraph to default style(heading1, heading2) of word document?James

1 Answers

0
votes

First you need to add the style definitions to the document. The default styles are not included when constructing an OpenXml Document. After you define the styles, you can reference them in the paragraph properties element (serialized as "pPr") OR the run element properties. Take a look at: http://msdn.microsoft.com/en-us/library/cc850838.aspx