Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update openTemplate to support file stream #246

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/OpenXMLSDK.Engine/Word/WordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,38 @@ public bool OpenDoc(Stream streamFile, bool isEditable)
/// <summary>
/// Ouverture d'un document depuis un template dotx
/// </summary>
/// <param name="streamTemplateFile">Chemin et nom complet du template</param>
/// <param name="newFilePath">Chemin et nom complet du fichier qui sera sauvegardé</param>
/// <param name="templateFileStream">Chemin et nom complet du template</param>
/// <returns>True si le document a bien été ouvert</returns>
public bool OpenDocFromTemplate(Stream templateFileStream)
{
if (templateFileStream is null || templateFileStream == Stream.Null)
throw new ArgumentNullException(nameof(templateFileStream), "templateFilePath must not be null");

streamFile = new MemoryStream();
try
{
// We copy the template file into the memory stream
templateFileStream.CopyTo(streamFile);

// Change the document type to Document
wdDoc = WordprocessingDocument.Open(streamFile, true);
wdDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

wdMainDocumentPart = wdDoc.MainDocumentPart;

return true;
}
catch
{
wdDoc = null;
return false;
}
}

/// <summary>
/// Ouverture d'un document depuis un template dotx
/// </summary>
/// <param name="templateFilePath">Chemin et nom complet du template</param>
/// <returns>True si le document a bien été ouvert</returns>
public bool OpenDocFromTemplate(string templateFilePath)
{
Expand Down
2 changes: 1 addition & 1 deletion src/OpenXMLSDK.UnitTest/OpenXMLSDK.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<None Update="Resources\Test.json">
<None Update="Resources\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions src/OpenXMLSDK.UnitTest/ReportEngine/ReportEngineUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System;
using System.IO;
using System.Globalization;
using OpenXMLSDK.Engine.Word.ReportEngine;
using OpenXMLSDK.Engine.Word;

namespace OpenXMLSDK.UnitTest.ReportEngine
{
Expand All @@ -10,5 +16,38 @@ public void Global_Generation()
{
ReportEngineTest.ReportEngine(string.Empty, string.Empty, false);
}

[TestMethod]
public void Global_Generation_Stream()
{
// Configuration of sample data :
var rootFolder = Path.Combine(Environment.CurrentDirectory, "Resources/MdFiles");
var templatePath = Path.Combine(Environment.CurrentDirectory, "Resources/Dotx/sample.dotx");
var outputPath = Path.Combine(Environment.CurrentDirectory, "Resources/Dotx/sample.docx");

// Generate Word document :
using var templateStream = File.OpenRead(templatePath);
var markdownContent = File.ReadAllText(Path.Combine(rootFolder, "0.md"));

// Launch transformation :
var reports = new List<Report>();

var culture = new CultureInfo("en-US");
using var templateDocument = File.OpenRead(templatePath);

var output = Stream.Null;
using (var word = new WordManager())
{
if (templateDocument != null && templateDocument != Stream.Null)
word.OpenDocFromTemplate(templateDocument);

// Append documentation :
word.AppendSubDocument(reports, true, culture);

word.SaveDoc();

output = word.GetMemoryStream();
}
}
}
}
Binary file not shown.
46 changes: 46 additions & 0 deletions src/OpenXMLSDK.UnitTest/Resources/MdFiles/0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Sample title for a random markdown file

## Overview

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nisl quis tincidunt aliquam, nunc nisl ultricies nunc, quis aliquam nis

## Function Signature

Here is a random code in python that generate a random value. It takes some parameters like a date and return a value.

```python
def random_function(date: datetime) -> float:
"""Generate a random value based on the provided date.

Args:
date (datetime): The date to be used to generate the random value.

Returns:
float: The random value generated based on the provided date.
"""
return random.random()
```

## Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| date | datetime | The date to be used to generate the random value. |

## Return Value

| Type | Description |
| ---- | ----------- |
| float | The random value generated based on the provided date. |

## Usage

```python
random_function(datetime.now())
```

## Example Output

```python
0.123456789
```
11 changes: 11 additions & 0 deletions src/OpenXMLSDK.UnitTest/Resources/MdFiles/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Another sample markdown file

## Overview

This file is here only for the purpose of testing the generator.

## Sample with mage

Here is an image generated from Dall-E 2 :

![Dall-E 2](images/sampleimagefromdalle.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.