We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Get Font from bookmark

How do i get the Font (or all the formatering) from "under" a bookmark?
I have a precreated document that contains several bookmarks at different locations.
I want to get the formatting from each bookmark but i can figure out how to do it.
There are no WTextRange elements in the WParagraph that i get from bookmarkNavigator.GetBookmarkContent().
There is only a WParagraph as the only childElement.

<Code>
WordDocument document = new WordDocument();
document.OpenReadOnly("Bookmarks.docx", FormatType.Docx);
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark(bookmark);
var textBodyPart = bookmarkNavigator.GetBookmarkContent();
foreach (var childEntity in textBodyPart.BodyItems)
{
    if (childEntity is WParagraph)
    {
        var paragraph = childEntity as WParagraph;
        IWTextRange firsttext = paragraph.AppendText(text);

        firsttext.CharacterFormat.FontSize = 14;

        firsttext.CharacterFormat.Bold = true;

        firsttext.CharacterFormat.TextColor = Color.Green;
    }
}
</code>

So the question is...how do i get the font from where the bookmark exists?

6 Replies

SR Suganya Rathinam Syncfusion Team April 6, 2016 07:35 AM UTC

Hi Anders,

Thank you for contacting Syncfusion support.

The GetBookmarkContent API of DocIO gets the contents of the bookmark which will be the TextBodyParts such as paragraphs and tables and the paragraph inturn contains text ranges. If your requirement is to modify the bookmark contents then you can use ReplaceBookmarkContent API and if your requirement is to insert new content to the existing bookmark you can use InsertText API of BookmarkNavigator class. Please find the modified code snippets for your reference.


WordDocument document = new WordDocument();

document.OpenReadOnly(FileName, FormatType.Docx);

BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);

bookmarkNavigator.MoveToBookmark("Bookmark");

//GetBookmarkContent will get the cloned copy of the bookmark contents and the changes made to the retrieved contents

//will not be reflected in the bookmark contents.

var textBodyPart = bookmarkNavigator.GetBookmarkContent();

foreach (var childEntity in textBodyPart.BodyItems)

{

    if (childEntity is WParagraph)

    {

        var paragraph = childEntity as WParagraph;

        IWTextRange firsttext = paragraph.AppendText("New text");


        firsttext.CharacterFormat.FontSize = 14;


        firsttext.CharacterFormat.Bold = true;


        firsttext.CharacterFormat.TextColor = Color.Green;

    }

}

//Replace the modified bookmark content

bookmarkNavigator.ReplaceBookmarkContent(textBodyPart);

document.Save(FileName, FormatType.Docx);



Please find the following UG documentation link on working with bookmarks for your reference.

http://help.syncfusion.com/file-formats/docio/working-with-bookmarks

If we misunderstood any of your requirement then kindly elaborate the actual requirement with detailed description. Thereby we will analyse further on the mentioned case and will provided you appropriate solution.

Regards,
Suganya



AW Anders Wester April 7, 2016 09:47 AM UTC

Hi. Thanks for the quick reply.
I have been using the same code that you posted. And that works just fine.
But what happens if i want to Append text and not replace the whole content?
I use this code:

            WordDocument document = new WordDocument();
            document.OpenReadOnly("Bookmarks.docx", FormatType.Docx);
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
            bookmarkNavigator.MoveToBookmark(bookmark);
            bookmarkNavigator.InsertText(text,true);
            document.Save("Result.docx", FormatType.Docx);

But when i use that code my Font gets changes from [whatever] to Calibri and the fontsize get changed from X to 11.
That is why i wanted to know how to get the Font and Fontsize from the under the bookmark

For clarification: I have placed a bookmark in a table cell that have the Font 'Bookman Old' and the fornsize 8. There is, in this case, no other text in the table that tablecell so i should be able to use the ReplaceBookmarkContent method. But i want to know if there is a way to get the font and other styling from the cell that i have my bookmark in.



SR Suganya Rathinam Syncfusion Team April 8, 2016 05:47 AM UTC

Hi Anders,

The GetBookmarkContent API retrieves the bookmark contents as cloned copy and the changes made to the retrieved contents will not be reflected in the bookmark contents. If your requirement is to modify the contents of the bookmark then you should replace the bookmark contents with the modified TextBodyPart. You can also use InsertText API of BookmarkNavigator class to add new text to the bookmark.

We have prepared a sample to get the paragraph from the table cell and modify the formatting of the text in the bookmarks and attached the same. Please find the sample from the following link and let us know if it helps.

Sample Link:
http://www.syncfusion.com/downloads/support/forum/123628/ze/Sample39603649.zip

Regards,
Suganya




AW Anders Wester April 8, 2016 06:32 AM UTC

Hi again.
I think you are misunderstanding me.
The sample you sent works as expected. But the issue i have is when there is no WTextRange entities to work with. For example if there is no text in the tablecell where the bookmark is.
I attached your example, the only thing i changed was the input document. Now when you run the sample there is no WtextRange element in the cell where the bookmark is. And therefor i can't get the formatting from that cell.
And as there is no WTextRange to modify i tried replacing the ReplaceBookmarkContent method with the InsertText. When i do that the cell formatting is reset from what it was to Calibi with a fontsize of 11.

So, again, what im trying to achieve is to get the formatering from a tablecell with no text in it. In Word i can see that there is formatting applied, but when i use InsertText("abc", true) that formatting is reset.

I hope i explained it better this time.

As context why im asking the question. Im trying to fill a reporttemplate. So there is no text whatsoever in the template. The template consists of a big empty table that we have formatted and placed several bookmarks in. 


Attachment: SyncFusion_Sample_86083783.zip


AW Anders Wester April 8, 2016 10:04 AM UTC

*update*
It got it working by using the ReplaceBookmarkContent method on a empty tablecell...which was probably what you where telling me to do all along, sorry about that!

But just for academic reason, not sure what i want to use it for yet, is it possible to get the Font used in a single empty tablecell? I can with the above method replace the content of the cell and retain the formatting, but is it possible to just retrieve the Font without changing the content?

regards


SR Suganya Rathinam Syncfusion Team April 11, 2016 07:10 AM UTC

Hi Anders,

The bookmark contents are retrieved as TextBodyPart which includes paragraphs, tables and the formatting of the entire bookmark contents cannot be retrieved as single formatting. You can use the InsertText API of BookmarkNavigator class to preserve the same formatting for the newly inserted text. Please find the following API documentation links for preserving the existing formatting of the newly inserted text to the bookmark contents.

InsertText:
http://help.syncfusion.com/cr/cref_files/file-formats/docio/Syncfusion.DocIO.Base~Syncfusion.DocIO.DLS.BookmarksNavigator~InsertText(String,Boolean).html

ReplaceBookmarkContent:
http://help.syncfusion.com/cr/cref_files/file-formats/docio/Syncfusion.DocIO.Base~Syncfusion.DocIO.DLS.BookmarksNavigator~ReplaceBookmarkContent(String,Boolean).html

The InsertText API will return a new text range instance to which you can define new font formattings. If your requirement to modify the formatting of the newly inserted text other than the existing format or default format,.then you can define separate formattings to the newly inserted text. Please find the following code snippets for your reference.


IWTextRange text = bookmarkNavigator.InsertText("New text");

text.CharacterFormat.TextColor = Color.Green;


If your requirement is to modify the existing formatting of the bookmark content, then kindly use the sample which we have provided on 4th April, 2016.

Mail merge functionality of DocIO:
From the given details we suspect that you are replacing the bookmarks in the report template with multiple data using the ReplaceBookmarkContent API or InsertText API. Mail merge functionality of DocIO helps to merge data from data sources to the template Word documents which will be the optimal solution to achieve your requirement instead of bookmark replacement. Kindly refer the following UG documentation link and online sample link and use this functionality if it suits your requirement.

UG Documentation:
http://help.syncfusion.com/file-formats/docio/working-with-mailmerge
Online Sample:
http://asp.syncfusion.com/demos/web/docio/employeereport.aspx

Regards,
Suganya


Loader.
Live Chat Icon For mobile
Up arrow icon