-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordService.cs
82 lines (70 loc) · 3.34 KB
/
WordService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using DocumentFormat.OpenXml.Packaging;
using JsonToWord.Models;
using JsonToWord.Services;
using System.IO;
using JsonToWord.Services.Interfaces;
namespace JsonToWord
{
public class WordService : IWordService
{
private readonly ContentControlService _contentControlService;
private readonly FileService _fileService;
private readonly HtmlService _htmlService;
private readonly PictureService _pictureService;
private readonly TableService _tableService;
private readonly TextService _textService;
private readonly DocumentService _documentService;
public WordService()
{
_contentControlService = new ContentControlService();
_fileService = new FileService();
_htmlService = new HtmlService();
_pictureService = new PictureService();
_tableService = new TableService();
_textService = new TextService();
_documentService = new DocumentService();
}
public string Create(WordModel _wordModel)
{
log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
var documentPath = _documentService.CreateDocument(_wordModel.LocalPath);
using (var document = WordprocessingDocument.Open(documentPath, true))
{
log.Info("Starting on doc path: " + documentPath);
foreach (var contentControl in _wordModel.ContentControls)
{
_contentControlService.ClearContentControl(document, contentControl.Title, contentControl.ForceClean);
foreach (var wordObject in contentControl.WordObjects)
{
switch (wordObject.Type)
{
case WordObjectType.File:
_fileService.Insert(document, contentControl.Title, (WordAttachment)wordObject);
break;
case WordObjectType.Html:
_htmlService.Insert(document, contentControl.Title, (WordHtml)wordObject);
break;
case WordObjectType.Picture:
_pictureService.Insert(document, contentControl.Title, (WordAttachment)wordObject);
break;
case WordObjectType.Paragraph:
_textService.Write(document, contentControl.Title, (WordParagraph)wordObject);
break;
case WordObjectType.Table:
_tableService.Insert(document, contentControl.Title, (WordTable)wordObject);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
_contentControlService.RemoveContentControl(document, contentControl.Title);
}
log.Info("Finished on doc path: " + documentPath);
}
//documentService.RunMacro(documentPath, "updateTableOfContent",sw);
//log.Info("Ran Macro");
return documentPath;
}
}
}