[英]How to correctly apply style to Open Office XML Bookmark text
I have prepared a MS Word document template ,and inserted bookmarks where I would like to insert text. 我准备了MS Word文档模板,并在要插入文本的位置插入了书签。 Currently, I am able to insert text, but all native styling is gone (ie: font size/style) I was expecting the styles to be inherited. 目前,我能够插入文本,但是所有本机样式都消失了(即:字体大小/样式),我希望样式能够被继承。
I've been referring to this question for inspiration, but I receive malformed XML: 我一直在参考这个问题来寻求启发,但是我收到了格式错误的XML:
<w:rFonts w:ascii="Playfair Display" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
Here is a snippet of the bookmark start/end I am working with: 这是我正在使用的书签开始/结束的代码片段:
<?xml version="1.0" encoding="UTF-8"?>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidR="00300E4C" w:rsidRDefault="00582F11">
<w:pPr>
<w:spacing w:before="265" />
<w:ind w:left="2822" w:right="1122" />
<w:jc w:val="center" />
<w:rPr>
<w:rFonts w:ascii="Playfair Display" />
<w:i />
<w:sz w:val="88" />
</w:rPr>
</w:pPr>
<w:bookmarkStart w:name="F_4000" w:id="0" />
<w:bookmarkEnd w:id="0" />
<w:r>
<w:rPr>
<w:rFonts w:ascii="Playfair Display" />
<w:i />
<w:color w:val="113628" />
<w:sz w:val="88" />
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:bookmarkStart w:name="F_4002" w:id="1" />
<w:bookmarkEnd w:id="1" />
</w:p>
When using the following code: 使用以下代码时:
IEnumerable<OpenXmlElement> elementsAfter = bookmark.ElementsAfter();
IEnumerable<OpenXmlElement> insideBookmark = elementsAfter.TakeWhile(element => !(element is BookmarkEnd));
foreach (OpenXmlElement element in insideBookmark)
{
element.RemoveAllChildren();
}
OpenXmlElement previousSibling = bookmark.PreviousSibling();
while (previousSibling is BookmarkStart || previousSibling is BookmarkEnd)
{
previousSibling = previousSibling.PreviousSibling();
}
//Get previous font.
var runProperties = previousSibling.GetFirstChild<ParagraphMarkRunProperties>().GetFirstChild<RunFonts>();
//var runProperties = previousSibling.GetFirstChild<RunProperties>(); - if its simple element.
// Clone.
var newProperty = (RunFonts)runProperties.Clone();
// Create container with properties
// This is where I run into malformed XML for newProperty
var container = new Run(text)
{
RunProperties = new RunProperties() { RunFonts = newProperty }
};
EDIT: After inserting some text into the bookmark locations here is the XML (the paragraph XML that contains the XML) 编辑:将一些文本插入书签位置后,这里是XML(包含XML的段落XML)
<w:p w:rsidR="00300E4C" w:rsidRDefault="00582F11" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:spacing w:before="265" />
<w:ind w:left="2822" w:right="1122" />
<w:jc w:val="center" />
<w:rPr>
<w:rFonts w:ascii="Playfair Display" />
<w:i />
<w:sz w:val="88" />
</w:rPr>
</w:pPr>
<w:r>
<w:t>First</w:t>
</w:r>
<w:bookmarkStart w:name="F_4000" w:id="0" />
<w:bookmarkEnd w:id="0" />
<w:r>
<w:rPr>
<w:rFonts w:ascii="Playfair Display" />
<w:i />
<w:color w:val="113628" />
<w:sz w:val="88" />
</w:rPr>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r>
<w:t>Last</w:t>
</w:r>
<w:bookmarkStart w:name="F_4002" w:id="1" />
<w:bookmarkEnd w:id="1" />
</w:p>
I'm just trying to grab the native (already active) style, but I think the bookmark is breaking it up somehow. 我只是想尝试获取本机(已处于活动状态)样式,但是我认为书签正在以某种方式将其分解。 Any suggestions are much appreciated. 任何建议,不胜感激。
The way I eventually figured out is below. 我最终想出的方法如下。 The biggest help was using the Open XML SDK Productivity Tool to figure out what sort of structure I need to be inserting into. 最大的帮助是使用Open XML SDK生产率工具来确定我需要插入哪种结构。 The two spots I needed to input text into were situated in a strange manner, so it was easier to do them in one go, and for the rest of the elements I just InsertAfterSelf() 我需要输入文本的两个位置以一种奇怪的方式放置,因此一口气就能轻松完成,对于其余元素,我只需要插入AfterSelf()
if (bookMarkEnd.Key == "F_4000" || bookMarkEnd.Key == "F_4002")
{
OpenXmlElement previousSibling = bookMarkEnd.Value.Parent.PreviousSibling();
while (previousSibling is BookmarkStart || previousSibling is BookmarkEnd)
{
previousSibling = previousSibling.PreviousSibling();
}
OpenXmlElement next = bookMarkEnd.Value.NextSibling<Run>();
if (next != null)
{
next.GetFirstChild<Text>().Text = bookMarkEnd.Key + " " + "F_4002";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.