我的情况是,用户可以上传包含某些属性占位符的word文档(例如,在MS Word中,用户已经转到插入>快速部件>文档属性并选择了属性)。我要支持的具体属性是Title
,Author
,Company
和Publish Date
。
Title
和Author
设置为包属性,Company
设置为扩展文件属性。这些是使用以下代码设置的,它可以正常工作:
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
是否应该实际设置为自定义属性或其他类型的属性。
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
,可以使用下面的代码段创建,但我仍然无法找到如何设置它正确地在文件中。
var coverPageProps = new DocumentFormat.OpenXml.Office.CoverPageProps.CoverPageProperties
{
PublishDate = new PublishDate(DateTime.Today.ToShortDateString())
};
答案 0 :(得分:0)
将以下代码添加到我的SetDocumentProperties方法似乎可行。我必须承认我不完全理解下面的代码,所以任何解释仍然是受欢迎的。另外,如果有人的解决方案不包括在C#中将XML写为字符串,我宁愿避免这种情况。
public void addStudent(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException,SystemException {
try {
System.out.println("Student : "+StudentLocalServiceUtil.getStudentByGenderMale());
String firstName = ParamUtil.getString(actionRequest, "firstName");
String lastName = ParamUtil.getString(actionRequest, "lastName");
int studentAge = ParamUtil.getInteger(actionRequest, "studentAge");
int studentGender = ParamUtil.getInteger(actionRequest, "sex", 1);
String address = ParamUtil.getString(actionRequest, "address");
String teacherName=(String) actionRequest.getParameter("teacher");
System.out.println("teacher name " + teacherName);
// add student record
// create primary key
long studentId = CounterLocalServiceUtil.increment();
// create student persistance object
Student student = null;
student = StudentLocalServiceUtil.createStudent(studentId);
// fill the data in persistance object
student.setFirstName(firstName);
student.setLastName(lastName);
student.setStudentAge(studentAge);
student.setStudentGender(studentGender);
student.setStudentAddress(address);
// Add student persistance object to database student table
student = StudentLocalServiceUtil.addStudent(student);
if (student != null) {
// adding success message
SessionMessages.add(actionRequest.getPortletSession(),
"student-add-success");
_log.info("Student have been added successfylly");
} else {
SessionErrors.add(actionRequest.getPortletSession(),
"student-add-error");
_log.error("There is an Erron in adding Student");
}
// navigate to add student jsp page
actionResponse.setRenderParameter("mvcPath",
"/html/jsps/add_student.jsp");
} catch (Exception e) {
SessionErrors.add(actionRequest.getPortletSession(),
"student-add-error");
e.printStackTrace();
}
}