. Net implementation code for Word replacement using Aspose. Words

Source: Internet
Author: User

In the previous work, you must implement the Word printing function and insert an image. The method adopted at that time was to use bookmarks for operations. First, insert the bookmarks in the word, and save the bookmarks as a template. The program loads the template, finds the bookmarks, and writes the text at the specified position.

In the later maintenance process, it is found that the template often needs to be changed, but it is not convenient to view the bookmarks in the word. Users are prone to errors when editing the word. So I want to replace it with a special string. At this time, there is a problem with the image insertion, And the cursor cannot be directly moved to the specified string.

Download resources:

Source http://www.jb51.net/codes/84240.html

Development ideas:

Check the API provided by Aspose. Words and find that the Range class has this method:

Copy codeThe Code is as follows: public int Replace (Regex pattern, IReplacingCallback handler, bool isForward );

The IReplacingCallback interface can be executed when the regular expression is used to replace the document.

The specific implementation code is as follows:

Copy codeThe Code is as follows: /* ===================================================== ==========================================================
* File name: Program
* Function description:
* Copyright (c) 2013 Wuhan Jingwei video Technology Co., Ltd.
* Creator: alone
* Creation Time: 11:16:19
* Modifier:
* Modification time:
* Description:
* Version: v1.0.0.0
* ===================================================== ======================================================= */
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;
Using Aspose. Words;
Namespace WordDemo
{
Class Program
{
Static void Main (string [] args)
{
Var dic = new Dictionary <string, string> ();
Dic. Add ("name", "Yang Mi ");
Dic. Add ("education", "Undergraduate ");
Dic. Add ("Contact", "02759597666 ");
Dic. Add ("Mailbox", "304885433@qq.com ");
Dic. Add ("Avatar", ". // 1.jpg ");
// Use bookmarks
Document doc = new Document (". // 1.doc ");
DocumentBuilder builder = new DocumentBuilder (doc );
Foreach (var key in dic. Keys)
{
Builder. MoveToBookmark (key );
If (key! = "Avatar ")
{
Builder. Write (dic [key]);
}
Else
{
Builder. InsertImage (dic [key]);
}
}
Doc. Save (" .doc"); // you can Save it as 1.doc compatible. 03-07
Console. WriteLine ("Bookmarks have been completed ");
// Replace with a special string
Doc = new Document (". // 2.doc ");
Foreach (var key in dic. Keys)
{
If (key! = "Avatar ")
{
Var repStr = string. Format ("& {0} &", key );
Doc. Range. Replace (repStr, dic [key], false, false );
}
Else
{
Regex reg = new Regex ("& Avatar &");
Doc. Range. Replace (reg, new ReplaceAndInsertImage (". // 1.jpg"), false );
}
}
Doc. Save ("replacement string replacement operation. doc"); // It can also be saved as 1.doc compatible 03-07
Console. WriteLine ("Special string replacement operation completed ");
Console. ReadKey ();
}
}

Public class ReplaceAndInsertImage: IReplacingCallback
{
/// <Summary>
/// Path of the image to be inserted
/// </Summary>
Public string url {get; set ;}

Public ReplaceAndInsertImage (string url)
{
This. url = url;
}

Public ReplaceAction Replacing (ReplacingArgs e)
{
// Obtain the current node
Var node = e. MatchNode;
// Obtain the current document
Document doc = node. Document as Document;
DocumentBuilder builder = new DocumentBuilder (doc );
// Move the cursor to the specified Node
Builder. MoveTo (node );
// Insert an image
Builder. InsertImage (url );
Return ReplaceAction. Replace;
}
}
}

Template:

Generate document:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.