Hello there,
We are looking to purchase Aspose.Word, however we have to test the conversion of some macro into Aspose code.
Those macro are find and replace pattern, they do the following :
- look for custom tag at the bottom of each page and replace it the current page number.
- lookup text between custom tag and copy it.
As I am reading about Aspose, I have spotted the .Replace features, they seem good fit for the need. However, I haave the following problem :
- I cannot replace the custom with the page number, somehow giving unique integer as value makes it crash (detail code and error might be needed as time)
So i’m asking myself :
- Is this features is right to accomplish this “find, scan & replace” ?
- Is there any way to get current page number implementing IReplaceCallBack ? Tricks ?
- Anyway to replace a custom field, used many times with different values ?
- Is it Worth pushing it or am I better get stuck with Interop and go for some sort of hybrid solutions ? The merging and printing features goes pretty well so far, i’d like to get ride of Automation !
Thanks !
Runtime :
Document doc = new Document(“test.doc);
doc.Range.Replace(new Regex(@”"), new MyReplaceEvaluator(), true);
…
…
public class MyReplaceEvaluator : IReplacingCallback
{
static int mMatchNumber = 1;
public ReplaceAction Replacing(ReplacingArgs e)
{
e.Replacement = mMatchNumber.ToString();
mMatchNumber++;
return ReplaceAction.Replace;
}
Hello Tahir,
Thanks for your response, the code you have provided perfectly answer our need for replacing tag.
Our last requirement, which is a little more complexe, is the following :
1) Find the opening tag
2) Find the closing tag
3) Copy the text between those two tags
4) Past it 3 times
5) Remove the tags.
Document spécifications looks like the following :
- The document we are working with only have one section
- In the tag, the number of occurrence to be pasted is defined in an attribute.
- The closing tag can be in another page of the document
- The tag looks like it :
< COPIER 3 FOIS AVANT >
CANADA
< /COPIER >
where “3 FOIS AVANT” is the number of past that has to be made.
I have taken a look at the “ExtractContent” page, where I can find an implementation of the replace content methods, however the field I want to set as StartField and EndField are not static (can be placed anywhere in the document) and need to be searched in the document.
I think this could be achieved by the following :
1) Implement IReplacingCallback to find the starting tag
2) Look in the document until the closing tag is found
3) Call the ExtractContent with the start and end field found
Would that be the best algorithm to achieve this dynamic copy content task ? I get some “Could not insert Node at the specified location”
Best regards,
Hello Tahir,
I am able to find the appropriate tags using GetChildNodes, looping for the exact string.
This is being done by the following code :
var childs = doc.GetChildNodes(NodeType.Any, true);
Node ndDebut = null;
Node ndFin = null;
for (int i = 0; i < childs.Count; i++)
{
if (childs[i].GetText().Equals(@"<copier 3="" fois="" avant="">"))
{
<copier 3="" fois="" avant="">ndDebut = childs[i];
<copier 3="" fois="" avant="">}
<copier 3="" fois="" avant="">}
<br>for (int i = 0; i < childs.Count; i++)
{
<copier 3="" fois="" avant="">if (childs[i].GetText().Equals(@""))
{
ndFin = childs[i];
break;
}
}
Then I try to parse the content using the example you provided :
public static Document CopierColler(Document p_doc, Node start, Node end)
{
<br><blockquote>ArrayList extractedNodes = ExtractContent(start, end, true);<br>extractedNodes.Reverse();
<br>while (extractedNodes.Count > 0)
<br>{
<br><blockquote>end.ParentNode.InsertAfter((Node)extractedNodes[0], (Node)end);
extractedNodes.RemoveAt(0);
}
return p_doc;
}
However, I get this message : “Cannot insert a node of this type at this location.”
Any idea ?
Hello Tahir,
it seems that the code provided by the demo does not support PageBreak !! Could you validate that please ?
Thanks
Hi Richard,
Thanks for your inquiry.
RitchGreen:
Our last requirement, which is a little more complexe, is the following :
1) Find the opening tag
2) Find the closing tag
3) Copy the text between those two tags
4) Past it 3 times
5) Remove the tags.
Document spécifications looks like the following :
- The document we are working with only have one section
- In the tag, the number of occurrence to be pasted is defined in an attribute.
- The closing tag can be in another page of the document
- The tag looks like it :
< COPIER 3 FOIS AVANT >
CANADA
< /COPIER >
In your case, I suggest you following solution of your query.
1) Implement IReplacingCallback interface as shared
here (in this forum thread) to find the start custom tag and insert bookmark e.g startbookmark. Similarly, find the end custom tag and insert another bookmark e.g endbookmark
2) Extract contents using the code shared here:
http://www.aspose.com/docs/display/wordsnet/Extract+Content+from+a+BookmarkNote : in this step, you need to use the BookmarkStart of startbookmark and BookmarkEnd of endbookmark.
3) After extracting the contents, insert the extracted Document at your desired location using the
InsertDocument method shared here:
http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+DocumentYou can call InsertDocument method as much times you want. Hope this helps you.
Hi Richard,
Thanks for your inquiry.
RitchGreen:
it seems that the code provided by the demo does not support PageBreak !! Could you validate that please ? Thanks
Please create a standalone/runnable simple application (
for example a Console Application Project) that demonstrates the code you used to generate your
output document and share it here along with
input document. We will then provide you more information about your query along with code.