1- using DocumentFormat . OpenXml . Packaging ;
2- using DocumentFormat . OpenXml . Wordprocessing ;
3- using DocumentFormat . OpenXml ;
41using System ;
52using System . IO ;
63using System . Text ;
74using System . Threading ;
85using System . Xml ;
6+ using DocumentFormat . OpenXml ;
7+ using DocumentFormat . OpenXml . Packaging ;
8+ using DocumentFormat . OpenXml . Wordprocessing ;
99
1010namespace LiveWordXml . Wpf . Services
1111{
@@ -36,7 +36,9 @@ public void RefreshDocument()
3636 {
3737 if ( string . IsNullOrEmpty ( _filePath ) )
3838 {
39- throw new InvalidOperationException ( "No document path is set. Please load a document first." ) ;
39+ throw new InvalidOperationException (
40+ "No document path is set. Please load a document first."
41+ ) ;
4042 }
4143
4244 if ( ! File . Exists ( _filePath ) )
@@ -56,7 +58,9 @@ private WordprocessingDocument GetReadOnlyDocument()
5658 {
5759 if ( string . IsNullOrEmpty ( _filePath ) )
5860 {
59- throw new InvalidOperationException ( "No document is loaded. Please load a document first." ) ;
61+ throw new InvalidOperationException (
62+ "No document is loaded. Please load a document first."
63+ ) ;
6064 }
6165
6266 if ( ! File . Exists ( _filePath ) )
@@ -68,15 +72,7 @@ private WordprocessingDocument GetReadOnlyDocument()
6872
6973 try
7074 {
71- // First try to open the original file directly
72- var openSettings = new OpenSettings
73- {
74- MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings (
75- MarkupCompatibilityProcessMode . ProcessAllParts ,
76- FileFormatVersions . Office2019 )
77- } ;
78-
79- var document = WordprocessingDocument . Open ( fileToOpen , false , openSettings ) ;
75+ var document = WordprocessingDocument . Open ( fileToOpen , false ) ;
8076
8177 // Validate that we can actually read the document content
8278 if ( document ? . MainDocumentPart ? . Document != null )
@@ -93,27 +89,24 @@ private WordprocessingDocument GetReadOnlyDocument()
9389 try
9490 {
9591 fileToOpen = CreateTemporaryCopy ( _filePath ) ;
96-
97- var openSettings = new OpenSettings
98- {
99- MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings (
100- MarkupCompatibilityProcessMode . ProcessAllParts ,
101- FileFormatVersions . Office2019 )
102- } ;
103-
104- var document = WordprocessingDocument . Open ( fileToOpen , false , openSettings ) ;
92+ var document = WordprocessingDocument . Open ( fileToOpen , false ) ;
10593
10694 if ( document ? . MainDocumentPart ? . Document == null )
10795 {
10896 document ? . Dispose ( ) ;
109- throw new InvalidOperationException ( "Unable to access document content from temporary copy." ) ;
97+ throw new InvalidOperationException (
98+ "Unable to access document content from temporary copy."
99+ ) ;
110100 }
111101
112102 return document ;
113103 }
114104 catch ( Exception ex )
115105 {
116- throw new InvalidOperationException ( $ "Cannot open document. File may be locked and unable to create temporary copy: { ex . Message } ", ex ) ;
106+ throw new InvalidOperationException (
107+ $ "Cannot open document. File may be locked and unable to create temporary copy: { ex . Message } ",
108+ ex
109+ ) ;
117110 }
118111 }
119112 catch ( Exception ex )
@@ -136,7 +129,10 @@ private string CreateTemporaryCopy(string originalFilePath)
136129 string tempDirectory = Path . GetTempPath ( ) ;
137130 string fileName = Path . GetFileNameWithoutExtension ( originalFilePath ) ;
138131 string extension = Path . GetExtension ( originalFilePath ) ;
139- _tempFilePath = Path . Combine ( tempDirectory , $ "{ fileName } _temp_{ DateTime . Now : yyyyMMdd_HHmmss} { extension } ") ;
132+ _tempFilePath = Path . Combine (
133+ tempDirectory ,
134+ $ "{ fileName } _temp_{ DateTime . Now : yyyyMMdd_HHmmss} { extension } "
135+ ) ;
140136
141137 // 尝试多种方式复制文件
142138 if ( TryCopyWithFileStream ( originalFilePath , _tempFilePath ) )
@@ -154,7 +150,10 @@ private string CreateTemporaryCopy(string originalFilePath)
154150 }
155151 catch ( Exception ex )
156152 {
157- throw new InvalidOperationException ( $ "Failed to create temporary copy: { ex . Message } ", ex ) ;
153+ throw new InvalidOperationException (
154+ $ "Failed to create temporary copy: { ex . Message } ",
155+ ex
156+ ) ;
158157 }
159158 }
160159
@@ -165,8 +164,18 @@ private bool TryCopyWithFileStream(string source, string destination)
165164 {
166165 try
167166 {
168- using var sourceStream = new FileStream ( source , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ;
169- using var destStream = new FileStream ( destination , FileMode . Create , FileAccess . Write , FileShare . None ) ;
167+ using var sourceStream = new FileStream (
168+ source ,
169+ FileMode . Open ,
170+ FileAccess . Read ,
171+ FileShare . ReadWrite
172+ ) ;
173+ using var destStream = new FileStream (
174+ destination ,
175+ FileMode . Create ,
176+ FileAccess . Write ,
177+ FileShare . None
178+ ) ;
170179 sourceStream . CopyTo ( destStream ) ;
171180 return true ;
172181 }
@@ -277,7 +286,7 @@ public string FormatXml(string xml)
277286 IndentChars = " " ,
278287 NewLineChars = "\r \n " ,
279288 NewLineHandling = NewLineHandling . Replace ,
280- OmitXmlDeclaration = true
289+ OmitXmlDeclaration = true ,
281290 } ;
282291
283292 using var xmlWriter = XmlWriter . Create ( stringBuilder , xmlWriterSettings ) ;
0 commit comments