Skip to content

Commit 1ddab4d

Browse files
authored
Merge pull request #16 from sabir-aspose/main
Support for modifications to word document metadata
2 parents 8c897eb + 0a7f831 commit 1ddab4d

File tree

3 files changed

+140
-2
lines changed

3 files changed

+140
-2
lines changed

Openize.OpenXML-SDK.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
88
<Description>A .NET library that simplifies working with OpenXML documents. Create, read, and manipulate Wordprocessing documents (Docx), Excel spreadsheets (Xlsx), and PowerPoint presentations (Pptx) effortlessly with Openize.OpenXML SDK. Licensed under MIT.</Description>
9-
<ReleaseVersion>25.1.0</ReleaseVersion>
9+
<ReleaseVersion>25.4.1</ReleaseVersion>
1010
<SynchReleaseVersion>false</SynchReleaseVersion>
11-
<PackageVersion>25.1.0</PackageVersion>
11+
<PackageVersion>25.4.1</PackageVersion>
1212
<Authors>Openize Pty Ltd</Authors>
1313
<Copyright>2024-2025</Copyright>
1414
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
@@ -21,6 +21,7 @@
2121
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2222
<DocumentationFile>bin\Debug\netcoreapp3.1\Openize.OpenXMLSDK.xml</DocumentationFile>
2323
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
2425
<ItemGroup>
2526
<None Remove="DocumentFormat.OpenXml" />
2627

Word/OoxmData.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Openize.Words;
99
using System.Linq;
1010
using System.Xml.Linq;
11+
using System.IO;
1112

1213
namespace OpenXML.Words.Data
1314
{
@@ -262,6 +263,141 @@ internal void Append(FF.IElement newElement, Document doc)
262263
}
263264
}
264265

266+
/**
267+
internal void UpdateProperties(Document doc)
268+
{
269+
_staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc);
270+
DocumentProperties documentProperties = doc.GetDocumentProperties();
271+
staticDoc.Save();
272+
var corePart = staticDoc.CoreFilePropertiesPart;
273+
XDocument coreXml;
274+
275+
using (MemoryStream memoryStream = new MemoryStream())
276+
{
277+
// Read the core properties XML into memory
278+
using (Stream readStream = corePart.GetStream(FileMode.Open, FileAccess.Read))
279+
{
280+
readStream.CopyTo(memoryStream);
281+
}
282+
283+
memoryStream.Position = 0;
284+
coreXml = XDocument.Load(memoryStream);
285+
}
286+
287+
// Define the XML namespace for Dublin Core metadata
288+
XNamespace dc = "http://purl.org/dc/elements/1.1/";
289+
XNamespace cp = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
290+
XNamespace dcterms = "http://purl.org/dc/terms/";
291+
292+
// Find or create the <dc:title> element
293+
var titleElement = coreXml.Descendants(dc + "title").FirstOrDefault();
294+
if (titleElement != null)
295+
{
296+
titleElement.Value = documentProperties.Title; // Update existing title
297+
}
298+
else
299+
{
300+
coreXml.Root.Add(new XElement(dc + "title", documentProperties.Title)); // Add title if missing
301+
}
302+
303+
// Find or create the <dc:subject> element
304+
var subjectElement = coreXml.Descendants(dc + "subject").FirstOrDefault();
305+
if (subjectElement != null)
306+
{
307+
subjectElement.Value = documentProperties.Subject; // Update existing subject
308+
}
309+
else
310+
{
311+
coreXml.Root.Add(new XElement(dc + "subject", documentProperties.Subject)); // Add subject if missing
312+
}
313+
314+
// Find or create the <dc:creator> element
315+
var creatorElement = coreXml.Descendants(dc + "creator").FirstOrDefault();
316+
if (creatorElement != null)
317+
{
318+
creatorElement.Value = documentProperties.Creator; // Update existing creator
319+
}
320+
else
321+
{
322+
coreXml.Root.Add(new XElement(dc + "creator", documentProperties.Creator)); // Add creator if missing
323+
}
324+
325+
// Find or create the <cp:keywords> element
326+
var keywordsElement = coreXml.Descendants(cp + "keywords").FirstOrDefault();
327+
if (keywordsElement != null)
328+
{
329+
keywordsElement.Value = documentProperties.Keywords; // Update existing keywords
330+
}
331+
else
332+
{
333+
coreXml.Root.Add(new XElement(cp + "keywords", documentProperties.Keywords)); // Add keywords if missing
334+
}
335+
336+
// Save the updated XML back to the CoreFilePropertiesPart
337+
using (Stream writeStream = corePart.GetStream(FileMode.Create, FileAccess.Write))
338+
{
339+
coreXml.Save(writeStream);
340+
}
341+
342+
staticDoc.Save(); // Save the document after updating properties
343+
}
344+
**/
345+
346+
internal void UpdateProperties(Document doc)
347+
{
348+
_staticDocDict.TryGetValue(doc.GetInstanceInfo(), out WordprocessingDocument staticDoc);
349+
DocumentProperties documentProperties = doc.GetDocumentProperties();
350+
351+
staticDoc.Save();
352+
var corePart = staticDoc.CoreFilePropertiesPart;
353+
354+
XDocument coreXml;
355+
using (var memoryStream = new MemoryStream())
356+
{
357+
using (var readStream = corePart.GetStream(FileMode.Open, FileAccess.Read))
358+
{
359+
readStream.CopyTo(memoryStream);
360+
}
361+
362+
memoryStream.Position = 0;
363+
coreXml = XDocument.Load(memoryStream);
364+
}
365+
366+
XNamespace dc = "http://purl.org/dc/elements/1.1/";
367+
XNamespace cp = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
368+
XNamespace dcterms = "http://purl.org/dc/terms/";
369+
370+
// Helper to update or insert a value
371+
void UpdateElement(XNamespace ns, string name, string value)
372+
{
373+
if (string.IsNullOrWhiteSpace(value)) return;
374+
375+
var element = coreXml.Descendants(ns + name).FirstOrDefault();
376+
if (element != null)
377+
element.Value = value;
378+
else
379+
coreXml.Root.Add(new XElement(ns + name, value));
380+
}
381+
382+
UpdateElement(dc, "title", documentProperties.Title);
383+
UpdateElement(dc, "subject", documentProperties.Subject);
384+
UpdateElement(dc, "description", documentProperties.Description);
385+
UpdateElement(dc, "creator", documentProperties.Creator);
386+
UpdateElement(cp, "keywords", documentProperties.Keywords);
387+
UpdateElement(cp, "lastModifiedBy", documentProperties.LastModifiedBy);
388+
UpdateElement(cp, "revision", documentProperties.Revision);
389+
UpdateElement(dcterms, "created", documentProperties.Created);
390+
UpdateElement(dcterms, "modified", documentProperties.Modified);
391+
392+
using (var writeStream = corePart.GetStream(FileMode.Create, FileAccess.Write))
393+
{
394+
coreXml.Save(writeStream);
395+
}
396+
397+
staticDoc.Save();
398+
}
399+
400+
265401
internal void Save(System.IO.Stream stream, Document doc)
266402
{
267403
lock (_lockObject)

Word/OpenizeWord.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public DocumentProperties GetDocumentProperties()
267267
public void SetDocumentProperties(DocumentProperties documentProperties)
268268
{
269269
_documentProperties = documentProperties;
270+
if(!_isNew) OWD.OoxmlDocData.CreateInstance().UpdateProperties(this);
270271
}
271272
/// <summary>
272273
/// Updates an existing element in the structure.

0 commit comments

Comments
 (0)