Free Support Forum - aspose.com

Working with Bookmarks in Aspose

I am trying to take the RICH TEXT (formatted, i.e., bold in some place, different font colors) from a bookmark in an existing document, and paste that rich text into a new document. So my starting code looks like this:

Aspose.Words.Document sourceDocument = new Aspose.Words.Document(fileName);
Bookmark bookmark = sourceDocument.Range.Bookmarks[bookmarkName];
if (bookmark != null)
{
// code goes here to create new document
// and paste rich text from bookmark
}

My question is what would be the code that goes inside the "if" statement. By looking at the object model & some of the basic examples provided, I haven't found a way to do this so far. The closest I've found is the code below, but this is for entering new text, and all of which is formatted the same way for the whole block of text. Is there a way to do what I am trying to accomplish above? Thanks!!

Document doc = new Document("MyDocument.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//Specify font formatting

builder.Font.Size = 16;

builder.Font.Bold = true;

builder.Font.Color = System.Drawing.Color.Blue;

builder.Font.Name = "Arial";

builder.Underline = Underline.Dash;

builder.Write("A single string of text.");

Regards,

Jafar

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks fro your inquiry. I think that you can try using the following code.

Document doc = new Document(@"Test025\in.doc");

Bookmark book = doc.Range.Bookmarks["myBookamrk"];

if (book != null)

{

//Create newdocument

Document newDoc = new Document();

//Get node that containd BookmarkStart

Node currentNode = book.BookmarkStart.ParentNode;

//Get node that containd BookmarkEnd

Node endNode = book.BookmarkEnd.ParentNode;

//Import nodes between BookmarkStart and BookmarkEnd

while (currentNode != endNode)

{

Node newNode = newDoc.ImportNode(currentNode,true, ImportFormatMode.KeepSourceFormatting);

newDoc.FirstSection.Body.AppendChild(newNode);

currentNode = currentNode.NextSibling;

}

//Save document

newDoc.Save(@"Test025\out.doc");

}

I hope this could help.

Best regards.

Thanks for the reply/suggestion; this makes sense looking at it, and seems like it should work, but it doesn't. When it hits the "while" loop, it simply fails on the initial condition check and never goes into the loop:

while (currentNode != endNode)
{
}

Any ideas why? I know for a fact that the bookmark exists, and that I can view its text in debug mode. Please advise.

Thanks again!

-Jafar

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

This code will work if BookmarkStart and BookmarkEnd have different parent node. You can check this condition. For example see the following code.

Document doc = new Document(@"Test075\in.doc");

Bookmark book = doc.Range.Bookmarks["myBookmark"];

if (book != null)

{

//Create newdocument

Document newDoc = new Document();

//Get node that containd BookmarkStart

Node currentNode = book.BookmarkStart.ParentNode;

//Get node that containd BookmarkEnd

Node endNode = book.BookmarkEnd.ParentNode;

//If bookmarkStart and bookmarkEnd are children of different nodes

//Then import nodes between parent nodes of bookmark start and bookmark end

if (currentNode != endNode)

{

//Import nodes between BookmarkStart and BookmarkEnd Parent nodes

while (currentNode != endNode)

{

Node newNode = newDoc.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);

newDoc.FirstSection.Body.AppendChild(newNode);

currentNode = currentNode.NextSibling;

}

}

//Else import nodes between bookmark start and bookmark end

else

{

currentNode = book.BookmarkStart.NextSibling;

//Import nodes between BookmarkStart and BookmarkEnd

while (currentNode != book.BookmarkEnd)

{

Node newNode = newDoc.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);

newDoc.FirstSection.Body.FirstParagraph.AppendChild(newNode);

currentNode = currentNode.NextSibling;

}

}

//Save document

newDoc.Save(@"Test075\out.doc");

}

I hope this could help you.

Best regards.

That did the trick. Thanks again for all your help here!!

-Jafar

I used the code you suggested, and it still works only partially. What happens is that if the bookmark contains multiple lines of text, then the last line is not copied to the target document. When I debug, I see why this is happening, but can't find a way to fix it.

Below is my code (almost exactly like the code you suggested); can you take a look at see why this doesn't work:

// This method is used to export a bookmark to a document.
private void ExportBookmarkToDocument(Bookmark bookmark, ref Aspose.Words.Document targetDocument)
{
// Get node that containd BookmarkStart and BookmarkEnd
Node currentNode = bookmark.BookmarkStart.ParentNode;
Node endNode = bookmark.BookmarkEnd.ParentNode;

// If bookmarkStart and bookmarkEnd are children of different nodes
// Then import nodes between parent nodes of bookmark start and bookmark end
if (currentNode != endNode)
{
//Import nodes between BookmarkStart and BookmarkEnd Parent nodes
while (currentNode != endNode)
{
Node newNode = targetDocument.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);targetDocument.FirstSection.Body.AppendChild(newNode);
currentNode = currentNode.NextSibling;
}
}

// Else import nodes between bookmark start and bookmark end
else
{
currentNode = bookmark.BookmarkStart.NextSibling;

//Import nodes between BookmarkStart and BookmarkEnd
while (currentNode != bookmark.BookmarkEnd)
{
Node newNode = targetDocument.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);
targetDocument.FirstSection.Body.FirstParagraph.AppendChild(newNode);
currentNode = currentNode.NextSibling;
}
}
}

Thanks in advance!

-Jafar

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Sorry I missed this case. This occurs because endNode is ignored. I modified my code. Please see the following code snippet:

Document doc = new Document(@"Test075\in.doc");

Bookmark book = doc.Range.Bookmarks["myBookmark"];

if (book != null)

{

//Create newdocument

Document newDoc = new Document();

//Get node that containd BookmarkStart

Node currentNode = book.BookmarkStart.ParentNode;

//Get node that containd BookmarkEnd

Node endNode = book.BookmarkEnd.ParentNode;

//If bookmarkStart and bookmarkEnd are children of different nodes

//Then import nodes between parent nodes of bookmark start and bookmark end

if (currentNode != endNode)

{

//Import nodes between BookmarkStart and BookmarkEnd Parent nodes

while (currentNode != endNode)

{

Node newNode = newDoc.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);

newDoc.FirstSection.Body.AppendChild(newNode);

currentNode = currentNode.NextSibling;

}

newDoc.FirstSection.Body.AppendChild(newDoc.ImportNode(endNode, true, ImportFormatMode.KeepSourceFormatting));

}

//Else import nodes between bookmark start and bookmark end

else

{

currentNode = book.BookmarkStart.NextSibling;

//Import nodes between BookmarkStart and BookmarkEnd

while (currentNode != book.BookmarkEnd)

{

Node newNode = newDoc.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);

newDoc.FirstSection.Body.FirstParagraph.AppendChild(newNode);

currentNode = currentNode.NextSibling;

}

}

//Save document

newDoc.Save(@"Test075\out.doc");

}

I hope this could help you.

Best regards.

That did the trick for the last line in the bookmark, thanks!

A couple of somewhat related questions:
1. If the bookmark from the source document is the first bit of text on some page other than the first, what happens is that the form feed (seen as "\f" in the Range.Text property) gets copied when the bookmark is copied from the source document to the new document. Is there any way to remove this in the new document?

2. If the bookmark has a comment associated to it, how can I remove that comment in the new document?

Thanks again!

-Jafar

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request.

1. “\f” indicates section break. So this paragraph is the last paragraph in the current section or in the document.

2. You can remove all comments from source document before copying content.

Document doc = new Document("in.doc");

NodeCollection comments = doc.GetChildNodes(NodeType.Comment, true);

comments.Clear();

Best regards.

The code for the comments looks be working fine. Though a quick question about section breaks: is there any way to remove those using the Aspose API? If there's isn't an elegant way to do this, can this be done by a simple find & replace (for "\f") somehow?

Thanks again for your continuous help here!!

-Jafar

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. No, you can’t replace “\f” character in the document. But you can remove section breaks using the following code.

//Open document

Document doc = new Document(@"Test150\in.doc");

//Append content of all section to the first section

foreach (Section sect in doc.Sections)

{

if (!sect.Equals(doc.FirstSection))

{

doc.FirstSection.AppendContent(sect);

}

}

//Remove all sections but first

while (!doc.LastSection.Equals(doc.FirstSection))

{

doc.LastSection.Remove();

}

//Save document

doc.Save(@"Test150\out.doc");

I hope this helps.

Best regards.

I tried doing this, and what I saw as I was in debug-mode was that there is only one section, so nothing was appended/removed. It really looks like your proposed solution should do the trick, but there seems to be something missing in this puzzle. The only thing I could say is that the physical document (by visual inspection) does translate that "\f" into a page-break/new section. Is there something else you can think of there?

Thanks!

-Jafar

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. “\f” indicates end of section or end of document. If you need to convert your document to text and remove “\f” then you can use the following code:

string text = doc.ToTxt().Remove(“\f”, “”);

Best regards.