English 中文(简体)
替换案文Box
原标题:Replacing InnerText of a TextBox

利用MS OpenXml Sdk 在机构结束时,我能够使用以下代码复制一个模板流,并附上动态案文(w.p>>>w.t):

var templateStream = File.OpenRead(templatePath);
templateStream.CopyTo(resultStream);

using (var resultPackage = WordprocessingDocument.Open(resultStream, true))
{
     var document = resultPackage.MainDocumentPart.Document;

     var body = document.Body;

     // Add new text.
     var para = body.AppendChild(new Paragraph());
     var run = para.AppendChild(new Run());
     run.AppendChild(new Text(firstName));
     document.Save();
}

我的下一个合乎逻辑的步骤是,将结果Stream中文本框的内文改为下文守则中的第一行。

// replacing code in using statement from above
var document = resultPackage.MainDocumentPart.Document;
var textbox = document.Descendants<TextBox>().First();
const string firstNametag = "<<IH.FirstName>>";
if (textbox.InnerText.Contains(firstNametag))
{
   var textboxContent = textbox.Elements<TextBoxContent>().First();
   textboxContent.RemoveAllChildren();
   var paragraph = textboxContent.AppendChild(new Paragraph());
   var run = paragraph.AppendChild(new Run());
   run.AppendChild(new Text(firstName));
}
document.Save();

在第一个例子中,加上一些补充代码,结果流被适当编成单体,第一Name在文中看到后被附在体尾。 在第二个例子中,尽管在变迁者中进行的进一步审查显示,文字箱中的儿童反映了上述变化,但文本箱及其内容仍然相同。

我是开放式反洗钱法发展的新鲜事,如果有任何明显的话,请指出这一点。

最佳回答

吴 Oh,我甚至不想想一想,我会理解整个情况,但这里却一 quick。 更开放 ......

当你在印章上打上文字箱时,文件xml文档的标记如下:

<w.r>
    <mc.AlertnateContent>
        <mc.Choice Requires="wps">
            <wps:txbx>
                <w:txbxContent>
                    <w:r>
                        <w.t>
                            Text Goes Here
                        </w.t>
                    </w.r>
                </w:txbxContent>
            </wps:txbx>
        </mc.Choice>
        <mc.Fallback>
            <v.textbox>
                <w:txbxContent>
                    <w:r>
                        <w.t>
                            Text Goes Here
                        </w.t>
                    </w.r>
                </w:txbxContent>
            </v.textbox>
        </mc.Fallback>
    </mc.AlertnateContent>
</w.r>

通知 m、 m和 m。 背后标签。 什么? 有些人将这种方式放在一条博客上:

"As I understand it - but don t take my word as gospel - AlternateContent can appear anywhere and provides a mechanism for including enhanced functionality if the consuming application can handle it, along with a fallback if it can t." -Tony Jollans - http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f8a5c277-7049-48c2-a295-199d2914f4ba/

In my case I was only modifying the fallback textbox(v.txbx not wps.txbx) because of my mishap in assuming Resharper was right in asking me to import the DocumentFormat.OpenXml.Vml namespace for my dependency on the TextBox object. Not sure why there isn t a Textbox definition in one of my already included namespaces, DocumentFormat.OpenXml.Packaging or DocumentFormat.OpenXml.Wordprocessing but that s beyond the scope of this question. Needless to say, upon realizing this and updating my code to look for the common w.txbxContent for the two I achieved what I wanted to do.

Here s the updated code with some refactoring, call the ReplaceTag method in the using statement from the original question and supply a model object instead of a string. Also, use the tagToValueSelector dictionary for convenience.

private void ReplaceTags(Document document, SomeModel model)
{
    var textBoxContents = document.Descendants<TextBoxContent>().ToList();
    foreach (var textBoxContent in textBoxContents)
    {
        ReplaceTag(textBoxContent, model);
    }
}

private void ReplaceTag(TextBoxContent textBoxContent, SomeModel model)
{
    var tag = textBoxContent.InnerText.Trim();
    if (!tagsTomValues.ContainsKey(tag)) return;
    var valueSelector = tagsTomValues[tag];
    textBoxContent.RemoveAllChildren();
    var paragraph = textBoxContent.AppendChild(new Paragraph());
    var run = paragraph.AppendChild(new Run());
    run.AppendChild(new Text(valueSelector(model)));
}

// called in the ctor
private static void IntializeTags(IDictionary<string, Func<SomeModel, string>> dictionary)
{
    dictionary.Add("<<IH.Name>>", m => string.Format("{0} {1}", m.FirstName, m.LastName));
}

快乐:

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签