Skip to content

Commit

Permalink
Update openTemplate to support file stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieumack committed Jul 2, 2023
1 parent d2a24ca commit 3656a48
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/OpenXMLSDK.Engine/Word/WordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class WordManager : IDisposable
{
#region Fields

private MemoryStream streamFile;

private string filePath;

/// <summary>
Expand Down Expand Up @@ -123,19 +121,6 @@ wdDoc.MainDocumentPart.Document.Body.LastChild.FirstChild is ParagraphProperties
}
}

/// <summary>
/// Permet de renvoyer le MemoryStream associé au document en cours
/// </summary>
/// <returns>MemoryStream en cours, null sinon</returns>
public MemoryStream GetMemoryStream()
{
var memoryStream = new MemoryStream();
streamFile.Position = 0;
streamFile.CopyTo(memoryStream);
memoryStream.Position = 0;
return memoryStream;
}

#endregion

#region Settings
Expand Down Expand Up @@ -229,8 +214,35 @@ 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");

try
{
wdDoc = WordprocessingDocument.Open(templateFileStream, true);

// Change the document type to Document
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 All @@ -239,7 +251,7 @@ public bool OpenDocFromTemplate(string templateFilePath)
if (!File.Exists(templateFilePath))
throw new FileNotFoundException("file not found");

streamFile = new MemoryStream();
using var streamFile = new MemoryStream();
try
{
byte[] byteArray = File.ReadAllBytes(templateFilePath);
Expand Down

0 comments on commit 3656a48

Please sign in to comment.