|
Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
---------------------------------Aspose技术交流群(761297826)----------------------------
如果您正在处理一堆 Word 文档,您可能会遇到需要以编程方式将多个文档合并为一个文档的场景。为此,本文为您提供了有关如何使用 C# 在 http://ASP.NET 应用程序中合并 MS Word 文档的完整指南。
合并多个 MS Word 文档可能有助于将相同类型的文档保存到一个文件中,在共享之前合并多个文档,等等。为了让您实现这一点,我们将向您展示如何创建一个 http://ASP.NET 应用程序来合并 MS Word (DOC/DOCX) 文档。此 Word 文档合并应用程序将具有以下功能:
一、用于在 http://ASP.NET 中合并 MS Word 文档的 C# 库
Aspose.Words for .NET是一个功能丰富的文字处理库,可让您轻松处理 MS Word 文档。它还允许您在 http://ASP.NET 或任何 .NET/.NET Core 应用程序中将多个 Word 文档合并为一个文档。Aspose.Words for .NET 可以使用NuGet安装,也可以作为DLL文件下载。
PM> install-package Aspose.Words二、在 http://ASP.NET 中合并 MS Word 文档
以下是创建 http://ASP.NET 应用程序的步骤,该应用程序可让您在不使用 MS Office/Word 的情况下合并两个或多个 Word (DOC/DOCX) 文档。
- 在 Visual Studio 中创建http://ASP.NET Core Web 应用程序。

- 从模板列表中选择Web 应用程序(模型-视图-控制器) 。

- 从 NuGet 包管理器或包管理器控制台安装Aspose.Words for .NET 。

@{
ViewData["Title"] = "Merge MS Word Documents in ASP.NET";
}
<div class=&#34;row&#34;>
<div class=&#34;col-md-12&#34; align=&#34;center&#34;>
<h2 class=&#34;text-info&#34;>Merge Two or More Word DOC/DOCX Documents</h2>
<p class=&#34;text-info&#34;>Merge MS Word documents and get the results in DOCX or PDF format.</p>
</div>
</div>
<br />
<form asp-controller=&#34;Home&#34; asp-action=&#34;UploadFiles&#34; method=&#34;post&#34; class=&#34;form-inline dropzone&#34; enctype=&#34;multipart/form-data&#34;>
<div class=&#34;row&#34;>
<div class=&#34;col-md-12&#34; align=&#34;center&#34;>
<div>
<input type=&#34;file&#34; id=&#34;input-id&#34; name=&#34;files&#34; multiple accept=&#34;.doc, .docx&#34; class=&#34;form-control file&#34; data-preview-file-type=&#34;text&#34; />
</div>
</div>
</div>
<hr />
<div class=&#34;row&#34;>
<div class=&#34;col-md-12&#34; align=&#34;center&#34;>
<div class=&#34;input-group-lg&#34;>
<strong>Save As</strong>
<select name=&#34;outputFormat&#34; class=&#34;form-control&#34;>
<option value=&#34;DOCX&#34;>DOCX</option>
<option value=&#34;PDF&#34;>PDF</option>
</select>
<button type=&#34;submit&#34; class=&#34;form-control btn btn-success&#34;>Merge and Download</button>
</div>
</div>
</div>
</form>
<script>
// Drag and drop plugin options
$(&#34;#input-id&#34;).fileinput({ &#39;mainClass&#39;: &#34;input-group-lg&#34;, &#39;showBrowse&#39;: true, &#39;showUpload&#39;: false, &#39;previewFileType&#39;: &#39;any&#39;, &#39;showClose&#39;: false, &#39;maxFileCount&#39;: 5, });
</script
- 在HomeController.cs类中插入以下代码。
public FileResult UploadFiles(List<IFormFile> files, string outputFormat)
{
if (files.Count() <= 1)
{
// display some message
return null;
}
string fileName = &#34;merged-document.docx&#34;;
string path = &#34;wwwroot/uploads&#34;;
List<Document> documents = new List<Document>();
// upload files
foreach (IFormFile file in files)
{
string filePath = Path.Combine(path, file.FileName);
// Save files
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
}
// Add all documents to the list
documents.Add(new Document(filePath));
}
// Load first Word document
Document doc1 = documents[0];
for (int i = 1; i < documents.Count(); i++)
{
// Merge Word documents
doc1.AppendDocument(documents, ImportFormatMode.KeepSourceFormatting);
}
var outputStream = new MemoryStream();
if (outputFormat == &#34;DOCX&#34;)
{
doc1.Save(outputStream, SaveFormat.Docx);
outputStream.Position = 0;
// Return generated Word file
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Rtf, fileName);
}
else
{
fileName = &#34;merged-document.pdf&#34;;
doc1.Save(outputStream, SaveFormat.Pdf);
outputStream.Position = 0;
// Return generated PDF file
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
}
}
view rawHomeController.cs hosted with ❤ by GitHub
在_layout.cshtml文件的 head 标签中包含以下拖放插件的 JS 和 CSS 文件。
<!--drag and drop file plugin-->
<link href=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/css/fileinput.min.css&#34; media=&#34;all&#34; rel=&#34;stylesheet&#34; type=&#34;text/css&#34; />
<script src=&#34;https://code.jquery.com/jquery-3.3.1.min.js&#34; crossorigin=&#34;anonymous&#34;></script>
<script src=&#34;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js&#34; crossorigin=&#34;anonymous&#34;></script>
<script src=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/js/fileinput.min.js&#34;></script>
<!--end of drag and drop-->

以上便是使用 C# http://ASP.NET 合并 MS Word 文档 ,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。 |
|