Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions Word-document/Split-by-bookmark/.NET/Split-by-bookmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Compression.Zip;
using System.IO;

namespace Split_a_document_by_bookmark
Expand All @@ -11,27 +12,34 @@ static void Main(string[] args)
//Load an existing Word document.
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
using (ZipArchive zipArchive = new ZipArchive())
{
//Create the bookmark navigator instance to access the bookmark.
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
BookmarkCollection bookmarkCollection = document.Bookmarks;
//Iterate each bookmark in Word document.
foreach (Bookmark bookmark in bookmarkCollection)


using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Move the virtual cursor to the bookmark.
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())
//Create the bookmark navigator instance to access the bookmark.
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
BookmarkCollection bookmarkCollection = document.Bookmarks;
//Iterate each bookmark in Word document.
foreach (Bookmark bookmark in bookmarkCollection)
{
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
//Move the virtual cursor to the bookmark.
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 MemoryStream.
MemoryStream memoryStream = new MemoryStream();
newDocument.Save(memoryStream, FormatType.Docx);
//Add the Word document to Zip archive.
zipArchive.AddItem(bookmark.Name + ".docx", memoryStream, true, Syncfusion.Compression.FileAttributes.Normal);
}
}

zipArchive.Save(Path.GetFullPath(@"Output/Split-by-Bookmark.zip"));
}
}

Expand Down
Loading