Skip to content

Commit

Permalink
revert code from mini-software#93 MR which was removed by someone.
Browse files Browse the repository at this point in the history
  • Loading branch information
isdaniel committed Dec 14, 2024
1 parent 55ccf51 commit f0f9d31
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 46 deletions.
42 changes: 38 additions & 4 deletions src/MiniWord/MiniWord.Implment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MiniSoftware
namespace MiniSoftware
{
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
Expand All @@ -18,6 +18,8 @@
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Drawing.Charts;
using System.Threading.Tasks;
using System.Threading;

public static partial class MiniWord
{
Expand Down Expand Up @@ -47,6 +49,33 @@ private static void SaveAsByTemplateImpl(Stream stream, byte[] template, Diction
stream.Write(bytes, 0, bytes.Length);
}

private static async Task SaveAsByTemplateImplAsync(Stream stream, byte[] template, Dictionary<string, object> data, CancellationToken token)
{
var value = data; //TODO: support dynamic and poco value
byte[] bytes = null;
using (var ms = new MemoryStream())
{
await ms.WriteAsync(template, 0, template.Length).ConfigureAwait(false);
ms.Position = 0;
using (var docx = WordprocessingDocument.Open(ms, true))
{
var hc = docx.MainDocumentPart.HeaderParts.Count();
var fc = docx.MainDocumentPart.FooterParts.Count();
for (int i = 0; i < hc; i++)
docx.MainDocumentPart.HeaderParts.ElementAt(i).Header.Generate(docx, value);
for (int i = 0; i < fc; i++)
docx.MainDocumentPart.FooterParts.ElementAt(i).Footer.Generate(docx, value);
docx.MainDocumentPart.Document.Body.Generate(docx, value);
docx.Save();
}

bytes = ms.ToArray();
}

await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
}


private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocument docx,
Dictionary<string, object> tags)
{
Expand Down Expand Up @@ -1125,12 +1154,17 @@ private static void AddPictureAnchor(OpenXmlElement appendElement, string relati

private static byte[] GetBytes(string path)
{
using (var st = Helpers.OpenSharedRead(path))
return GetByteAsync(path).GetAwaiter().GetResult();
}

private static async Task<byte[]> GetByteAsync(string path)
{
using (var st = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true))
using (var ms = new MemoryStream())
{
st.CopyTo(ms);
await st.CopyToAsync(ms).ConfigureAwait(false);
return ms.ToArray();
}
}
}
}
}
31 changes: 31 additions & 0 deletions src/MiniWord/MiniWord.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace MiniSoftware
{
using DocumentFormat.OpenXml.Office2013.Excel;
using MiniSoftware.Extensions;
using MiniSoftware.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.IO;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

public static partial class MiniWord
{
Expand All @@ -26,5 +35,27 @@ public static void SaveAsByTemplate(this Stream stream, byte[] templateBytes, ob
{
SaveAsByTemplateImpl(stream, templateBytes, value.ToDictionary());
}

public static async Task SaveAsByTemplateAsync(this Stream stream, byte[] templateBytes, object value,CancellationToken token = default(CancellationToken))
{
await SaveAsByTemplateImplAsync(stream, templateBytes, value.ToDictionary(),token).ConfigureAwait(false);
}

public static async Task SaveAsByTemplateAsync(this Stream stream, string templatePath, object value,CancellationToken token = default(CancellationToken))
{
await SaveAsByTemplateImplAsync(stream, await GetByteAsync(templatePath), value.ToDictionary(),token).ConfigureAwait(false);
}

public static async Task SaveAsByTemplateAsync(string path, string templatePath, object value,CancellationToken token = default(CancellationToken))
{
using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true))
await SaveAsByTemplateImplAsync(stream, await GetByteAsync(templatePath), value.ToDictionary(),token);
}

public static async Task SaveAsByTemplateAsync(string path, byte[] templateBytes, object value,CancellationToken token = default(CancellationToken))
{
using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true))
await SaveAsByTemplateImplAsync(stream, templateBytes, value.ToDictionary(),token);
}
}
}
2 changes: 1 addition & 1 deletion src/MiniWord/MiniWord.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
<None Include="icon.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="[3.1.1,4.0.0)" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
</ItemGroup>
</Project>
Loading

0 comments on commit f0f9d31

Please sign in to comment.