Free Support Forum - aspose.com

Help with document insertion

Hi there,

I am looking for a solution to insert an existing document into another document
below is what I’ve found on your website, and its exactly what I am looking for.
http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document

There is a problem with the sample code. If the source document has a header and a footer, they’ll not be inserted into destination document with the sample code.
I can get header with following code, srcSection.HeadersFooters[HeaderFooterType.HeaderPrimary]; and then manually create a node and insert it into destination document, the header shows up in the destination document but it pushes the contents down to next page because the header was inserted in as contents; beyond that, if I have 2 pages in the source document, after I insert it into destination document with my approach, I have header on first page, but no header on second page. Is there a way to fix this issue?

thx

Hi Hui,


Thanks for your inquiry. To ensure a timely and accurate response please supply us with the following information.

  • What version of Aspose.Words for .NET are you using?
  • Please supply us with the code from your application that is causing the issue
  • Please supply us with the sample input source and destination Word documents that are causing the issue
  • Please supply us with the output document showing the undesired behavior
  • Please supply us with the target/expected document showing the desired behavior. You can create this document using Microsoft Word. We will investigate the structure as to how you would like your target document to be generated like

As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Many thanks,
  • Version of Aspose.Words for .NET is 11.2.0.0
  • I have attached the source code for you to investigate, this source code is a copy&paste from http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document
  • Sample source and destination document is under bin\Debug directory in attached zip file
  • output document is under bin\Debug directory in attached zip file
  • expected document showing desired behavior is the source document under bin\Debug

Demo program is written on VS2008, file is compressed by 7-Zip. The destination document appears to be empty, it contains only a bookmark namely 'MyBookmark', the source document is being inserted at this bookmark.

As you can see the 2-page source document has a header, but when I insert it into destination document, the header is lost.

Thanks

Hi,


Thanks for the additional information. In your case, you need to copy headers/footers form the first Section of source document to the first Section of destination document. Please see the following code snippet:

Document dstDoc = new
Document(@“C:\Temp\destination.docx”);

Document srcDoc = new Document(@"C:\Temp\source.docx");

Bookmark bookmark = dstDoc.Range.Bookmarks["MyBookmark"];

InsertDocument(bookmark.BookmarkStart.ParentNode, srcDoc);

Section srcSection = srcDoc.FirstSection;

Section dstSection = dstDoc.FirstSection;

dstSection.HeadersFooters.Clear();

foreach (HeaderFooter headerFooter in srcSection.HeadersFooters)

{

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);

Node dstNode = importer.ImportNode(headerFooter, true);

dstSection.HeadersFooters.Add(dstNode);

}

dstDoc.Save(@"C:\Temp\myout.docx");


I hope, this helps.

Best regards,

Hi there,

thanks for the reply, however, this solution does not fix my problem, I’ll clarify this a bit. I’ve attached 2 docx files for your reference.
In the destination file, I now have 3 pages, I have contents on first page and third page, second page appears to be blank, it contains only a book mark, my source file will be inserted at this bookmark; in the output file, the header shows on all pages, what I really need is the header should be only showing on second and third pages in output file, the first and fourth pages(3rd page in destination docx) should remain the same(without header)

I uploaded another docx file showing what the output should look like. thanks

Hi,


Thanks for the additional information. Please use the following code to achieve what you’re looking for:

Document dstDoc = new
Document(@“C:\Temp\destination.docx”);

Document srcDoc = new Document(@"C:\Temp\source.docx");

Bookmark bookmark = dstDoc.Range.Bookmarks["MyBookmark"];

// Insert a Section before Bookmark

Paragraph prevPara = new Paragraph(dstDoc);

prevPara.Runs.Add(new Run(dstDoc, ""));

Paragraph bmPara = bookmark.BookmarkStart.ParentNode as Paragraph;

bmPara.ParentNode.InsertBefore(prevPara, bmPara);

DocumentBuilder dstBuilder = new DocumentBuilder(dstDoc);

dstBuilder.MoveTo(prevPara);

dstBuilder.InsertBreak(BreakType.SectionBreakNewPage);

// Insert a Section After Bookmark

dstBuilder.MoveTo(bmPara);

dstBuilder.InsertBreak(BreakType.SectionBreakNewPage);

InsertDocument(bookmark.BookmarkStart.ParentNode, srcDoc);

Section srcSection = srcDoc.FirstSection;

Section dstSection = dstDoc.Sections[1];

dstSection.HeadersFooters.Clear();

foreach (HeaderFooter headerFooter in srcSection.HeadersFooters)

{

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);

Node dstNode = importer.ImportNode(headerFooter, true);

dstSection.HeadersFooters.Add(dstNode);

}

dstDoc.Sections[2].HeadersFooters.LinkToPrevious(false);

dstDoc.Save(@"C:\Temp\myout.docx");


I hope, this helps.

Best regards,

It works for me, but the output file contains a blank page right before last page. how can I void it?

also could you explain more about your code? my destination document is actually more complicate than this, I am afraid the output will be messed up if I use hardcode sections[1] and sections[2]

thx

Hi,


Thanks for your inquiry. I have removed a couple of empty Paragraphs and the ‘Page Break’ following the Bookmark Paragraph from your ‘destination.docx’ and attached the modified version of this document here for your reference. When using the code from my previous post, you no longer need to insert Page Breaks after the Bookmark. I hope, this helps.

Best regards,

Hi,

Could you explain the idea behind? When I integrate your code into my code, the output doc is messed up, I attached another solution for you.
Actually in our product, the destination document contains a lot of pages with a hyperlink on each page, we replace those hyperlinks with real documents, there could be many pages before where I want the source document to be inserted, there also could be many pages after. As you can see in the new documents I provided under bin\Debug, the destination document has 2 pages with hyperlinks on them, I insert report.docx at the first location( to simply the demo I still use bookmarks). then insert source.doc at the second location, the output is not what I want, its even worse in really scenario

thx

Hi,


Thanks for your request. The main idea of this code is that we insert a couple of section breaks (one before the bookmark and one after the bookmark - for example, this splits one big section into three sections) at the place where we need to insert the source document and insert sections from the source document between newly created sections in the destination document.

Best regards,

alright, I am going to play with it a bit, thanks for the help

Hi,


Sure, please let us know any time you have any further queries. We are always glad to help you.

Best regards,