je le fais comme ceci:
private DocX fullReportDocument;
private DocX titleDocument;
private DocX firstDocument;
private DocX secondDocument;
public byte[] GetReport(bool WantFirstReport, bool WantSecondReport)
{
fullDocument = DocX.Create(new MemoryStream());
// Create title for the report
this.GenerateTitleDocument();
// Insert the old document into the new document.
fullDocument.InsertDocument(titleDocument);
// Save the new document.
fullDocument.Save();
if (WantFirstReport)
{
// Create expertise report
this.GenrateFirstDocument();
// Insert a page break at the beginning to separate title and expertise report properly
firstDocument.Paragraphs[0].InsertPageBreakBeforeSelf();
firstDocument.Save();
// Insert the old document into the new document.
fullDocument.InsertDocument(firstDocument);
// Save the new document.
fullDocument.Save();
}
if (WantSecondReport)
{
// Create expertise report
this.GenrateSecondDocument();
// Insert a page break at the beginning to separate title and expertise report properly
secondDocument.Paragraphs[0].InsertPageBreakBeforeSelf();
secondDocument.Save();
// Insert the old document into the new document.
fullDocument.InsertDocument(secondDocument);
// Save the new document.
fullDocument.Save();
}
return fullReportDocument.DocumentMemoryStream.ToArray();
}
Mais j'ai modifié la propriété librairie DocX ajouter DocumentMemoryStream qui expose le MemoryStream interne MemoryStream
Si vous ne voulez pas modifier la bibliothèque DocX vous pouvez également faire ceci:
fullDocument.SaveAs(GENERATED_DOCX_LOCATION);
return System.IO.File.ReadAllBytes(GENERATED_DOCX_LOCATION);
GENERATED_DOCX_LOCATION est évidemment une chaîne avec le chemin physcal du fichier que vous souhaitez enregistrer.