1
-
2
- using Serilog ;
1
+
2
+ using Serilog ;
3
3
using Syncfusion . DocIO . DLS ;
4
- using Syncfusion . DocIO ;
5
- using Syncfusion . DocIORenderer ;
4
+ using Syncfusion . DocIO ;
5
+ using Syncfusion . DocIORenderer ;
6
6
using Syncfusion . Pdf ;
7
7
using DocumentFormat . OpenXml . Packaging ;
8
- using DocumentFormat . OpenXml . Wordprocessing ;
9
-
10
-
11
-
12
- namespace MCS . FOI . DocToPDF
13
- {
14
- public class DocFileProcessor : IDocFileProcessor , IDisposable
15
- {
16
-
17
-
18
- public DocFileProcessor ( ) { }
19
-
20
- public DocFileProcessor ( Stream SourceStream )
21
- {
22
- this . SourceStream = SourceStream ;
23
- }
24
-
25
- public Stream SourceStream { get ; set ; }
26
-
27
- public int FailureAttemptCount { get ; set ; }
28
-
29
- public int WaitTimeinMilliSeconds { get ; set ; }
30
-
31
- public bool IsSinglePDFOutput { get ; set ; }
32
-
33
-
34
- private MemoryStream ? output = null ;
35
- public ( bool , Stream ) ConvertToPDF ( )
36
- {
37
- bool converted = false ;
38
- string message = string . Empty ;
39
- bool _isSinglePDFOutput = IsSinglePDFOutput ;
40
- output = new MemoryStream ( ) ;
41
- try
42
- {
43
- for ( int attempt = 1 ; attempt <= FailureAttemptCount && ! converted ; attempt ++ )
44
- {
45
- try
46
- {
47
- using ( WordDocument wordDocument = new WordDocument ( SourceStream , Syncfusion . DocIO . FormatType . Automatic ) )
48
- {
49
- SourceStream . Position = 0 ;
50
-
51
- using ( var docXML = WordprocessingDocument . Open ( SourceStream , false ) )
52
- {
8
+ using DocumentFormat . OpenXml . Wordprocessing ;
53
9
54
- DocumentFormat . OpenXml . Wordprocessing . Body body = docXML . MainDocumentPart . Document . Body ;
55
- List < String > originalDates = new List < String > ( ) ;
56
- foreach ( var textItem in body . Descendants < FieldCode > ( ) . Where ( textItem => textItem . Text . Contains ( "DATE" ) ) )
57
- {
58
- var datetext = textItem . Parent . NextSibling ( ) . NextSibling ( ) ;
59
- originalDates . Add ( datetext . InnerText ) ;
60
- }
61
10
62
- List < Entity > datefields = wordDocument . FindAllItemsByProperty ( EntityType . Field , "FieldType" , FieldType . FieldDate . ToString ( ) ) ;
63
- foreach ( var ( datefield , i ) in datefields . Select ( ( datefield , i ) => ( datefield , i ) ) )
11
+
12
+ namespace MCS . FOI . DocToPDF
13
+ {
14
+ public class DocFileProcessor : IDocFileProcessor , IDisposable
15
+ {
16
+
17
+
18
+ public DocFileProcessor ( ) { }
19
+
20
+ public DocFileProcessor ( Stream SourceStream )
21
+ {
22
+ this . SourceStream = SourceStream ;
23
+ }
24
+
25
+ public Stream SourceStream { get ; set ; }
26
+
27
+ public int FailureAttemptCount { get ; set ; }
28
+
29
+ public int WaitTimeinMilliSeconds { get ; set ; }
30
+
31
+ public bool IsSinglePDFOutput { get ; set ; }
32
+
33
+
34
+ private MemoryStream ? output = null ;
35
+ public ( bool , Stream ) ConvertToPDF ( )
36
+ {
37
+ bool converted = false ;
38
+ string message = string . Empty ;
39
+ bool _isSinglePDFOutput = IsSinglePDFOutput ;
40
+ output = new MemoryStream ( ) ;
41
+ try
42
+ {
43
+ for ( int attempt = 1 ; attempt <= FailureAttemptCount && ! converted ; attempt ++ )
44
+ {
45
+ try
46
+ {
47
+ using ( WordDocument wordDocument = new WordDocument ( SourceStream , Syncfusion . DocIO . FormatType . Automatic ) )
48
+ {
49
+ SourceStream . Position = 0 ;
50
+
51
+ if ( wordDocument . ActualFormatType == FormatType . Docx )
52
+ {
53
+ using ( var docXML = WordprocessingDocument . Open ( SourceStream , false ) )
64
54
{
65
- var dateField = datefield as WField ;
66
- //Takes the owner paragraph.
67
- WParagraph ownerPara = dateField . OwnerParagraph ;
68
- int dateFieldIndex = ownerPara . ChildEntities . IndexOf ( dateField ) ;
69
- //Removes the date field.
70
- ownerPara . ChildEntities . Remove ( dateField ) ;
71
- //Creating a new text range with required date.
72
- WTextRange textRange = new WTextRange ( ownerPara . Document ) ;
73
- textRange . Text = originalDates [ i ] ; //"February 12, 2023";
74
- //Inserting the date field with the created text range.
75
- ownerPara . ChildEntities . Insert ( dateFieldIndex , textRange ) ;
55
+
56
+ DocumentFormat . OpenXml . Wordprocessing . Body body = docXML . MainDocumentPart . Document . Body ;
57
+ List < String > originalDates = new List < String > ( ) ;
58
+ foreach ( var textItem in body . Descendants < FieldCode > ( ) . Where ( textItem => textItem . Text . Contains ( "DATE" ) ) )
59
+ {
60
+ var datetext = textItem . Parent . NextSibling ( ) . NextSibling ( ) ;
61
+ originalDates . Add ( datetext . InnerText ) ;
62
+ }
63
+
64
+ List < Entity > datefields = wordDocument . FindAllItemsByProperty ( EntityType . Field , "FieldType" , FieldType . FieldDate . ToString ( ) ) ;
65
+ if ( datefields != null )
66
+ {
67
+ foreach ( var ( datefield , i ) in datefields . Select ( ( datefield , i ) => ( datefield , i ) ) )
68
+ {
69
+ var dateField = datefield as WField ;
70
+ //Takes the owner paragraph.
71
+ WParagraph ownerPara = dateField . OwnerParagraph ;
72
+ int dateFieldIndex = ownerPara . ChildEntities . IndexOf ( dateField ) ;
73
+ //Removes the date field.
74
+ ownerPara . ChildEntities . Remove ( dateField ) ;
75
+ //Creating a new text range with required date.
76
+ WTextRange textRange = new WTextRange ( ownerPara . Document ) ;
77
+ textRange . Text = originalDates [ i ] ; //"February 12, 2023";
78
+ //Inserting the date field with the created text range.
79
+ ownerPara . ChildEntities . Insert ( dateFieldIndex , textRange ) ;
80
+ }
81
+ }
76
82
}
77
83
}
78
84
79
- wordDocument . RevisionOptions . CommentDisplayMode = CommentDisplayMode . ShowInBalloons ;
80
- wordDocument . RevisionOptions . CommentColor = RevisionColor . Blue ;
81
- wordDocument . RevisionOptions . ShowMarkup = RevisionType . Deletions | RevisionType . Insertions ;
82
-
83
- using ( DocIORenderer renderer = new DocIORenderer ( ) )
84
- {
85
- using PdfDocument pdfDocument = renderer . ConvertToPDF ( wordDocument ) ;
86
- //Save the PDF file
87
- //Close the instance of document objects
88
- pdfDocument . Save ( output ) ;
89
- pdfDocument . Close ( true ) ;
90
- converted = true ;
91
-
92
- }
93
-
94
- }
95
- }
96
- catch ( Exception e )
97
- {
98
- string errorMessage = $ "Exception occured while coverting a document file, exception : { e . Message } ";
99
- message = $ "Exception happened while accessing File, re-attempting count : { attempt } , Error Message : { e . Message } , Stack trace : { e . StackTrace } ";
100
- Log . Error ( message ) ;
101
- Console . WriteLine ( message ) ;
102
- if ( attempt == FailureAttemptCount )
103
- {
104
- throw new Exception ( errorMessage ) ;
105
- }
106
- Thread . Sleep ( WaitTimeinMilliSeconds ) ;
107
- }
108
- }
109
- }
110
- catch ( Exception ex )
111
- {
112
- converted = false ;
113
- string error = $ "Exception occured while coverting Doc file, exception : { ex . Message } , stacktrace : { ex . StackTrace } ";
114
- Log . Error ( error ) ;
115
- Console . WriteLine ( error ) ;
116
- throw ;
117
- }
118
- return ( converted , output ) ;
119
- }
120
-
121
- public void Dispose ( )
122
- {
123
- Dispose ( true ) ;
124
- GC . SuppressFinalize ( this ) ;
125
- }
126
-
127
- protected virtual void Dispose ( bool disposing )
128
- {
129
- if ( disposing )
130
- {
131
- if ( this . SourceStream != null )
132
- {
133
- this . SourceStream . Close ( ) ;
134
- this . SourceStream . Dispose ( ) ;
135
- }
136
-
137
- if ( output != null ) output . Dispose ( ) ;
138
- // free managed resources
139
- }
140
-
141
- }
142
- }
143
- }
85
+ wordDocument . RevisionOptions . CommentDisplayMode = CommentDisplayMode . ShowInBalloons ;
86
+ wordDocument . RevisionOptions . CommentColor = RevisionColor . Blue ;
87
+ wordDocument . RevisionOptions . ShowMarkup = RevisionType . Deletions | RevisionType . Insertions ;
88
+
89
+ using ( DocIORenderer renderer = new DocIORenderer ( ) )
90
+ {
91
+ using PdfDocument pdfDocument = renderer . ConvertToPDF ( wordDocument ) ;
92
+ //Save the PDF file
93
+ //Close the instance of document objects
94
+ pdfDocument . Save ( output ) ;
95
+ pdfDocument . Close ( true ) ;
96
+ converted = true ;
97
+
98
+ }
99
+
100
+ }
101
+ }
102
+ catch ( Exception e )
103
+ {
104
+ string errorMessage = $ "Exception occured while coverting a document file, exception : { e . Message } ";
105
+ message = $ "Exception happened while accessing File, re-attempting count : { attempt } , Error Message : { e . Message } , Stack trace : { e . StackTrace } ";
106
+ Log . Error ( message ) ;
107
+ Console . WriteLine ( message ) ;
108
+ if ( attempt == FailureAttemptCount )
109
+ {
110
+ throw new Exception ( errorMessage ) ;
111
+ }
112
+ Thread . Sleep ( WaitTimeinMilliSeconds ) ;
113
+ }
114
+ }
115
+ }
116
+ catch ( Exception ex )
117
+ {
118
+ converted = false ;
119
+ string error = $ "Exception occured while coverting Doc file, exception : { ex . Message } , stacktrace : { ex . StackTrace } ";
120
+ Log . Error ( error ) ;
121
+ Console . WriteLine ( error ) ;
122
+ throw ;
123
+ }
124
+ return ( converted , output ) ;
125
+ }
126
+
127
+ public void Dispose ( )
128
+ {
129
+ Dispose ( true ) ;
130
+ GC . SuppressFinalize ( this ) ;
131
+ }
132
+
133
+ protected virtual void Dispose ( bool disposing )
134
+ {
135
+ if ( disposing )
136
+ {
137
+ if ( this . SourceStream != null )
138
+ {
139
+ this . SourceStream . Close ( ) ;
140
+ this . SourceStream . Dispose ( ) ;
141
+ }
142
+
143
+ if ( output != null ) output . Dispose ( ) ;
144
+ // free managed resources
145
+ }
146
+
147
+ }
148
+ }
149
+ }
0 commit comments