首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OpenXML设置Word文档中的发布日期

如何使用OpenXML设置Word文档中的发布日期
EN

Stack Overflow用户
提问于 2016-09-06 10:46:10
回答 1查看 769关注 0票数 1

在我的情况下,用户可以上传包含某些属性占位符的word文档(即,在upload中,用户已经插入> Quick > document并选择了一个属性)。我希望支持的特定属性是TitleAuthorCompanyPublish Date

TitleAuthor设置为Package属性,Company设置为Extended属性。下面的代码设置了这些代码,这些代码正确地工作:

代码语言:javascript
复制
private static void SetDocumentProperties(ReportTemplateModel model, WordprocessingDocument wordDocument)
{
    //these properties always exist
    wordDocument.PackageProperties.Title = model.Title;
    wordDocument.PackageProperties.Creator = model.Author;
    wordDocument.PackageProperties.Created = DateTime.Now;
    wordDocument.PackageProperties.Modified = DateTime.Now;

    //these properties can be null
    if (wordDocument.ExtendedFilePropertiesPart == null)
    {
        wordDocument.AddNewPart<ExtendedFilePropertiesPart>();
    }
    if (wordDocument.ExtendedFilePropertiesPart.Properties == null)
    {
        wordDocument.ExtendedFilePropertiesPart.Properties = new Properties(new Company(model.SiteName));
    }
    else
    {
        wordDocument.ExtendedFilePropertiesPart.Properties.Company = new Company(model.SiteName);
    }
}

我的问题是,我无法确定如何设置Publish Date属性。我尝试使用下面的代码(从https://www.snip2code.com/Snippet/292005/WDSetCustomProperty中改编)将其添加为自定义文件属性,但这不起作用。我读过一些关于设置自定义属性的内容,但是我很困惑它们是如何工作的。我也不确定是否应该将Publish Date设置为自定义属性,还是其他类型的属性。

代码语言:javascript
复制
var customProps = wordDocument.CustomFilePropertiesPart;
if (customProps == null)
{
    customProps = wordDocument.AddCustomFilePropertiesPart();
    customProps.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties();
}
var properties1 = new DocumentFormat.OpenXml.CustomProperties.Properties();

//I have tried both of these lines, neither worked.
//properties1.AddNamespaceDeclaration("op", "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties");
properties1.AddNamespaceDeclaration("vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");

var customDocumentProperty1 = new DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty()
{
    FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
    PropertyId = 2,
    Name = "Publish Date"
};

customDocumentProperty1.Append(new DocumentFormat.OpenXml.VariantTypes.VTLPWSTR { Text = DateTime.Today.ToShortDateString() });

properties1.Append(customDocumentProperty1);

part.Properties = properties1;

应该将Publish Date设置为何种类型的属性,以及设置该属性的正确语法是什么?

更新:我发现Publish Date是一个可以使用下面的片段创建的CoverPageProperty,但是我仍然没有找到如何在文档中正确地设置它。

代码语言:javascript
复制
var coverPageProps = new DocumentFormat.OpenXml.Office.CoverPageProps.CoverPageProperties
{
    PublishDate = new PublishDate(DateTime.Today.ToShortDateString())
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-06 11:52:27

将下面的代码添加到我的SetDocumentProperties方法似乎是可行的。我必须承认我不完全理解下面的代码,所以任何解释都是欢迎的。此外,如果有一个解决方案不包括在C#中将XML作为字符串来编写,那么我更愿意避免这种情况。

代码语言:javascript
复制
const string publishDatePartId = "publishDatePart";

var publishDateXmlPart = wordDocument.MainDocumentPart.AddNewPart<CustomXmlPart>("application/xml", publishDatePartId);

var writer = new XmlTextWriter(publishDateXmlPart.GetStream(FileMode.Create), System.Text.Encoding.UTF8);

writer.WriteRaw($"<CoverPageProperties xmlns=\"http://schemas.microsoft.com/office/2006/coverPageProps\">" +
                $"<PublishDate>{DateTime.Today.ToShortDateString()}</PublishDate>" +
                $"</CoverPageProperties>");

writer.Flush();

writer.Close();

var customXmlPropertiesPart = publishDateXmlPart.AddNewPart<CustomXmlPropertiesPart>(publishDatePartId);

customXmlPropertiesPart.DataStoreItem = new DocumentFormat.OpenXml.CustomXmlDataProperties.DataStoreItem()
{
    //I don't know what this ID is, but it seems to somehow relate to the Publish Date
    ItemId = "{55AF091B-3C7A-41E3-B477-F2FDAA23CFDA}"
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39347121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档