My approach is pretty simple. I am getting two files from internet (served as .docx files), get the byte[]
for those two file. And performing Append()
operation on the destination file, appending the cloned Body
of the source file. The below is my code
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using System.Collections.Generic;
namespace WhatApp.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DocController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> Get()
{
byte[] file1 = await GetBytes("https://dummyfileserver.io/file/1");
byte[] file2 = await GetBytes("https://dummyfileserver.io/file/2");
byte[] result = MergeFiles(file1, file2);
// To return the file
return File(result, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
private async Task<byte[]> GetBytes(string url)
{
using HttpClient httpClient = new HttpClient();
var res = await httpClient.GetAsync(url);
if (res.IsSuccessStatusCode)
{
using var filestream = await res.Content.ReadAsStreamAsync();
var filebytes = new byte[filestream.Length];
filestream.Read(filebytes, 0, filebytes.Length);
return filebytes;
}
throw new Exception();
}
private byte[] MergeFiles(byte[] dest, byte[] src)
{
using (MemoryStream destMem = new MemoryStream())
{
destMem.Write(dest, 0, (int)dest.Length);
using (WordprocessingDocument mywDoc =
WordprocessingDocument.Open(destMem, true))
{
mywDoc.MainDocumentPart.Document.Body.InsertAt(new PageBreakBefore(), 0);
mywDoc.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));
var srcElements = GetSourceDoc(src);
mywDoc.MainDocumentPart.Document.Body.Append(srcElements);
mywDoc.Close();
}
return destMem.ToArray();
}
}
private OpenXmlElement GetSourceDoc(byte[] src)
{
using (MemoryStream srcMem = new MemoryStream())
{
srcMem.Write(src, 0, (int)src.Length);
using (WordprocessingDocument srcDoc =
WordprocessingDocument.Open(srcMem, true))
{
OpenXmlElement elem = srcDoc.MainDocumentPart.Document.Body.CloneNode(true);
srcDoc.Close();
return elem;
}
}
}
}
}
The result file does not show the images properly in the region where file2
is being added (second part of the response document).
What must be the reason for this problem? How to solve it?
Another issue I noticed is the the debugging forcefully stops after I save the file to local machine. What must be the cause of that?