Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon May 11, 2020 6:08 am

hi,
i am adding the book mark in word document page 2 below byte to stream bind same content code.
but it's bind in last page of word document. how to bind the exact page in spire doc using .NET

Section section = document.Sections[0];
section.Paragraphs[0].AppendBookmarkStart("NeighborhoodDoc");
byte[] byteValue = bytefile;

if byteValue != null)
{
byte[] theData = imageBytes;
byte[] dataIn = new byte[(int)theData.Length];
using (Stream ms = new MemoryStream(dataIn, true))
{
ms.Write(theData, 0, theData.Length);
section.Document.InsertTextFromStream(ms, FileFormat.Auto);

}
}


Thanks
Tamilslevan S

Tamil6593
 
Posts: 3
Joined: Mon May 11, 2020 3:54 am

Mon May 11, 2020 7:16 am

Hello,

Thanks for your inquiry.
Do you want to insert the bookmark at a special location of the document, and then add content to the bookmark? If so, please refer to the following code.
Code: Select all
    Document document = new Document();
    document.LoadFromFile("input.docx");

    Section section1 = document.Sections[0];
    Paragraph para = section1.Paragraphs[0];
    //Add bookmark
    para.AppendBookmarkStart("bookmark");
    para.AppendText("bookmark");
    para.AppendBookmarkEnd("bookmark");

    BookmarksNavigator bn = new BookmarksNavigator(document);
    //Find bookmark by name
    bn.MoveToBookmark("bookmark", true, true);

    //The content to be inserted
    Stream stream = new FileStream("InsertContent.docx", FileMode.Open);
    Document insertContent = new Document();
    insertContent.LoadFromStream(stream, FileFormat.Auto);

    //Create a TextBodyPart instance and add the content to it
    TextBodyPart part = new TextBodyPart(document);
    foreach (Section section2 in insertContent.Sections)
    {
        foreach (Paragraph paragraph in section2.Paragraphs)
        {
            part.BodyItems.Add(paragraph.Clone());
        }
    }
    //Replace bookmark content
    bn.ReplaceBookmarkContent(part);
    document.SaveToFile("output.docx", FileFormat.Docx);

If this is not what you want, to help us better understand your requirement, please share your input file as well as your expected Word file with us. You could upload them here or send them to us(support@e-iceblue.com) via email.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon May 11, 2020 12:18 pm

hi,
Thanks for your reply.
i am using above code but i get the exception in this part bn.ReplaceBookmarkContent(part).
System.NullReferenceException: 'Object reference not set to an instance of an object.'
how can i fix it.

Thanks
Tamilselvan S

Tamil6593
 
Posts: 3
Joined: Mon May 11, 2020 3:54 am

Tue May 12, 2020 1:39 am

Hello,

Thanks for your feedback.
To help us further investigate your issue, please share your input files or a runnable project that could reproduce your issue. Meanwhile, in order to help us better understand the effect you want to achieve, please provide your desired output as well. You could upload them here or send them to us(support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc