I'm using this method for generating docx
file:
public static void CreateDocument(string documentFileName, string text)
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Create(documentFileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
string docXml =
@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body><w:p><w:r><w:t>#REPLACE#</w:t></w:r></w:p></w:body>
</w:document>";
docXml = docXml.Replace("#REPLACE#", text);
using (Stream stream = mainPart.GetStream())
{
byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
stream.Write(buf, 0, buf.Length);
}
}
}
It works like a charm:
CreateDocument("test.docx", "Hello");
But what if I want to put HTML content instead of Hello
? for example:
CreateDocument("test.docx", @"<html><head></head>
<body>
<h1>Hello</h1>
</body>
</html>");
Or something like this:
CreateDocument("test.docx", @"Hello<BR>
This is a simple text<BR>
Third paragraph<BR>
Sign
");
both cases creates an invalid structure for document.xml
.
Any idea? How can I generate a docx file from a HTML content?