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 Aug 15, 2011 3:19 pm

Hi I am quit new to this product, all I am trying to do is add content from database to the word document after a page or paragraph.

Please see the current code


Document document = new Document();
document.LoadFromFile(@"..\..\..\Template_structure\Full_Junior_template.docx");
Paragraph paragraph = document.AddSection().AddParagraph();

paragraph.AppendText("Hello World");

document.SaveToFile("sample.docx", FileFormat.Docx,Response,HttpContentType.Attachment);

try
{


}

catch
{

}

Any help will be appriciatable.

Thank you.

ahamed
 
Posts: 15
Joined: Mon Aug 15, 2011 1:09 pm

Tue Aug 16, 2011 6:26 am

Hi.
Thanks for your inquiry.
If you want to add content into an existing word document after a page or paragraph, you can:

1. create a document object to load the original word document, as your code:
Code: Select all
Document document = new Document();
document.LoadFromFile(@"..\..\..\Template_structure\Full_Junior_template.docx");


2. if you need to add new content to a new section, please:
Code: Select all
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Hello World");

3. if you don't need to add a new section, you could use this code below to get the last section:
Code: Select all
Section section = document.LastSection;

4. if you want to add cotent to a new page, you need append a page separator first:
Code: Select all
Paragraph paragraph = section.AddParagraph();
paragraph.AppendBreak(Spire.Doc.Documents.BreakType.PageBreak);
paragraph.AppendText("Hello World");
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Tue Aug 16, 2011 8:45 am

Hi,

Thanks for the reply,I am comfortable with above code may be I did not clearly mentioned my reqirement.

I have a 13page word document I need to add some content in page 5 after a paragraph and page 9 after a sentence.

Any help will be appriciatable.

Thanks

ahamed
 
Posts: 15
Joined: Mon Aug 15, 2011 1:09 pm

Thu Aug 18, 2011 3:22 am

Hi,

I suggest you add some bookmarks into where you will add new content. And you can use BookmarksNavigator to move to the bookmark and append your new content:

Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;

namespace AddNewContent
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document(@"..\..\Summary_of_Science.doc");
            BookmarksNavigator nav = new BookmarksNavigator(doc);

            //append a new paragraph after bookmark Position1
            nav.MoveToBookmark("Position1");

            Paragraph p1 = new Paragraph(doc);
            TextRange t1 = p1.AppendText("I'm new paragraph.");
            t1.CharacterFormat.Bold = true;
            t1.CharacterFormat.TextColor = System.Drawing.Color.Red;
            nav.InsertParagraph(p1);

            //remove blank
            p1.OwnerTextBody.ChildObjects.Remove(p1.NextSibling);

            //insert a text befer bookmark Position2
            nav.MoveToBookmark("Position2", true, false);
            ITextRange t2 = nav.InsertText("I'm before Position2 ");
            t2.CharacterFormat.Bold = true;
            t2.CharacterFormat.TextColor = System.Drawing.Color.Red;

            //insert a text in bookmark Position3
            nav.MoveToBookmark("Position3");
            ITextRange t3 = nav.InsertText("I'm new content ");
            t3.CharacterFormat.Bold = true;
            t3.CharacterFormat.TextColor = System.Drawing.Color.Red;

            doc.SaveToFile("test.doc");
            System.Diagnostics.Process.Start("test.doc");
        }
    }
}

A full demo is attached.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc