-
Notifications
You must be signed in to change notification settings - Fork 5
/
Exporter.cs
102 lines (82 loc) · 3.73 KB
/
Exporter.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections.Generic;
using System.Linq;
using VisioAutomation.Extensions;
namespace VisioExportPagesToDocs
{
public class Exporter
{
public ExporterSettings Settings;
public List<LogRecord> Log;
public Exporter(ExporterSettings settings)
{
Log = new List<LogRecord>();
if (settings == null)
{
throw new System.ArgumentNullException();
}
if (settings.InputDocument == null)
{
throw new System.ArgumentNullException();
}
if (settings.DestinationPath == null)
{
throw new System.ArgumentNullException();
}
this.Settings = settings;
if (!System.IO.Directory.Exists(this.Settings.DestinationPath))
{
throw new System.ArgumentException(string.Format("destination path does not exist: \"{0}\"", this.Settings.DestinationPath));
}
string input_filname = settings.InputDocument.Name;
this.Settings.BaseName = this.Settings.BaseName ?? System.IO.Path.GetFileNameWithoutExtension(input_filname);
this.Settings.InputExtension = System.IO.Path.GetExtension(input_filname);
}
public IEnumerable< LogRecord> Run()
{
var pages = this.Settings.InputDocument.Pages;
var app = this.Settings.InputDocument.Application;
var docs = app.Documents;
var _pages = pages.AsEnumerable().ToList();
int pageindex = 1;
foreach (var page in _pages)
{
string pagename = page.Name;
var newdoc = docs.Add("");
var newpage = newdoc.Pages[1];
VisioAutomation.Pages.PageHelper.DuplicateToDocument(page, newdoc, newpage, pagename, true);
// Visio allows characters in page names that are not valid for file names. Replace them.
foreach (var c in System.IO.Path.GetInvalidFileNameChars())
{
pagename = pagename.Replace(c, '_');
}
string basename = string.Format("{0}_{1}_{2}{3}", this.Settings.BaseName, pageindex.ToString(), pagename, this.Settings.InputExtension);
string destname = System.IO.Path.Combine( this.Settings.DestinationPath, basename);
var rec = new LogRecord();
rec.OutputFileAlreadyExists = System.IO.File.Exists(destname);
rec.OutputFilename = destname;
rec.Settings = this.Settings;
rec.PageIndex = pageindex;
rec.PageName = pagename;
// Perform a save if the output file doesn't exist or the settings force an overwrite
bool perform_save = this.Settings.Overwrite || !rec.OutputFileAlreadyExists;
if (perform_save)
{
if (this.Settings.Overwrite && rec.OutputFileAlreadyExists)
{
// the output file exists and we are instructed to overwrite it so try to delete it
System.IO.File.Delete(destname);
}
var activewindow = app.ActiveWindow;
activewindow.ViewFit = (int)Microsoft.Office.Interop.Visio.VisWindowFit.visFitPage;
newdoc.SaveAs(destname);
newdoc.Close(true);
rec.OutputFileWritten = true;
}
this.Log.Add(rec);
yield return rec;
pageindex++;
}
this.Settings.InputDocument.Close(true);
}
}
}