Inserting Text after bookmark in C#.net

Hi All,

I have added some bookmark in my word document template.

I want to Insert the text after bookmark

Kindly help me out

@pravinghadge,

Thanks for your inquiry. Please refer to the following articles.
Use DocumentBuilder to Insert Document Elements
Moving to a Bookmark

Please use the following code example to insert text after bookmark. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("bookmarkname", false, true);
builder.Write("Text after bookmark");

doc.Save(MyDir + "18.1.docx");
1 Like

Thanx for your reply. It works for me.

Our company is planning to buy the Aspose.word dll.

My final requirement is

  1. We have 10-15 RichTextbox (summernote textbox), here user can do formatting in textbox , insert image in it.
  2. I have Word Template with 10-15 bookmarks. After Export from the Website, the data of Richtextbox will get inserted after corresponding Bookmark
  3. User will make the changes in Exported Word document & again import it in Website
  4. After Import of data, it should replace into the corresponding RichTextbox.

Here above 3 points are covered by me & now working on 4 point

After Import of data, i am unable to find way to Copy Bookmark data into corresponding Textbox

Kindly help me out in final point

@pravinghadge,

Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents. We will then provide you more information about your query along with code example.

Hi,

Please find attached file.

We have attached Input file & expected output file in attachement zip file

Kindly give me the solution asap

@pravinghadge,

Thanks for sharing the documents. Following code example shows how to move the cursor to the parent node of bookmark and insert the content. Please read the articles shared in this thread. Hope this helps you.

Document doc = new Document(MyDir + "Input Word Template.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark CFHeadings = doc.Range.Bookmarks["CFHeadings"];

builder.MoveTo(CFHeadings.BookmarkStart.ParentNode);
builder.InsertParagraph();
builder.Font.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = "Arial";
builder.Font.Size = 9;
builder.Write("Mandatory Structure:");
builder.Font.Bold = false;
builder.Write("The front page of a Full Rating Report or Update should...");
               
doc.Save(MyDir + "output.docx");

Thanx for reply.

The Code which you have given is for Export of Data from RichTexbox’s to Word Template

But now my main concern is retrieve the “output.docx” data into RichTexboxe’s with the Same formatting of Word document.

@pravinghadge,

Thanks for your inquiry. You can save your document to RTF using Document.Save method and use RTF in RichTextBox. However, Aspose.Words does not provide API to work with RichTextBox. Moreover, please note that Aspose.Words mimics the behavior of MS Word.

doc.Save(MyDir + "output.rtf");

HI Manzoor,

Can you please provide the code for reading the data of Bookmarks from word

@pravinghadge,

Thanks for your inquiry. We suggest you please extract the content from bookmark and save the document to RTF. Please refer to the following article.

How to Extract Selected Content Between Nodes in a Document

Hi,

I am getting error at Common.ExtractContent

Also from code i have noticed that it is saving to word document.

I don’t want to save document.

i just have to extract the content of Bookmarks

@pravinghadge,

Thanks for your inquiry.

Could you please share in which format you want to extract the contents? The Common.ExtractContent extracts the nodes from document and returns ArrayList. You can save the extracted content to memory stream or String.

As per my understanding, you want to extract the bookmark’s content in the form of RTF. If this is the case, you can save the extracted content into RTF MemoryStream and use it in your application according to your requirement.

MemoryStream stream = new MemoryStream();

doc.Save(stream, SaveFormat.Rtf);

Hi,

I dont want to the Extract Bookmark’s content in the form of RTF.

Lets take an example , I have 2 bookmarks in the Word document.

[Name
pravin Ghadge
]
[Address
Mumbai, India
]

Here Name & address are to 2 bookmarks in the word document.

I just want to read the Bookmarkcollection of word document in the code.

So i will get the text of Name & Address bookmark

@pravinghadge,

Thanks for your inquiry. We suggest you please read about Aspose.Words’ document object model.

Please use the Bookmark.Text property to get the text of bookmark. If you want to remove some text from the bookmark’s text, you can using String.Replace method. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
                 
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    //E.g. Name and Address is the bookmark names
    //String.Repalce method can be used to remove text "Name" and "Address" from the bookmark's text
    if (bookmark.Name == "Name")
    {
        string name = bookmark.Text.Replace("Name", "").Trim();
    }
    else if (bookmark.Name == "Address")
    {
        string address = bookmark.Text.Replace("Address", "").Trim();
    }
}

Hi ,

Thanx for your reply and support
I have searched through aspose website, i got one solution which is very nearer to what i needed.

Link: Read Formatted Text from Bookmark to Database - #20 by paybills

@pravinghadge,

Thanks for your feedback. It is nice to hear from you that you have found the solution of your query. This solution extracts the content from bookmark and converts it to HTML using Node.ToString method.

Instead of saving document to stream, you can convert the extracted content to HTML using Node.ToString method. You may save the content to HTML using Document.Save method. Please check the articles shared in my old post about extracting contents.

Ok.
Means we can use Stream or String method to extract the contents

Thanx for clearing the doubts.

Can you please suggest which one is faster and reliable

@pravinghadge,

Thanks for your inquiry. You can use Document.Save and Node.ToString(SaveFormat.Html) methods according to your requirements. The performance depends on complexity and size of content.

Hi,

I am getting Empty string when using following code:

   Document doc = new Document(fname);

   System.IO.MemoryStream stream = new System.IO.MemoryStream();
   doc.Save(stream, SaveFormat.Rtf);

     System.IO.StreamReader reader = new System.IO.StreamReader(stream);
     string text1 = reader.ReadToEnd();

Here text1 coming empty. Can you please suggest where i am wrong

@pravinghadge,

Thanks for your inquiry. Please set the value of MemoryStream.Position to 0 as shown below to get the desired output.

Document doc = new Document(fname);

System.IO.MemoryStream stream = new System.IO.MemoryStream();
doc.Save(stream, SaveFormat.Rtf);
stream.Position = 0;
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
string text1 = reader.ReadToEnd();