-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordApplication.cs
More file actions
202 lines (181 loc) · 6.04 KB
/
WordApplication.cs
File metadata and controls
202 lines (181 loc) · 6.04 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.IO;
using System.Runtime.InteropServices;
public enum OfficeAppType
{
Word,
Excel,
PPT
}
public interface IOfficeApplication : IDisposable
{
void OpenDocument(string filePath);
void SaveAsPDF(string toFilePath);
void CloseDocument();
}
public class WordApplication : IOfficeApplication
{
private Microsoft.Office.Interop.Word.Application _application;
private Document? _document;
public bool IsPrintRevisions { get; set; } = true;
public WordApplication()
{
_application = new Microsoft.Office.Interop.Word.Application() { Visible = false, DisplayAlerts = WdAlertLevel.wdAlertsNone };
}
public void OpenDocument(string filePath)
{
_document = _application.Documents.Open(filePath, ReadOnly: true);
}
public void SaveAsPDF(string toFilePath)
{
if (_document != null)
{
var originalShowRevisions = _document.ShowRevisions;
_document.ShowRevisions = IsPrintRevisions;
_document.SaveAs2(toFilePath, WdSaveFormat.wdFormatPDF);
_document.ShowRevisions = originalShowRevisions;
}
}
public void CloseDocument()
{
if (_document != null)
{
_document.Close(WdSaveOptions.wdDoNotSaveChanges);
Marshal.ReleaseComObject(_document);
_document = null;
}
}
public void Dispose()
{
_application.Quit();
Marshal.ReleaseComObject(_application);
}
}
public class ExcelApplication : IOfficeApplication
{
private Microsoft.Office.Interop.Excel.Application _application;
private Workbook? _workbook;
public bool IsConvertOneSheetOnePDF { get; set; } = true;
public bool IsPrintToOneColumn { get; set; } = false;
public ExcelApplication()
{
_application = new Microsoft.Office.Interop.Excel.Application() { Visible = false, DisplayAlerts = false };
}
public void OpenDocument(string filePath)
{
_workbook = _application.Workbooks.Open(filePath, ReadOnly: true);
}
public void SaveAsPDF(string toFilePath)
{
if (_workbook != null)
{
if (IsConvertOneSheetOnePDF)
{
// 每个sheet保存为单独的PDF
if (_workbook.Sheets.Count == 1)
{
var worksheet = _workbook.Sheets[1];
if (IsPrintToOneColumn)
{
SetPrintToOneColumn(worksheet);
}
worksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, toFilePath);
Marshal.ReleaseComObject(worksheet);
}
else
{
for (int i = 1; i <= _workbook.Sheets.Count; i++)
{
var worksheet = _workbook.Sheets[i];
if (IsPrintToOneColumn)
{
SetPrintToOneColumn(worksheet);
}
var directory = Path.GetDirectoryName(toFilePath);
if (directory == null) throw new ArgumentException("文件没有目录", nameof(toFilePath));
worksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(toFilePath)}_sheet{i}.pdf"));
Marshal.ReleaseComObject(worksheet);
}
}
}
else
{
// 所有sheet保存为一个PDF
if (IsPrintToOneColumn)
{
// 为所有工作表设置一列宽打印
for (int i = 1; i <= _workbook.Sheets.Count; i++)
{
var worksheet = _workbook.Sheets[i];
SetPrintToOneColumn(worksheet);
Marshal.ReleaseComObject(worksheet);
}
}
_workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, toFilePath);
}
}
}
private void SetPrintToOneColumn(Worksheet worksheet)
{
try
{
// 设置页面设置为适合一页宽
worksheet.PageSetup.Zoom = false;
worksheet.PageSetup.FitToPagesWide = 1;
worksheet.PageSetup.FitToPagesTall = 1; // 0表示自动调整高度
}
catch (Exception ex)
{
// 如果设置失败,记录错误但不中断转换
System.Diagnostics.Debug.WriteLine($"设置一列宽打印失败: {ex.Message}");
}
}
public void CloseDocument()
{
if (_workbook != null)
{
_workbook.Close(false);
Marshal.ReleaseComObject(_workbook);
_workbook = null;
}
}
public void Dispose()
{
_application.Quit();
Marshal.ReleaseComObject(_application);
}
}
public class PowerPointApplication : IOfficeApplication
{
private Microsoft.Office.Interop.PowerPoint.Application _application;
private Presentation? _presentation;
public PowerPointApplication()
{
_application = new Microsoft.Office.Interop.PowerPoint.Application();
}
public void OpenDocument(string filePath)
{
_presentation = _application.Presentations.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
}
public void SaveAsPDF(string toFilePath)
{
_presentation?.ExportAsFixedFormat(toFilePath, PpFixedFormatType.ppFixedFormatTypePDF);
}
public void CloseDocument()
{
if (_presentation != null)
{
_presentation.Close();
Marshal.ReleaseComObject(_presentation);
_presentation = null;
}
}
public void Dispose()
{
_application.Quit();
Marshal.ReleaseComObject(_application);
}
}