DocumentFormat.OpenXml 修改文档的创建者 属性
DocumentFormat.OpenXml Modify Creator Propery of Document
我希望用其他内容替换 Word Document Creator 元数据,但在调用 WordprocessingDocument.Save()
时它没有保留。我正在利用 DocumentFormat.OpenXML version 2.7.2 和以下代码来加载和更改 Creator:
public static SetCreator(Stream inputStream, string newCreator)
{
//the inputStream is actually a MemoryStream
WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true);
document.PackageProperties.Creator = newCreator;
document.Save();
//on this point i expect the original stream to be replaced with the new one
}
该文档仅可作为流使用,因为它适用于 Web 应用程序项目。但是,当我尝试在调试模式下重新读取流时,document
实例上的更改似乎没有保留。此外,它不会触发任何异常。
编辑:进一步测试,使用旧的DocumentFormat.OpenXML(版本2.0.50022。WordprocessingDocument
class没有Save()
方法,而是 Close()
方法。尝试将代码修改为以下内容并且它有效。但仍然想知道 2.7.2 上的正确方法。
public static SetCreator(Stream inputStream, string newCreator)
{
//the inputStream is actually a MemoryStream
WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true);
document.PackageProperties.Creator = newCreator;
document.Close(); //On older version, calling this will save it into memory stream without actually closing it.
}
你能试试这个吗:
// Change MemoryStream by FileStream
public static SetCreator(FileStream inputStream, string newCreator)
{
using (WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true))
{
document.PackageProperties.Creator = newCreator;
// You shouldn't need to do document.Save()
}
}
// Main code
path = "C:\myPath";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
SetCreator(fs, "ME");
}
我希望用其他内容替换 Word Document Creator 元数据,但在调用 WordprocessingDocument.Save()
时它没有保留。我正在利用 DocumentFormat.OpenXML version 2.7.2 和以下代码来加载和更改 Creator:
public static SetCreator(Stream inputStream, string newCreator)
{
//the inputStream is actually a MemoryStream
WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true);
document.PackageProperties.Creator = newCreator;
document.Save();
//on this point i expect the original stream to be replaced with the new one
}
该文档仅可作为流使用,因为它适用于 Web 应用程序项目。但是,当我尝试在调试模式下重新读取流时,document
实例上的更改似乎没有保留。此外,它不会触发任何异常。
编辑:进一步测试,使用旧的DocumentFormat.OpenXML(版本2.0.50022。WordprocessingDocument
class没有Save()
方法,而是 Close()
方法。尝试将代码修改为以下内容并且它有效。但仍然想知道 2.7.2 上的正确方法。
public static SetCreator(Stream inputStream, string newCreator)
{
//the inputStream is actually a MemoryStream
WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true);
document.PackageProperties.Creator = newCreator;
document.Close(); //On older version, calling this will save it into memory stream without actually closing it.
}
你能试试这个吗:
// Change MemoryStream by FileStream
public static SetCreator(FileStream inputStream, string newCreator)
{
using (WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true))
{
document.PackageProperties.Creator = newCreator;
// You shouldn't need to do document.Save()
}
}
// Main code
path = "C:\myPath";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
SetCreator(fs, "ME");
}