Skip to content

Commit eb19084

Browse files
authored
Merge pull request #12 from sabir-aspose/main
Read and Write Metadata for Word Documents
2 parents c118026 + 1af8400 commit eb19084

File tree

5 files changed

+161
-13
lines changed

5 files changed

+161
-13
lines changed
File renamed without changes.

Word/OoxmData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using FF = Openize.Words.IElements;
88
using Openize.Words;
99
using System.Linq;
10+
using System.Xml.Linq;
1011

1112
namespace OpenXML.Words.Data
1213
{
@@ -40,6 +41,7 @@ internal static OoxmlDocData CreateInstance(WordprocessingDocument doc)
4041
{
4142
return new OoxmlDocData(doc);
4243
}
44+
4345
internal static OoxmlDocData CreateInstance()
4446
{
4547
return new OoxmlDocData();
@@ -269,7 +271,6 @@ internal void Save(System.IO.Stream stream, Document doc)
269271

270272
_staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc);
271273

272-
//_ooxmlDoc.CreateProperties(_staticDoc);
273274
staticDoc.Clone(stream);
274275
}
275276
catch (Exception ex)

Word/OoxmlTemplate.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13121,16 +13121,19 @@ private void GeneratePartContent(CoreFilePropertiesPart part,
1312113121
//string formattedTime = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
1312213122
var title = dict["Title"];// "My Title";//dict["Title"];
1312313123
var subject = dict["Subject"];// "My Subject";//dict["Subject"];
13124+
var creator = dict["Creator"];
1312413125
var keywords = dict["Keywords"];//"My Keyword";//dict["Keywords"];
1312513126
var description = dict["Description"];// "My Description";//dict["Description"];
13126-
var creator = dict["Creator"];// "Openize.Words"; //dict["Creator"];
13127+
var lastModifiedBy = dict["LastModifiedBy"];
13128+
var revision = dict["Revision"];
1312713129
var created = dict["Created"];
1312813130
var modified = dict["Modified"];
1312913131
var writer = new System.Xml.XmlTextWriter(part.GetStream(
1313013132
System.IO.FileMode.Create), System.Text.Encoding.UTF8);
13131-
writer.WriteRaw("<?xml version=\"1.0\" encoding=\"utf-8\"?><coreProperties xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\"><dc:title>" + title + "</dc:title><dc:subject>" + subject + "</dc:subject><keywords>" + keywords + "</keywords><dc:description>" + description + "</dc:description><dcterms:created xsi:type=\"dcterms:W3CDTF\">" + created + "</dcterms:created><dcterms:modified xsi:type=\"dcterms:W3CDTF\">" + modified + "</dcterms:modified><dc:creator>" + creator + "</dc:creator></coreProperties>");
13133+
writer.WriteRaw("<?xml version=\"1.0\" encoding=\"utf-8\"?><coreProperties xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\"><dc:title>" + title + "</dc:title><dc:subject>" + subject + "</dc:subject><dc:creator>" + creator + "</dc:creator><keywords>" + keywords + "</keywords><dc:description>" + description + "</dc:description><lastModifiedBy>" + lastModifiedBy + "</lastModifiedBy><revision>" + revision + "</revision><dcterms:created xsi:type=\"dcterms:W3CDTF\">" + created + "</dcterms:created><dcterms:modified xsi:type=\"dcterms:W3CDTF\">" + modified + "</dcterms:modified></coreProperties>");
1313213134
writer.Flush();
1313313135
writer.Close();
13136+
//System.Console.WriteLine("Title: " + title);
1313413137
}
1313513138
}
1313613139
internal class CustomProperties

Word/OoxmlWord.cs

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using OWD = OpenXML.Words.Data;
1515
using OT = OpenXML.Templates;
1616
using Openize.Words;
17+
using System.Xml.Linq;
1718

1819
namespace 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
{

Word/OpenizeWord.cs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Document : IDisposable
3434
{
3535
private List<IElement> _lstStructure;
3636
private ElementStyles _elementStyles;// { get; internal set; }
37+
private DocumentProperties _documentProperties = new DocumentProperties();
3738
private OOXML.OwDocument _ooxmlDoc;
3839
private bool _isNew = true;
3940
//private static List<int> _instances = new List<int>();
@@ -115,9 +116,9 @@ public Document(string filename)
115116
{
116117
fs.CopyTo(_ms);
117118
}
118-
119119
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
120120
_lstStructure = _ooxmlDoc.LoadDocument(_ms);
121+
_documentProperties = _ooxmlDoc.LoadProperties();
121122
_elementStyles = _ooxmlDoc.LoadStyles();
122123
_originalSize = _lstStructure.Count;
123124

@@ -149,6 +150,7 @@ public Document(IO.Stream stream)
149150
stream.CopyTo(_ms);
150151
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
151152
_lstStructure = _ooxmlDoc.LoadDocument(_ms);
153+
_documentProperties = _ooxmlDoc.LoadProperties();
152154
_elementStyles = _ooxmlDoc.LoadStyles();
153155
_originalSize = _lstStructure.Count;
154156
}
@@ -174,15 +176,15 @@ public void Save(string filename)
174176
{
175177
if (!_isNew)
176178
{
177-
using var fs = new IO.FileStream(filename, IO.FileMode.Create);
178-
OWD.OoxmlDocData.CreateInstance().Save(fs, this);
179+
using (var fs = new IO.FileStream(filename, IO.FileMode.Create))
180+
OWD.OoxmlDocData.CreateInstance().Save(fs, this);
179181
}
180182
else
181183
{
182184
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
183-
_ooxmlDoc.CreateDocument(_lstStructure);
184-
using var fs = new IO.FileStream(filename, IO.FileMode.Create);
185-
_ooxmlDoc.SaveDocument(fs);
185+
_ooxmlDoc.CreateDocument(_lstStructure,_documentProperties);
186+
using (var fs = new IO.FileStream(filename, IO.FileMode.Create))
187+
_ooxmlDoc.SaveDocument(fs);
186188
}
187189
}
188190
catch (Exception ex)
@@ -213,7 +215,7 @@ public void Save(IO.Stream stream)
213215
else
214216
{
215217
_ooxmlDoc = OOXML.OwDocument.CreateInstance();
216-
_ooxmlDoc.CreateDocument(_lstStructure);
218+
_ooxmlDoc.CreateDocument(_lstStructure,_documentProperties);
217219
_ooxmlDoc.SaveDocument(stream);
218220
}
219221
}
@@ -253,6 +255,20 @@ public ElementStyles GetElementStyles()
253255
return _elementStyles;
254256
}
255257
/// <summary>
258+
/// Gets the core properties of the document.
259+
/// </summary>
260+
public DocumentProperties GetDocumentProperties()
261+
{
262+
return _documentProperties;
263+
}
264+
/// <summary>
265+
/// Sets the core properties of the document.
266+
/// </summary>
267+
public void SetDocumentProperties(DocumentProperties documentProperties)
268+
{
269+
_documentProperties = documentProperties;
270+
}
271+
/// <summary>
256272
/// Updates an existing element in the structure.
257273
/// </summary>
258274
/// <param name="element">The updated element to replace the existing one.</param>
@@ -1033,5 +1049,33 @@ public int AppendChild(IElement element)
10331049
}
10341050
}
10351051
}
1052+
1053+
public class DocumentProperties
1054+
{
1055+
public string Title { get; set; }
1056+
public string Subject { get; set; }
1057+
public string Description { get; set; }
1058+
public string Keywords { get; set; }
1059+
public string Creator { get; set; }
1060+
public string LastModifiedBy { get; set; }
1061+
public string Revision { get; set; }
1062+
public string Created { get; set; }
1063+
public string Modified { get; set; }
1064+
1065+
public DocumentProperties()
1066+
{
1067+
Title = "A WordProcessing Document";
1068+
Subject = "DOCX document created prgrammatically";
1069+
Description = "This word document is created programmatically by Openize.OpenXML-SDK for .NET";
1070+
Keywords = "docx";
1071+
Creator = "Openize.OpenXML-SDK for .NET";
1072+
LastModifiedBy = "Openize.OpenXML-SDK for .NET";
1073+
Revision = "1";
1074+
var currentTime = System.DateTime.UtcNow;
1075+
Created = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
1076+
Modified = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
1077+
}
1078+
}
1079+
10361080
}
10371081

0 commit comments

Comments
 (0)