Reputation: 555
I am working with documents, trying to create a docx via a web page, basicly i say i want a table, a paragraph and it should append to my doc, at the backgroud i have a function that deals witht that stuff this way:
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());
}
watch out the parameters received in the CreateDoc, it can receive a unlimited number of paramters inside the object, that is what i want since i don'tn know how much sections my doc will have (tables, paragraphs, images etc..)-
My problem is that i need t pass more parameters that are not directly related to the construction of the file, like for example: name of the file. My idea was to pass it as the second parameter in the CreateDoc function, but how can i pass it from the other side?
Any help?
Upvotes: 0
Views: 44
Reputation: 983
You Can pass Data with your own Structure
instead Object
.
Like Data Type
and Data Content
Check DataType
of Parameter and then Manage it
Upvotes: 0
Reputation: 14541
You can always pass specific parameters to function if you have defined them as such. For instance, if you are certain that you'll always need filename
as a parameter while calling this function, you can modify the function parameter as such:
public void CreateDoc(string filename, params object[] document)
{
...
}
Then you could call it like this:
CreateDoc("filename.docx", <param1>, <param2>);
Upvotes: 1