We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove text between two lines and insert document

Hello,

Is it possible without using bookmarks remove text between two lines?

Please see attached file.

First line with words: APPENDIX "A"

Second line with words: END OF APPENDIX "A"

Then insert another document between these two lines.

Thanks,

RK

Hi Rudolf,

Thanks for your request. I think, you can use a similar approach suggested here to extract/remove content between user defined strings and then insert new content:

https://forum.aspose.com/t/99344However, you should note that there were few changes in Aspose.Words API. So you should use IReplacingCallback instead of ReplaceEvaluator:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.ireplacingcallback.html

Hope this helps. Please let me know if you need more assistance, I will be glad to help you.

Best regards,

Hi, Alexey,

Can I programmatically create bookmark in the existing document?

For instance, programmatically find word “Appendix”, highlighted it and create new bookmark.

Thanks,

RK

Hi Rudolf,

Thanks for your request. Sure, you can create bookmarks programmatically using Aspose.Words. Please follow the link to learn how to achieve this:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.documentbuilder.startbookmark.html

Best regards,

Hi Alexey,

Do you have some sample code how to find specific word or line in document and

how to MoveTo( ) to this place.

For examle I need to find "Appendix 'A' " and create bookmark

Thanks,

RK

Hello

Thanks for your inquiry. I think in your case you can try using IReplacingCallback. Please see the following link for more information and to find some code examples:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.ireplacingcallback.html

Please let me know in case of any issues. I will be glad to help you.

Best regards,

But first I need to remove text between two lines:

First line: Appendix "A"

Second line: END OF APPENDIX "A"

How can I do this?

Please see attached file what I did send with first post for this issue

Thanks,

RK

Hello

Thanks for your request. Please try using the following code:

Document doc = new Document("C:\\Temp\\sample.doc");

// We want the "your document" phrase to be highlighted.

Regex regex1 = new Regex("APPENDIX \"A\"");

Regex regex2 = new Regex("END OF APPENDIX \"A\"");

doc.Range.Replace(regex1, new ReplaceEvaluatorFindAndReplace(), true);

doc.Range.Replace(regex2, new ReplaceEvaluatorFindAndReplace(), true);

doc.Range.Bookmarks["APPENDIX"].Text = "New Content";

doc.Save("C:\\Temp\\out.doc");

private class ReplaceEvaluatorFindAndReplace : IReplacingCallback

{

///

/// This method is called by the Aspose.Words find and replace engine for each match.

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.MatchNode;

builder.MoveTo(currentNode);

bool bookmarkInserted = false;

foreach (BookmarkStart bookmarkStart in e.MatchNode.Document.GetChildNodes(NodeType.BookmarkStart, true))

{

if (bookmarkStart.Bookmark.Name == "APPENDIX")

{

bookmarkInserted = true;

builder.EndBookmark("APPENDIX");

}

}

if (!bookmarkInserted)

builder.StartBookmark("APPENDIX");

// 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 highlighting.

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

}

// 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.Stop;

}

}

///

/// Splits text of the specified run into two runs.

/// Inserts the new run just after the specified run.

///

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;

}

Best regards,

Hi Andrey,

Thank you very much.

It is works.

Is it possible to insert document instead of "New Content" ?

Thanks again.

Regards,

RK

Hello

Thanks for your request. Yes of course, it is possible using Aspose.Words. Please see the following link to learn how to insert insert a Document at a Bookmark.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-insert-a-document-into-another-document.html

Best regards,

Thank you.

Best regards,

RK