Skip to content

Commit 24c0864

Browse files
authored
Merge pull request #891 from bcgov/test-rook-NK-FOIMOD-3038
fix doc conversion after datefield update
2 parents 4bfbdd1 + 3fcdf5d commit 24c0864

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

MCS.FOI.S3FileConversion/MCS.FOI.DocToPDF/DocFileProcessor.cs

+27-24
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,36 @@ public DocFileProcessor(Stream SourceStream)
4848
{
4949
SourceStream.Position = 0;
5050

51-
using (var docXML = WordprocessingDocument.Open(SourceStream, false))
51+
if (wordDocument.ActualFormatType == FormatType.Docx)
5252
{
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))
6454
{
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)
6666
{
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+
}
7881
}
7982
}
8083
}

0 commit comments

Comments
 (0)