我有一个带有一堆内容控件的 word 文档。这些被映射到自定义 XML 部分。为了即时构建文档,我只需覆盖自定义 XML 部分。
我遇到的问题是,如果我没有定义特定项目,它的空间在文档中仍然可见,将其下方的内容向下推,看起来与文档的其余部分不一致。
这是我的代码的一个基本示例:
var path = HttpContext.Current.Server.MapPath("~/Classes/Word/LawyerBio.docx");
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(path, true))
{
//create new XML string
//these values will populate the template word doc
string newXML = "<root>";
if (!String.IsNullOrEmpty(_lawyer["Recognition"]))
{
newXML += "<recognition>";
newXML += _text.Field("Recognition Title");
newXML += "</recognition>";
}
if (!String.IsNullOrEmpty(_lawyer["Board Memberships"]))
{
newXML += "<boards>";
newXML += _text.Field("Board Memberships Title");
newXML += "</boards>";
}
newXML += "</root>";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
//delete old xml part
mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);
//add new xml part
CustomXmlPart customXml = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
using(StreamWriter ts = new StreamWriter(customXml.GetStream()))
{
ts.Write(newXML);
}
myDoc.Close();
}
有没有办法让这些内容控件真正折叠/隐藏?
最佳答案
我认为您必须在 Word 中打开 docx 之前进行一些预处理,或者进行一些后处理(例如通过宏)。
作为预处理方法的一个例子,OpenDoPE定义一个“条件”,您可以使用它来排除未定义的内容。
关于c# - 使用自定义 XML 部分隐藏 Word 内容控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749501/