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());