Skip to content

Commit

Permalink
Merge pull request #187 from AntonihilSahayaraj/main
Browse files Browse the repository at this point in the history
Modified the to Split Word document samples
  • Loading branch information
MohanaselvamJothi authored May 27, 2024
2 parents 038e53f + b492d25 commit 93f8f98
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 67 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System;
using System.IO;

namespace Split_a_document_by_bookmark
Expand All @@ -10,29 +9,27 @@ class Program
static void Main(string[] args)
{
//Load an existing Word document.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Create the bookmark navigator instance to access the bookmark.
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Get the bookmark collections in the document.
BookmarkCollection bookmarkCollection = document.Bookmarks;
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
BookmarkCollection bookmarkCollection = document.Bookmarks;
//Iterate each bookmark in Word document.
foreach (Bookmark bookmark in bookmarkCollection)
{
//Move the virtual cursor to the location before the end of the bookmark.
bookmarkNavigator.MoveToBookmark(bookmark.Name);
//Get the bookmark content.
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
//Create a new Word document.
WordDocument newDocument = new WordDocument();
newDocument.AddSection();
//Add the retrieved content into another new document.
for (int i = 0; i < part.BodyItems.Count; i++)
newDocument.LastSection.Body.ChildEntities.Add(part.BodyItems[i].Clone());
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result"+ bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
bookmarksNavigator.MoveToBookmark(bookmark.Name);
//Get the bookmark content as WordDocumentPart.
WordDocumentPart documentPart = bookmarksNavigator.GetContent();
//Save the WordDocumentPart as separate Word document
using (WordDocument newDocument = documentPart.GetAsWordDocument())
{
newDocument.Save(outputFileStream, FormatType.Docx);
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
{
newDocument.Save(outputFileStream, FormatType.Docx);
}
}
}
}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

Expand All @@ -9,52 +8,56 @@ class Program
{
static void Main(string[] args)
{
using (FileStream inputStream = new FileStream(@"../../../Template.docx", FileMode.Open, FileAccess.Read))
using (FileStream inputStream = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read))
{
//Load the template document as stream
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
WordDocument newDocument = null;
WSection newSection = null;
int i = 0;
//Iterate each section from Word document
int headingIndex = 0;
//Iterate each section in the Word document.
foreach (WSection section in document.Sections)
{
// Clone the section and add into new document.
if (newDocument != null)
newSection = AddSection(newDocument, section);
foreach (TextBodyItem textbodyitem in section.Body.ChildEntities)
//Iterate each child entity in the Word document.
foreach (TextBodyItem item in section.Body.ChildEntities)
{
if (textbodyitem is WParagraph)
//If item is paragraph, then check for heading style and split.
//else, add the item into new document.
if (item is WParagraph)
{
WParagraph para = textbodyitem as WParagraph;
if (para.StyleName == "Heading 1")
WParagraph paragraph = item as WParagraph;
//If paragraph has Heading 1 style, then save the traversed content as separate document.
//And create new document for new heading content.
if (paragraph.StyleName == "Heading 1")
{
if (newDocument != null)
{
//Saves the Word document
string fileName = @"../../../Heading" + i + ".docx";
string fileName = @"../../../Document" + (headingIndex + 1) + ".docx";
SaveWordDocument(newDocument, fileName);
i++;
headingIndex++;
}
//Create new Word document
//Create new document for new heading content.
newDocument = new WordDocument();
newSection = AddSection(newDocument, section);
//Add cloned paragraphs into new section
AddEntity(newSection, para);
AddEntity(newSection, paragraph);
}
else if (newDocument != null)
//Add cloned paragraphs into new section
AddEntity(newSection, para);
AddEntity(newSection, paragraph);
}
else
//Add cloned item into new section
AddEntity(newSection, textbodyitem);
else
AddEntity(newSection, item);
}
}
//Save the remaining content as separate document.
if (newDocument != null)
{
//Saves the Word document
string fileName = @"../../../Heading" + i + ".docx";
string fileName = @"../../../Document" + (headingIndex + 1) + ".docx";
SaveWordDocument(newDocument, fileName);
}
}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace Split_a_document_by_placeholder_text
{
Expand All @@ -10,35 +11,78 @@ class Program
static void Main(string[] args)
{
//Load an existing Word document into DocIO instance.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
String[] findPlaceHolderWord = new string[] { "[First Content Start]", "[Second Content Start]", "[Third Content Start]" };
for (int i = 0; i < findPlaceHolderWord.Length; i++)

//Finds all the placeholder text in the Word document.
TextSelection[] textSelections = document.FindAll(new Regex("<<(.*)>>"));
if (textSelections != null)
{
//Get the start placeholder paragraph in the document.
WParagraph startParagraph = document.Find(findPlaceHolderWord[i], true, true).GetAsOneRange().OwnerParagraph;
//Get the end placeholder paragraph in the document.
WParagraph endParagraph = document.Find(findPlaceHolderWord[i].Replace("Start", "End"), true, true).GetAsOneRange().OwnerParagraph;
//Get the text body.
WTextBody textBody = startParagraph.OwnerTextBody;
//Get the start PlaceHolder index.
int startPlaceHolderIndex = textBody.ChildEntities.IndexOf(startParagraph);
//Get the end PlaceHolder index.
int endPlaceHolderIndex = textBody.ChildEntities.IndexOf(endParagraph);
#region Insert bookmarks at placeholders
//Unique ID for each bookmark.
int bkmkId = 1;
//Collection to hold the inserted bookmarks.
List<string> bookmarks = new List<string>();
//Iterate each text selection.
for (int i = 0; i < textSelections.Length; i++)
{
#region Insert bookmark start before the placeholder
//Get the placeholder as WTextRange.
WTextRange textRange = textSelections[i].GetAsOneRange();
//Get the index of the placeholder text.
WParagraph startParagraph = textRange.OwnerParagraph;
int index = startParagraph.ChildEntities.IndexOf(textRange);
string bookmarkName = "Bookmark_" + bkmkId;
//Add new bookmark to bookmarks collection.
bookmarks.Add(bookmarkName);
//Create bookmark start.
BookmarkStart bkmkStart = new BookmarkStart(document, bookmarkName);
//Insert the bookmark start before the start placeholder.
startParagraph.ChildEntities.Insert(index, bkmkStart);
//Remove the placeholder text.
textRange.Text = string.Empty;
#endregion

#region Insert bookmark end after the placeholder
i++;
//Get the placeholder as WTextRange.
textRange = textSelections[i].GetAsOneRange();
//Get the index of the placeholder text.
WParagraph endParagraph = textRange.OwnerParagraph;
index = endParagraph.ChildEntities.IndexOf(textRange);
//Create bookmark end.
BookmarkEnd bkmkEnd = new BookmarkEnd(document, bookmarkName);
//Insert the bookmark end after the end placeholder.
endParagraph.ChildEntities.Insert(index + 1, bkmkEnd);
bkmkId++;
//Remove the placeholder text.
textRange.Text = string.Empty;
#endregion

//Create a new Word document.
WordDocument newDocument = new WordDocument();
newDocument.AddSection();
//Add the retrieved content into another new document.
for (int j = startPlaceHolderIndex + 1; j < endPlaceHolderIndex; j++)
newDocument.LastSection.Body.ChildEntities.Add(textBody.ChildEntities[j].Clone());
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result" + i + ".docx"), FileMode.Create, FileAccess.ReadWrite))
}
#endregion
#region Split bookmark content into separate documents
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
int fileIndex = 1;
foreach (string bookmark in bookmarks)
{

newDocument.Save(outputFileStream, FormatType.Docx);
//Move the virtual cursor to the location before the end of the bookmark.
bookmarksNavigator.MoveToBookmark(bookmark);
//Get the bookmark content as WordDocumentPart.
WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent();
//Save the WordDocumentPart as separate Word document.
using (WordDocument newDocument = wordDocumentPart.GetAsWordDocument())
{
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
{
newDocument.Save(outputFileStream, FormatType.Docx);
}
}
fileIndex++;
}
#endregion
}
}
}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

Expand All @@ -9,12 +8,12 @@ class Program
{
static void Main(string[] args)
{
using (FileStream inputStream = new FileStream(@"../../../Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (FileStream inputStream = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Load the template document as stream
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
int i = 0;
int fileId = 1;
//Iterate each section from Word document
foreach (WSection section in document.Sections)
{
Expand All @@ -24,12 +23,12 @@ static void Main(string[] args)
//Add cloned section into new Word document
newDocument.Sections.Add(section.Clone());
//Saves the Word document to MemoryStream
using (FileStream outputStream = new FileStream(@"../../../Section" + i + ".docx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (FileStream outputStream = new FileStream(@"../../../Section" + fileId + ".docx", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
newDocument.Save(outputStream, FormatType.Docx);
}
}
i++;
fileId++;
}
}
}
Expand Down
Binary file not shown.

0 comments on commit 93f8f98

Please sign in to comment.