|
8 | 8 | using Openize.Words;
|
9 | 9 | using System.Linq;
|
10 | 10 | using System.Xml.Linq;
|
| 11 | +using System.IO; |
11 | 12 |
|
12 | 13 | namespace OpenXML.Words.Data
|
13 | 14 | {
|
@@ -262,6 +263,141 @@ internal void Append(FF.IElement newElement, Document doc)
|
262 | 263 | }
|
263 | 264 | }
|
264 | 265 |
|
| 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 | + |
265 | 401 | internal void Save(System.IO.Stream stream, Document doc)
|
266 | 402 | {
|
267 | 403 | lock (_lockObject)
|
|
0 commit comments