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.

Wed Aug 03, 2016 1:06 pm

I am using this code to try and copy the contents of a document to another document. It copies the body. However, I do not get any headers or footers.

foreach (Section sec in pDocument.Sections)
{
foreach (DocumentObject obj in sec.Body.ChildObjects)
{
pMaindocument.Sections[0].Body.ChildObjects.Add(obj.Clone());
}
}

druchti
 
Posts: 8
Joined: Tue Aug 02, 2016 12:47 pm

Thu Aug 04, 2016 3:28 am

Dear druchti,

Thanks for your inquiry.
The header and footer are not included in Section.Body, please clone the entire section into another document. Here is sample code for your reference.
Code: Select all
            Document sourceDoc = new Document(FilePath +"source.docx");
            Document destinationDoc = new Document(FilePath + "target.docx");
            foreach (Section sec in sourceDoc.Sections)
            {
                destinationDoc.Sections.Add(sec.Clone());
            }           
            destinationDoc.SaveToFile("8257target.docx");


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu Aug 04, 2016 5:27 pm

That took care of the headers and footers. However, it created two issues.

The first page of my main document is a blank page.

The 4th page of document B, after bullet 20 has a picture. In the original document, the picture is below bullet 20. In the main document, the picture gets pushed to the next page.

I have attached Document A, Document B and the new document Main

Here is the code I am using.

Document oMainDoc = new Document();
Section oSection = oMainDoc.AddSection();

Document oDocumentC = new Document("Document C.docx");
oDocumentC.Replace("#unit", "12345", true, true);
oDocumentC.Replace("#wo", "98765", true, true);
AddDocumentToMainDocument(oDocumentC, oMainDoc);

Document oDocumentB = new Document("Document B.docx");
oDocumentB.Replace("#unit", "23456", true, true);
oDocumentB.Replace("#wo", "87654", true, true);
AddDocumentToMainDocument(oDocumentB, oMainDoc);

oMainDoc.SaveToFile("Main.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Main.docx");
oMainDoc.SaveToFile("Main.pdf", FileFormat.PDF);

private void AddDocumentToMainDocument(Document pDocument, Document pMaindocument)
{
foreach (Section sec in pDocument.Sections)
{
pMaindocument.Sections.Add(sec.Clone());
}
}

Thanks for the help

druchti
 
Posts: 8
Joined: Tue Aug 02, 2016 12:47 pm

Fri Aug 05, 2016 2:23 am

Dear druchti,

Thanks for your feedback.
About the blank page issue, please don't add one section when create a new file(Section oSection = oMainDoc.AddSection()).
About the second issue, I have noticed and posted it to our Dev team, we will let you know when it is fixed.
Sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu Sep 01, 2016 1:15 am

Dear druchti,

Thanks for your waiting.
Here is a hotfix which resolves the issue you mentioned, welcome to test it.
http://www.e-iceblue.com/downloads/Temp ... 5.7.84.zip
If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Sat Sep 03, 2016 3:38 pm

Hi Betsy,

I tested the fix. It worked.

Thanks

druchti
 
Posts: 8
Joined: Tue Aug 02, 2016 12:47 pm

Mon Sep 05, 2016 1:24 am

Dear druchti,

Thanks for your feedback.
Welcome to write to us again for further question.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc