14
14
using OWD = OpenXML . Words . Data ;
15
15
using OT = OpenXML . Templates ;
16
16
using Openize . Words ;
17
+ using System . Xml . Linq ;
17
18
18
19
namespace OpenXML . Words
19
20
{
@@ -38,7 +39,7 @@ private OwDocument()
38
39
_mainPart . Document = new WP . Document ( ) ;
39
40
var tmp = new OT . DefaultTemplate ( ) ;
40
41
tmp . CreateMainDocumentPart ( _mainPart ) ;
41
- CreateProperties ( _pkgDocument ) ;
42
+ // CreateProperties(_pkgDocument);
42
43
43
44
_numberingPart = _mainPart . NumberingDefinitionsPart ;
44
45
@@ -91,6 +92,7 @@ private OwDocument(WordprocessingDocument pkg)
91
92
}
92
93
}
93
94
95
+ /**
94
96
#region Create Core Properties for OpenXML Word Document
95
97
internal void CreateProperties(WordprocessingDocument pkgDocument)
96
98
{
@@ -105,13 +107,14 @@ internal void CreateProperties(WordprocessingDocument pkgDocument)
105
107
pkgDocument.DeletePart(customPart);
106
108
}
107
109
var coreProperties = new OT.CoreProperties();
110
+
108
111
var dictCoreProp = new Dictionary<string, string>
109
112
{
110
113
["Title"] = "Newly Created OWDocument",
111
114
["Subject"] = "WordProcessing OWDocument Generation",
112
115
["Keywords"] = "DOCX",
113
116
["Description"] = "A WordProcessing OWDocument Created from Scratch.",
114
- [ "Creator" ] = "Openize.Words "
117
+ ["Creator"] = "Openize.OpenXML-SDK "
115
118
};
116
119
var currentTime = System.DateTime.UtcNow;
117
120
dictCoreProp["Created"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
@@ -121,6 +124,41 @@ internal void CreateProperties(WordprocessingDocument pkgDocument)
121
124
customProperties.CreateExtendedFilePropertiesPart(pkgDocument.AddExtendedFilePropertiesPart());
122
125
}
123
126
#endregion
127
+ **/
128
+
129
+ #region Create Core Properties for OpenXML Word Document
130
+ internal void CreateProperties ( WordprocessingDocument pkgDocument , DocumentProperties documentProperties )
131
+ {
132
+ var corePart = pkgDocument . CoreFilePropertiesPart ;
133
+
134
+ if ( corePart != null )
135
+ {
136
+ pkgDocument . DeletePart ( corePart ) ;
137
+ }
138
+ var dictCoreProp = new Dictionary < string , string >
139
+ {
140
+ [ "Title" ] = documentProperties . Title ,
141
+ [ "Subject" ] = documentProperties . Subject ,
142
+ [ "Creator" ] = documentProperties . Creator ,
143
+ [ "Keywords" ] = documentProperties . Keywords ,
144
+ [ "Description" ] = documentProperties . Description ,
145
+ [ "LastModifiedBy" ] = documentProperties . LastModifiedBy ,
146
+ [ "Revision" ] = documentProperties . Revision ,
147
+ [ "Created" ] = documentProperties . Created ,
148
+ [ "Modified" ] = documentProperties . Modified
149
+ } ;
150
+ var coreProperties = new OT . CoreProperties ( ) ;
151
+ coreProperties . CreateCoreFilePropertiesPart ( pkgDocument . AddCoreFilePropertiesPart ( ) , dictCoreProp ) ;
152
+
153
+ var customPart = pkgDocument . CustomFilePropertiesPart ;
154
+ if ( customPart != null )
155
+ {
156
+ pkgDocument . DeletePart ( customPart ) ;
157
+ }
158
+ var customProperties = new OT . CustomProperties ( ) ;
159
+ customProperties . CreateExtendedFilePropertiesPart ( pkgDocument . AddExtendedFilePropertiesPart ( ) ) ;
160
+ }
161
+ #endregion
124
162
125
163
public static OwDocument CreateInstance ( )
126
164
{
@@ -135,7 +173,7 @@ public static OwDocument CreateInstance(WordprocessingDocument pkg)
135
173
#region Create OpenXML Word Document Contents Based on Openize.Words.IElements
136
174
137
175
#region Main Method
138
- internal void CreateDocument ( List < FF . IElement > lst )
176
+ internal void CreateDocument ( List < FF . IElement > lst , DocumentProperties documentProperties )
139
177
{
140
178
try
141
179
{
@@ -185,6 +223,7 @@ internal void CreateDocument(List<FF.IElement> lst)
185
223
}
186
224
}
187
225
}
226
+ CreateProperties ( _pkgDocument , documentProperties ) ;
188
227
}
189
228
catch ( Exception ex )
190
229
{
@@ -1205,6 +1244,8 @@ private DWS.WordprocessingShape CreatePartialShape(int Id, int X, int Y, int Wid
1205
1244
_numberingPart = _mainPart . NumberingDefinitionsPart ;
1206
1245
_wpBody = _pkgDocument . MainDocumentPart . Document . Body ;
1207
1246
1247
+ //LoadProperties(_pkgDocument);
1248
+
1208
1249
var sequence = 1 ;
1209
1250
var elements = new List < FF . IElement > ( ) ;
1210
1251
@@ -1312,6 +1353,65 @@ private DWS.WordprocessingShape CreatePartialShape(int Id, int X, int Y, int Wid
1312
1353
1313
1354
#endregion
1314
1355
1356
+ #region Load Core Properties for OpenXML Word Document
1357
+ internal DocumentProperties LoadProperties ( )
1358
+ {
1359
+ var corePart = _pkgDocument . CoreFilePropertiesPart ;
1360
+ DocumentProperties documentProperties = new DocumentProperties ( ) ;
1361
+
1362
+ if ( corePart != null )
1363
+ {
1364
+ // Load the XML document from the CoreFilePropertiesPart
1365
+ XDocument coreXml = XDocument . Load ( corePart . GetStream ( ) ) ;
1366
+
1367
+ // Define the namespaces used in core properties
1368
+ XNamespace dc = "http://purl.org/dc/elements/1.1/" ;
1369
+ XNamespace cp = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties" ;
1370
+ XNamespace dcterms = "http://purl.org/dc/terms/" ;
1371
+ // Extract metadata values using the appropriate XML elements
1372
+ documentProperties . Title = coreXml . Descendants ( dc + "title" ) . FirstOrDefault ( ) ? . Value ;
1373
+ documentProperties . Subject = coreXml . Descendants ( dc + "subject" ) . FirstOrDefault ( ) ? . Value ;
1374
+ documentProperties . Description = coreXml . Descendants ( dc + "description" ) . FirstOrDefault ( ) ? . Value ;
1375
+ documentProperties . Creator = coreXml . Descendants ( dc + "creator" ) . FirstOrDefault ( ) ? . Value ;
1376
+ documentProperties . Keywords = coreXml . Descendants ( cp + "keywords" ) . FirstOrDefault ( ) ? . Value ;
1377
+ documentProperties . LastModifiedBy = coreXml . Descendants ( cp + "lastModifiedBy" ) . FirstOrDefault ( ) ? . Value ;
1378
+ documentProperties . Revision = coreXml . Descendants ( cp + "revision" ) . FirstOrDefault ( ) ? . Value ;
1379
+ documentProperties . Created = coreXml . Descendants ( dcterms + "created" ) . FirstOrDefault ( ) ? . Value ;
1380
+ documentProperties . Modified = coreXml . Descendants ( dcterms + "modified" ) . FirstOrDefault ( ) ? . Value ;
1381
+ }
1382
+ else
1383
+ {
1384
+ //Console.WriteLine("Core properties not found.");
1385
+ }
1386
+ /**
1387
+ var customPart = pkgDocument.CustomFilePropertiesPart;
1388
+ if (customPart != null)
1389
+ {
1390
+ pkgDocument.DeletePart(customPart);
1391
+ }
1392
+ **/
1393
+ /**
1394
+ var coreProperties = new OT.CoreProperties();
1395
+ var dictCoreProp = new Dictionary<string, string>
1396
+ {
1397
+ ["Title"] = "Newly Created OWDocument",
1398
+ ["Subject"] = "WordProcessing OWDocument Generation",
1399
+ ["Keywords"] = "DOCX",
1400
+ ["Description"] = "A WordProcessing OWDocument Created from Scratch.",
1401
+ ["Creator"] = "Openize.Words"
1402
+ };
1403
+ var currentTime = System.DateTime.UtcNow;
1404
+ dictCoreProp["Created"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
1405
+ dictCoreProp["Modified"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
1406
+ coreProperties.CreateCoreFilePropertiesPart(pkgDocument.AddCoreFilePropertiesPart(), dictCoreProp);
1407
+ var customProperties = new OT.CustomProperties();
1408
+ customProperties.CreateExtendedFilePropertiesPart(pkgDocument.AddExtendedFilePropertiesPart());
1409
+ **/
1410
+ return documentProperties ;
1411
+ }
1412
+ #endregion
1413
+
1414
+
1315
1415
#region Load OpenXML Paragraph
1316
1416
internal FF . Paragraph LoadParagraph ( WP . Paragraph wpPara , int id )
1317
1417
{
0 commit comments