Novacode Docx合并多个单词文档

时间:2017-09-28 14:58:01

标签: c# ms-word httpresponse novacode-docx

我正在使用Novacode Docx用c#创建word文档但是我将两个文档合并为一个有问题。根据我的要求,我需要下载两个word文档而不是一次性压缩它们,但是我没有找到为此我已经计划合并word文档并将它们下载为单个文件。可以帮助我使用Novacode Docx合并word文档。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

我这样做:

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

但我修改了DocX librairy以添加暴露memoryStream内部MemoryStream的DocumentMemoryStream属性

如果您不想修改DocX库,您也可以这样做:

fullDocument.SaveAs(GENERATED_DOCX_LOCATION);
return System.IO.File.ReadAllBytes(GENERATED_DOCX_LOCATION);

GENERATED_DOCX_LOCATION显然是一个字符串,其中包含您要保存的文件的physcal路径。