Insert document into another document

Posted by: martin.bruce on 3 March 2020, 8:56 pm EST

  • Posted 3 March 2020, 8:56 pm EST

    Hello,

    I'm trying to generate a document where part of that document is another document.

    The reason for this is we have some static information placed in a common document. This information is used in a number of different documents that I'm generating.

    I want to insert this static document content (it contains among other
    things a table) into a specific location in the document I'm generating.

    I've managed to get some information in but not at the location I want and also I don't get all the information for some reason.

    This is what I do now:

    //Find range where I want the static document inserted
    PersistenRange pr; //This points to the text ("<Attacment">) in the document I'm generated.

    //load the static document
    GcWordDocument doc = new GcWordDocument();
    doc.Load(<stream>);

    //Select all of my static document content.
    var range = doc.Body.GetPersistentRange(doc.bBody.Start, doc.Body.End);

    //Insert static document content into the document I'm generating
    range.CopyTo(rr.ParentSection.GetRange(), InsertLocation.End);

    This gives me about 25% of the static document inserted into the document I'm generating. It also placed in the wrong location.

    So obviously I'm doing somthing worng here.

    Can you help me?



  • Replied 3 March 2020, 9:04 pm EST

    Ok, forgott to add the key now I get all the content. However I still have the issue of where to place the static data. I cant seem to get it in the correct place.
  • Replied 4 March 2020, 12:44 am EST

    Hi Martin,

    Can you share where exactly do you want to insert the static data? Do you want to insert the static content at the end of the document you are generating, before it, after some specific type of data in the dynamic data?
    This information will help us suggest you a solution accordingly.

    ~Ruchir
  • Replied 4 March 2020, 7:46 pm EST

    Hello Ruchir,

    I want to insert the static data at a place that differentiate depending on the document I'm creating. Currently I have a simple text that I look for and want to insert the static data where that text is. This text can be anywhere in the document.

    That isn't possible since I can't insert a table unless it is in a section.
  • Replied 5 March 2020, 8:33 am EST

    Hello Martin.

    Could you please provide code you used to insert your table?
    Every control (table, paragraph, run etc) have set of allowed parent types. For example, table cannot be inserted into paragraph or run as it is forbidden.
    If you provide your code, may be we will be able to fix it in a proper way.
    Also, text of exception you got may help us too.

    A.
  • Replied 9 March 2020, 11:59 pm EST

    Hello,

    My table is in att docukemt. I insert that document by doing this:

    var range = doc.Body.GetPersistentRange(doc.Body.Start, doc.Body.End);
    range.CopyTo(rr.ParentSection.GetRange(), InsertLocation.End);


    rr is a PersistentRange of a text I find in the main document.

    So the above works since I insert into the ParentSection. However it is not a very good solution since if my ParentSection contains any other text besides the text I look for the position of my inserted "table" will mess upp my document.

    I'm looking for a way to specify exactly the location where my "table" will be inserted.
  • Marked as Answer

    Replied 10 March 2020, 7:19 am EST

    Hi,

    Please check out this sample:

    https://www.grapecity.com/documents-api-word/demos/basics/find-replace/insert-at-found/code-cs

    It shows how to insert a document at a location before an arbitrary found text in another document. NOTE: this sample uses (and needs) the new v3.1 version of our packages. While this version has not been officially released yet, the release packages are already available on NuGet.org, and will be released in a couple of weeks. If you have a valid v3 GcWord license, you should be able to use your key with those packages too.

    Hope this helps,
    Dmitry.
  • Replied 10 March 2020, 9:17 pm EST

    Hello Dmitry,

    That will work perfectly.

    Thankyou!

    /Martin
  • Replied 11 March 2020, 1:03 am EST

    Great, thanks for the heads up. Marking this thread as answered then. Let us know if you have more questions though.

    Cheers,
    Dmitry.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels