From f066ce36398a0cfe8831e7e5ac5ae6e48a625af8 Mon Sep 17 00:00:00 2001 From: Viswajith-SF4658 Date: Fri, 14 Aug 2026 11:25:38 +0530 Subject: [PATCH] Updated DocIO Example splitting bookmark sample to zip output files --- .../.NET/Split-by-bookmark/Program.cs | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/Word-document/Split-by-bookmark/.NET/Split-by-bookmark/Program.cs b/Word-document/Split-by-bookmark/.NET/Split-by-bookmark/Program.cs index f7a2b97f..45144e23 100644 --- a/Word-document/Split-by-bookmark/.NET/Split-by-bookmark/Program.cs +++ b/Word-document/Split-by-bookmark/.NET/Split-by-bookmark/Program.cs @@ -1,5 +1,6 @@ using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; +using Syncfusion.Compression.Zip; using System.IO; namespace Split_a_document_by_bookmark @@ -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")); } }