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,
- 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
- 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,
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");
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,
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");
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,
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,
alright, I am going to play with it a bit, thanks for the help
Hi,