-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathTextReplacer02.cs
More file actions
55 lines (51 loc) · 2.86 KB
/
TextReplacer02.cs
File metadata and controls
55 lines (51 loc) · 2.86 KB
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
namespace OpenXmlPowerTools
{
class Program
{
static void Main(string[] args)
{
var n = DateTime.Now;
var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));
tempDi.Create();
DirectoryInfo di2 = new DirectoryInfo("../../../");
foreach (var file in di2.GetFiles("*.docx"))
file.CopyTo(Path.Combine(tempDi.FullName, file.Name));
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test01.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", false);
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test02.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", false);
}
catch (Exception) { }
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test03.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", false);
}
catch (Exception) { }
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test04.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", true);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test05.docx"), true))
TextReplacer.SearchAndReplace(doc, "is on", "is above", true);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test06.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", false);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test07.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", true);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test08.docx"), true))
TextReplacer.SearchAndReplace(doc, "the", "this", true);
using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, "Test09.docx"), true))
TextReplacer.SearchAndReplace(doc, "===== Replace this text =====", "***zzz***", true);
}
}
}