1414using OWD = OpenXML . Words . Data ;
1515using OT = OpenXML . Templates ;
1616using Openize . Words ;
17+ using System . Xml . Linq ;
1718
1819namespace OpenXML . Words
1920{
@@ -38,7 +39,7 @@ private OwDocument()
3839 _mainPart . Document = new WP . Document ( ) ;
3940 var tmp = new OT . DefaultTemplate ( ) ;
4041 tmp . CreateMainDocumentPart ( _mainPart ) ;
41- CreateProperties ( _pkgDocument ) ;
42+ // CreateProperties(_pkgDocument);
4243
4344 _numberingPart = _mainPart . NumberingDefinitionsPart ;
4445
@@ -91,6 +92,7 @@ private OwDocument(WordprocessingDocument pkg)
9192 }
9293 }
9394
95+ /**
9496 #region Create Core Properties for OpenXML Word Document
9597 internal void CreateProperties(WordprocessingDocument pkgDocument)
9698 {
@@ -105,13 +107,14 @@ internal void CreateProperties(WordprocessingDocument pkgDocument)
105107 pkgDocument.DeletePart(customPart);
106108 }
107109 var coreProperties = new OT.CoreProperties();
110+
108111 var dictCoreProp = new Dictionary<string, string>
109112 {
110113 ["Title"] = "Newly Created OWDocument",
111114 ["Subject"] = "WordProcessing OWDocument Generation",
112115 ["Keywords"] = "DOCX",
113116 ["Description"] = "A WordProcessing OWDocument Created from Scratch.",
114- [ "Creator" ] = "Openize.Words "
117+ ["Creator"] = "Openize.OpenXML-SDK "
115118 };
116119 var currentTime = System.DateTime.UtcNow;
117120 dictCoreProp["Created"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
@@ -121,6 +124,41 @@ internal void CreateProperties(WordprocessingDocument pkgDocument)
121124 customProperties.CreateExtendedFilePropertiesPart(pkgDocument.AddExtendedFilePropertiesPart());
122125 }
123126 #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
124162
125163 public static OwDocument CreateInstance ( )
126164 {
@@ -135,7 +173,7 @@ public static OwDocument CreateInstance(WordprocessingDocument pkg)
135173 #region Create OpenXML Word Document Contents Based on Openize.Words.IElements
136174
137175 #region Main Method
138- internal void CreateDocument ( List < FF . IElement > lst )
176+ internal void CreateDocument ( List < FF . IElement > lst , DocumentProperties documentProperties )
139177 {
140178 try
141179 {
@@ -185,6 +223,7 @@ internal void CreateDocument(List<FF.IElement> lst)
185223 }
186224 }
187225 }
226+ CreateProperties ( _pkgDocument , documentProperties ) ;
188227 }
189228 catch ( Exception ex )
190229 {
@@ -1205,6 +1244,8 @@ private DWS.WordprocessingShape CreatePartialShape(int Id, int X, int Y, int Wid
12051244 _numberingPart = _mainPart . NumberingDefinitionsPart ;
12061245 _wpBody = _pkgDocument . MainDocumentPart . Document . Body ;
12071246
1247+ //LoadProperties(_pkgDocument);
1248+
12081249 var sequence = 1 ;
12091250 var elements = new List < FF . IElement > ( ) ;
12101251
@@ -1312,6 +1353,65 @@ private DWS.WordprocessingShape CreatePartialShape(int Id, int X, int Y, int Wid
13121353
13131354 #endregion
13141355
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+
13151415 #region Load OpenXML Paragraph
13161416 internal FF . Paragraph LoadParagraph ( WP . Paragraph wpPara , int id )
13171417 {
0 commit comments