使用自定义 XML 部分隐藏 Word 内容控件
我有一个 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();
}
有什么方法可以使这些内容控件实际上折叠/隐藏吗?
I have a word document with a bunch of content controls on it. These are mapped to a custom XML part. To build the document on the fly, I simply overwrite the custom XML part.
The problem I'm having, is that if I don't define a particular item, it's space is still visible in the document, pushing down the stuff below it, and looking inconsistent with the rest of the document.
Here's a basic example of my code:
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();
}
Is there any way to make these content controls actually collapse/hide?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须在 Word 中打开 docx 之前进行一些预处理,或者进行一些后处理(例如通过宏)。
作为预处理方法的一个示例,OpenDoPE 定义了一个“条件”,您可以使用它来排除未定义的内容。
I think you will have to do either some preprocessing before the docx is opened in Word, or some postprocessing (eg via a macro).
As an example of the preprocessing approach, OpenDoPE defines a "condition" which you could use to exclude the undefined stuff.