我的需求是在word中任意位置添加一个域变量,但是如何定位到对应的位置难到了,我在论坛中搜索到类似的需求,建议是参考这个issue,但是我没有找到对应的地址,可以帮我一下么。
WORDSNET-10148
我担心,没有直接的方法可以用来自己跟踪问题。 但是,欢迎您通过论坛帖子询问您的问题状态。 我们将从内部问题跟踪系统验证状态并回复您。
其次,这个问题(WORDSNET-10148)实际上要求我们在Aspose.Words API中实现一个新功能,我们很遗憾与你分享这个问题的实现目前已被推迟。 目前没有可用的估算值。 但是,这个问题的修复肯定会在未来的产品路线图中出现。 此主题也已链接到相应的问题(WORDSNET-10148),一旦解决,您将收到通知。 抱歉给你带来不便。
关于WORDSNET-10148,您可以使用以下代码作为解决方法。 请参阅这些示例输入/输出Word文档。 希望这可以帮助:
Docs.zip (17.6 KB)
int paragraphIndex = 0;
int characterIndex = 4;
string text = "demo";
Document doc = new Document(@"E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph targetPara = (Paragraph)builder.CurrentStory.GetChildNodes(NodeType.Paragraph, true)[paragraphIndex];
Node[] runs = targetPara.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
Run run = (Run)runs[i];
int length = run.Text.Length;
Run currentNode = run;
for (int x = 1; x < length; x++)
{
currentNode = SplitRun(currentNode, 1);
}
}
if (characterIndex >= 0 && characterIndex < targetPara.Runs.Count)
{
builder.MoveTo(targetPara.Runs[characterIndex]);
builder.Font.Name = "Verdana";
builder.Font.Size = 16;
builder.Font.Color = Color.Red;
builder.Write(text);
}
else
{
Console.WriteLine("Incorrect character index specified");
}
doc.JoinRunsWithSameFormatting();
doc.Save(@"E:\\Temp\\20.3.docx");
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
我们还将在以后解决WORDSNET-10148时通过此线程通知您。
The issues you have found earlier (filed as WORDSNET-10148) have been fixed in this Aspose.Words for .NET 21.2 update and this Aspose.Words for Java 21.2 update.