@@ -48,33 +48,36 @@ public DocFileProcessor(Stream SourceStream)
48
48
{
49
49
SourceStream . Position = 0 ;
50
50
51
- using ( var docXML = WordprocessingDocument . Open ( SourceStream , false ) )
51
+ if ( wordDocument . ActualFormatType == FormatType . Docx )
52
52
{
53
-
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
-
62
- List < Entity > datefields = wordDocument . FindAllItemsByProperty ( EntityType . Field , "FieldType" , FieldType . FieldDate . ToString ( ) ) ;
63
- if ( datefields != null )
53
+ using ( var docXML = WordprocessingDocument . Open ( SourceStream , false ) )
64
54
{
65
- foreach ( var ( datefield , i ) in datefields . Select ( ( datefield , i ) => ( datefield , i ) ) )
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
66
{
67
- var dateField = datefield as WField ;
68
- //Takes the owner paragraph.
69
- WParagraph ownerPara = dateField . OwnerParagraph ;
70
- int dateFieldIndex = ownerPara . ChildEntities . IndexOf ( dateField ) ;
71
- //Removes the date field.
72
- ownerPara . ChildEntities . Remove ( dateField ) ;
73
- //Creating a new text range with required date.
74
- WTextRange textRange = new WTextRange ( ownerPara . Document ) ;
75
- textRange . Text = originalDates [ i ] ; //"February 12, 2023";
76
- //Inserting the date field with the created text range.
77
- ownerPara . ChildEntities . Insert ( dateFieldIndex , textRange ) ;
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
+ }
78
81
}
79
82
}
80
83
}
0 commit comments