I am trying to build a openxml document dynamically, basically I have some classes responsible for creating the sections of my document (tables,paragraph...) then I need to have another class that builds my document, in my case I called it doc, and my other classes are docTable,docRun ...
So at the moment I have this:
DocRun projectNameTitle = new DocRun();
Run projectNameTxt = disclaimerDescription.createParagraph(document.projectName, SUBTITLECOLOR, FONTSIZESUBTITLE,FONTTYPE);
DocRun dateParagraph = new DocRun();
Run dateTxt = disclaimerDescription.createParagraph(date, PARAGRAPHTITLECOLOR, DATEFONTSIZE, DEFAULTFONT);
Doc simpleDoc = new Doc();
simpleDoc.CreateDoc(dateParagraph, projectNameTitle);
the paragraph builds correctly I just need to set it to the body of the doc at the moment, there is where the doc class enters, the parameters passed should be responsible to build the document in the order they are passed.
Here is my class responsible for building the document:
using System.Web.Hosting;
namespace openXml
{
public class Doc
{
public const String DOCUMENTSLOCATION = "~/Files"; // default documents location
public void CreateDoc(params object[] document)
{
var stream = new MemoryStream();
using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
{
MainDocumentPart mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
Body body = mainPart.Document.Body;
foreach (var docSections in document)
{
body.Append(new Paragraph(new ParagraphProperties(),
new Run((Run)docSections)));
}
}
stream.Seek(0, SeekOrigin.Begin);
Directory.CreateDirectory(HostingEnvironment.MapPath(DOCUMENTSLOCATION));
System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/Files/test5.docx"), stream.ToArray());
}
}
}
I am having some troubles here, because I don't know if I am passing a run or other thing, how can I iterate over a list of items passed and append to the document in this case, I don't know what I am doing wrong here, the document doesn't get created :S