Created
July 7, 2020 15:56
-
-
Save aspose-com-gists/a27eeff32f91830229096fff65c479d0 to your computer and use it in GitHub Desktop.
Merge MS Word Documents in ASP.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--drag and drop file plugin--> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" /> | |
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script> | |
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/5.0.9/js/fileinput.min.js"></script> | |
<!--end of drag and drop--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public FileResult UploadFiles(List<IFormFile> files, string outputFormat) | |
{ | |
if (files.Count() <= 1) | |
{ | |
// display some message | |
return null; | |
} | |
string fileName = "merged-document.docx"; | |
string path = "wwwroot/uploads"; | |
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[i], ImportFormatMode.KeepSourceFormatting); | |
} | |
var outputStream = new MemoryStream(); | |
if (outputFormat == "DOCX") | |
{ | |
doc1.Save(outputStream, SaveFormat.Docx); | |
outputStream.Position = 0; | |
// Return generated Word file | |
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Rtf, fileName); | |
} | |
else | |
{ | |
fileName = "merged-document.pdf"; | |
doc1.Save(outputStream, SaveFormat.Pdf); | |
outputStream.Position = 0; | |
// Return generated PDF file | |
return File(outputStream, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewData["Title"] = "Merge MS Word Documents in ASP.NET"; | |
} | |
<div class="row"> | |
<div class="col-md-12" align="center"> | |
<h2 class="text-info">Merge Two or More Word DOC/DOCX Documents</h2> | |
<p class="text-info">Merge MS Word documents and get the results in DOCX or PDF format.</p> | |
</div> | |
</div> | |
<br /> | |
<form asp-controller="Home" asp-action="UploadFiles" method="post" class="form-inline dropzone" enctype="multipart/form-data"> | |
<div class="row"> | |
<div class="col-md-12" align="center"> | |
<div> | |
<input type="file" id="input-id" name="files" multiple accept=".doc, .docx" class="form-control file" data-preview-file-type="text" /> | |
</div> | |
</div> | |
</div> | |
<hr /> | |
<div class="row"> | |
<div class="col-md-12" align="center"> | |
<div class="input-group-lg"> | |
<strong>Save As</strong> | |
<select name="outputFormat" class="form-control"> | |
<option value="DOCX">DOCX</option> | |
<option value="PDF">PDF</option> | |
</select> | |
<button type="submit" class="form-control btn btn-success">Merge and Download</button> | |
</div> | |
</div> | |
</div> | |
</form> | |
<script> | |
// Drag and drop plugin options | |
$("#input-id").fileinput({ 'mainClass': "input-group-lg", 'showBrowse': true, 'showUpload': false, 'previewFileType': 'any', 'showClose': false, 'maxFileCount': 5, }); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment