0

我们有一个以 [Signature] 键为段落的 word 文档,我们需要做的就是用一些名称替换签名,根据我们需要重复 [signature] 键的名称。

Ex: if names are containing 10 to 15 characters  it should be repeat 2 times in a row like below

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  


if names are containing 5 charecters should be repeat 3 times

XXXXXXXXXXXXXX            XXXXXXXXXXXXXX       XXXXXXXXXXXXXXX

based on the name node will repeat...?

please help how to solve this task ......
4

1 回答 1

1

为了在 word 文档中查找和替换文本,Aspose.Words 提供了 IReplacingCallback 接口,可以很容易地用于实现您的目标。我使用静态字符串来测试场景,因为我没有您的数据源的详细信息。您将需要根据名称长度在您的代码中添加一个检查,您需要添加签名。检查以下示例:

//Open the file
Document doc = new Document("c:\\data\\Signature.docx");

//Specify the string / tag to be replace 
doc.Range.Replace(new Regex(@"\[Signature\]", RegexOptions.IgnoreCase), new ReplaceEvaluatorSignature(), false);

//Save the updated document
doc.Save("c:\\data\\Output.docx");



 /// <summary>
/// Class to change the signature 
/// </summary>
public class ReplaceEvaluatorSignature : IReplacingCallback
{

    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// This method highlights the match string, even if it spans multiple runs.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {

        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)

            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&

            (currentNode != null) &&

            (currentNode.GetText().Length <= remainingLength))
        {

            runs.Add(currentNode);

            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.

            do
            {

                currentNode = currentNode.NextSibling;

            }

            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

        }





        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
        {

            SplitRun((Run)currentNode, remainingLength);

            runs.Add(currentNode);

        }

        //Name is defined for testing, replace it with your data source
       // string TestName = "Nausherwan Aslam";

        //Following is to test less or equal to 10 charators
        string TestName = "Nausherwan";

        // Create Document Buidler 
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);

        builder.MoveTo((Run)runs[runs.Count - 1]);

        if (TestName.Length > 10)
        {
            builder.Write(TestName+ "        " +  TestName);
        }
        else
        {
            builder.Write(TestName + "          " + TestName + "         " + TestName);
        }
        // Now remove all runs in the sequence.
        foreach (Run run in runs)
            run.Remove();

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;

    }

    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, position);

        run.ParentNode.InsertAfter(afterRun, run);

        return afterRun;

    }

}
于 2014-05-29T09:08:16.480 回答