Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks you for additional information.
1. This occurs because original document contains multiple sections. You can try using the following code to solve this problem.
//Open original document
Document originaldoc = new Document(@"Test056\in.doc");
//Create new document
Document newdoc = new Document();
DocumentBuilder builder = new DocumentBuilder(newdoc);
NodeImporter importer = new NodeImporter(originaldoc, newdoc, ImportFormatMode.KeepSourceFormatting);
foreach (Section originalSection in originaldoc.Sections)
{
//reate section in the new document
Section newSection = null;
if (!originalSection.Equals(originaldoc.FirstSection))
{
//Import SectionStart
builder.MoveToDocumentEnd();
switch (originalSection.PageSetup.SectionStart)
{
case SectionStart.Continuous:
builder.InsertBreak(BreakType.SectionBreakContinuous);
break;
case SectionStart.EvenPage:
builder.InsertBreak(BreakType.SectionBreakEvenPage);
break;
case SectionStart.NewColumn:
builder.InsertBreak(BreakType.SectionBreakNewColumn);
break;
case SectionStart.NewPage:
builder.InsertBreak(BreakType.SectionBreakNewPage);
break;
case SectionStart.OddPage:
builder.InsertBreak(BreakType.SectionBreakOddPage);
break;
}
newSection = builder.CurrentSection;
}
else
{
newSection = newdoc.FirstSection;
}
//Remove Headers/Footers from new section
newSection.ClearHeadersFooters();
//import headers and footers
foreach (HeaderFooter hf in originalSection.HeadersFooters)
{
newSection.HeadersFooters.Add(importer.ImportNode(hf, true));
}
newSection.PageSetup.DifferentFirstPageHeaderFooter = originalSection.PageSetup.DifferentFirstPageHeaderFooter;
}
//Save new Document
newdoc.Save(@"Test056\out.doc");
Also you can clone original document. See the following code.
//Open original document
Document originaldoc = new Document(@"Test056\in.doc");
//Create new document
Document newdoc = (Document)originaldoc.Clone(true);
//Crear content
foreach (Section newSection in newdoc.Sections)
{
newSection.Body.ChildNodes.Clear();
//Body can't be absolutely empty
Paragraph par = new Paragraph(newdoc);
newSection.Body.AppendChild(par);
}
//Save new Document
newdoc.Save(@"Test056\out.doc");
2. TOC items have “Hyperlink” style. So you can modify this style. See the following code.
newdoc.Styles[StyleIdentifier.Hyperlink].Font.Name = originaldoc.Styles[StyleIdentifier.Hyperlink].Font.Name;
newdoc.Styles[StyleIdentifier.Hyperlink].Font.Size = originaldoc.Styles[StyleIdentifier.Hyperlink].Font.Size;
newdoc.Styles[StyleIdentifier.Hyperlink].Font.Color = originaldoc.Styles[StyleIdentifier.Hyperlink].Font.Color;
//etc
I hope this could help you.
Best regards.