Hi,
if somebody know how to split document by bookmarks
Document doc = new Document();
doc.LoadFromFile(@"F:\testing\doc form\original document\10993.docx");
Document newWord = new Document();
Section section = newWord.AddSection();
int index = 0;
for (int a = 0; a < doc.Sections.Count; a++)
{
Section sec = doc.Sections[a];
for (int c = 0; c < sec.Body.ChildObjects.Count; c++)
{
DocumentObject obj = sec.Body.ChildObjects[c];
if (obj is Paragraph)
{
Paragraph para = obj as Paragraph;
section.Body.ChildObjects.Add(para.Clone());
for (int d = 0; d < para.ChildObjects.Count; d++)
{
DocumentObject parobj = para.ChildObjects[d];
if (parobj is BookmarkEnd && !(parobj as BookmarkEnd).Name.Contains("_GoBack"))
{
int i = para.ChildObjects.IndexOf(parobj);
newWord.SaveToFile(String.Format("10993out-{0}.docx", index), FileFormat.Docx);
index++;
newWord = new Document();
section = newWord.AddSection();
section.Body.ChildObjects.Add(para.Clone());
if (section.Paragraphs[0].ChildObjects.Count == 0)
{
section.Body.ChildObjects.RemoveAt(0);
}
else
{
while (i >= 0)
{
section.Paragraphs[0].ChildObjects.RemoveAt(i);
i--;
}
}
}
}
}
if (obj is Table)
{
section.Body.ChildObjects.Add(obj.Clone());
}
}
}
newWord.SaveToFile(String.Format("10993out-{0}.docx", index), FileFormat.Docx);
...
Document newWord = new Document();
Section section = newWord.AddSection();
List<DocumentObject> HeaderObj = new List<DocumentObject>();
List<DocumentObject> FooterObj = new List<DocumentObject>();
foreach (Section sec in doc.Sections)
{
if (!sec.HeadersFooters.IsEmpty)
{
foreach(DocumentObject objH in sec.HeadersFooters.Header.ChildObjects)
{
HeaderObj.Add(objH.Clone());
}
foreach (DocumentObject objH in sec.HeadersFooters.Footer.ChildObjects)
{
FooterObj.Add(objH.Clone());
}
break;
}
}
foreach (DocumentObject Ho in HeaderObj)
{
section.HeadersFooters.Header.ChildObjects.Add(Ho.Clone());
}
foreach (DocumentObject Fo in FooterObj)
{
section.HeadersFooters.Header.ChildObjects.Add(Fo.Clone());
}
int index = 0;
...
newWord = new Document();
section = newWord.AddSection();
foreach (DocumentObject Ho in HeaderObj)
{
section.HeadersFooters.Header.ChildObjects.Add(Ho.Clone());
}
foreach (DocumentObject Fo in FooterObj)
{
section.HeadersFooters.Footer.ChildObjects.Add(Fo.Clone());
}
section.Body.ChildObjects.Add(para.Clone());
...
public void SplitDoc_SpireDoc()
{
Document newWord = new Document();
Section section = newWord.AddSection();
List<DocumentObject> HeaderObj = new List<DocumentObject>();
List<DocumentObject> FooterObj = new List<DocumentObject>();
//Select the DifferentFirstPage option
section.PageSetup.DifferentFirstPageHeaderFooter = true;
foreach (Section sec in this.SPD.Sections)
{
if (!sec.HeadersFooters.IsEmpty)
{
foreach (DocumentObject objH in sec.HeadersFooters.FirstPageHeader.ChildObjects)
HeaderObj.Add(objH.Clone());
foreach (DocumentObject objH in sec.HeadersFooters.FirstPageFooter.ChildObjects)
FooterObj.Add(objH.Clone());
break;
}
}
foreach (DocumentObject Ho in HeaderObj)
section.HeadersFooters.FirstPageHeader.ChildObjects.Add(Ho.Clone());
foreach (DocumentObject Fo in FooterObj)
section.HeadersFooters.FirstPageFooter.ChildObjects.Add(Fo.Clone());
int index = 0;
string DocFile = "";
foreach (Section sec in this.SPD.Sections)
{
foreach (DocumentObject obj in sec.Body.ChildObjects)
{
if (obj is Paragraph)
{
Paragraph para = obj as Paragraph;
section.Body.ChildObjects.Add(para.Clone());
for (int d = 0; d < para.ChildObjects.Count; d++)
{
DocumentObject parobj = para.ChildObjects[d];
if (parobj is BookmarkEnd && (parobj as BookmarkEnd).Name.Contains("uLSF_"))
{
DocFile = @"d:\\" + ((BookmarkEnd)parobj).Name + ".pdf";
int i = para.ChildObjects.IndexOf(parobj);
newWord.SaveToFile(DocFile, FileFormat.PDF);
index++;
newWord = new Document();
section = newWord.AddSection();
section.PageSetup.DifferentFirstPageHeaderFooter = true;
foreach (DocumentObject Ho in HeaderObj)
section.HeadersFooters.FirstPageHeader.ChildObjects.Add(Ho.Clone());
foreach (DocumentObject Fo in FooterObj)
section.HeadersFooters.FirstPageFooter.ChildObjects.Add(Fo.Clone());
section.Body.ChildObjects.Add(para.Clone());
if (section.Paragraphs[0].ChildObjects.Count == 0) section.Body.ChildObjects.RemoveAt(0);
else
{
while (i >= 0)
{
section.Paragraphs[0].ChildObjects.RemoveAt(i);
i--;
}
}
}
}
}
if (obj is Table) section.Body.ChildObjects.Add(obj.Clone());
}
}
}
...
section.Body.ChildObjects.Add(para.Clone());
if (section.Paragraphs[0].ChildObjects.Count == 0) section.Body.ChildObjects.RemoveAt(0);
else
{
while (i >= 0)
{
section.Paragraphs[0].ChildObjects.RemoveAt(i);
i--;
}
//Remove the break
for (int c = 0; c < section.Paragraphs[0].ChildObjects.Count; c++)
{
if (section.Paragraphs[0].ChildObjects[c] is Break)
{
section.Paragraphs[0].ChildObjects.Remove(section.Paragraphs[0].ChildObjects[c]);
}
}
}
...
public void SplitDoc_SpireDoc()
{
Document newWord = new Document();
Section section = newWord.AddSection();
//Keep the same size
section.PageSetup.PageSize = this.SPD.Sections[0].PageSetup.PageSize;
//Keep the same margin
section.PageSetup.Margins = this.SPD.Sections[0].PageSetup.Margins;
...
public void SplitDoc_SpireDoc()
{
Document newWord = new Document();
Section section = newWord.AddSection();
//Keep the same size
section.PageSetup.PageSize = this.SPD.Sections[0].PageSetup.PageSize;
//Keep the same margin
section.PageSetup.Margins = this.SPD.Sections[0].PageSetup.Margins;
//keep the same header/footer distance
section.PageSetup.HeaderDistance = this.SPD.Sections[0].PageSetup.HeaderDistance;
section.PageSetup.FooterDistance = this.SPD.Sections[0].PageSetup.FooterDistance;
//Select the same header/footer option
section.PageSetup.DifferentFirstPageHeaderFooter = this.SPD.Sections[0].PageSetup.DifferentFirstPageHeaderFooter;
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = this.SPD.Sections[0].PageSetup.DifferentOddAndEvenPagesHeaderFooter;
//Clone header and footer
for (int i = 0; i < 6; i++)
{
foreach (DocumentObject item in this.SPD.Sections[0].HeadersFooters[i].ChildObjects)
{
section.HeadersFooters[i].ChildObjects.Add(item.Clone());
}
}
int index = 0;
string DocFile = "";
foreach (Section sec in this.SPD.Sections)
{
foreach (DocumentObject obj in sec.Body.ChildObjects)
{
if (obj is Paragraph)
{
Paragraph para = obj as Paragraph;
section.Body.ChildObjects.Add(para.Clone());
for (int d = 0; d < para.ChildObjects.Count; d++)
{
DocumentObject parobj = para.ChildObjects[d];
if (parobj is BookmarkEnd && (parobj as BookmarkEnd).Name.Contains("uLSF_"))
{
DocFile = "d:\\" + ((BookmarkEnd)parobj).Name + ".pdf";
int i = para.ChildObjects.IndexOf(parobj);
newWord.SaveToFile(DocFile, FileFormat.PDF);
index++;
newWord = new Document();
section = newWord.AddSection();
section.PageSetup.PageSize = sec.PageSetup.PageSize;
section.PageSetup.Margins = sec.PageSetup.Margins;
section.PageSetup.HeaderDistance = sec.PageSetup.HeaderDistance;
section.PageSetup.FooterDistance = sec.PageSetup.FooterDistance;
section.PageSetup.DifferentFirstPageHeaderFooter = sec.PageSetup.DifferentFirstPageHeaderFooter;
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = sec.PageSetup.DifferentOddAndEvenPagesHeaderFooter;
//Clone header and footer
for (int j = 0; j < 6; j++)
{
foreach (DocumentObject item in sec.HeadersFooters[j].ChildObjects)
{
section.HeadersFooters[j].ChildObjects.Add(item.Clone());
}
}
section.Body.ChildObjects.Add(para.Clone());
if (section.Paragraphs[0].ChildObjects.Count == 0) section.Body.ChildObjects.RemoveAt(0);
else
{
while (i >= 0)
{
section.Paragraphs[0].ChildObjects.RemoveAt(i);
i--;
}
for (int c = 0; c < section.Paragraphs[0].ChildObjects.Count; c++)
{
if (section.Paragraphs[0].ChildObjects[c] is Break)
{
section.Paragraphs[0].ChildObjects.Remove(section.Paragraphs[0].ChildObjects[c]);
}
}
}
}
}
}
if (obj is Table) section.Body.ChildObjects.Add(obj.Clone());
}
}
}