Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreaded-using-parallel-process", "Multithreaded-using-parallel-process\Multithreaded-using-parallel-process.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-parallel-process", "Multithreading-using-parallel-process\Multithreading-using-parallel-process.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multithreaded_using_parallel_process</RootNamespace>
<RootNamespace>Multithreading_using_parallel_process</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Threading.Tasks;

namespace Multithreaded_using_parallel_process
namespace Multithreading_using_parallel_process
{
class MultiThreading
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreaded-using-tasks", "Multithreaded-using-tasks\Multithreaded-using-tasks.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-tasks", "Multithreading-using-tasks\Multithreading-using-tasks.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multithreaded_using_tasks</RootNamespace>
<RootNamespace>Multithreading_using_tasks</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Threading.Tasks;

namespace Multithreaded_using_tasks
namespace Multithreading_using_tasks
{
class MultiThreading
{
Expand Down
37 changes: 20 additions & 17 deletions Word-document/Split-by-bookmark/.NET/Split-by-bookmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ class Program
static void Main(string[] args)
{
//Load an existing Word document.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//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 location before the end of 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 location before the end of 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 file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/" + bookmark.Name + ".docx"), FileMode.Create, FileAccess.ReadWrite))
{
newDocument.Save(outputFileStream, FormatType.Docx);
}
}
}
}
}

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,80 @@ class Program
static void Main(string[] args)
{
//Load an existing Word document into DocIO instance.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{

//Finds all the placeholder text in the Word document.
TextSelection[] textSelections = document.FindAll(new Regex("<<(.*)>>"));
if (textSelections != null)
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
#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++)

//Finds all the placeholder text in the Word document.
TextSelection[] textSelections = document.FindAll(new Regex("<<(.*)>>"));
if (textSelections != null)
{
#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 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
#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

}
#endregion
#region Split bookmark content into separate documents
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
int fileIndex = 1;
foreach (string bookmark in bookmarks)
{
//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())
}
#endregion
#region Split bookmark content into separate documents
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
int fileIndex = 1;
foreach (string bookmark in bookmarks)
{
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
//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())
{
newDocument.Save(outputFileStream, FormatType.Docx);
//Save the Word document to file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Placeholder_" + fileIndex + ".docx"), FileMode.Create, FileAccess.ReadWrite))
{
newDocument.Save(outputFileStream, FormatType.Docx);
}
}
fileIndex++;
}
fileIndex++;
#endregion
}
#endregion
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreaded-using-parallel-process", "Multithreaded-using-parallel-process\Multithreaded-using-parallel-process.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-parallel-process", "Multithreading-using-parallel-process\Multithreading-using-parallel-process.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multithreaded_using_parallel_process</RootNamespace>
<RootNamespace>Multithreading_using_parallel_process</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.IO;
using System.Threading.Tasks;

namespace Multithreaded_using_parallel_process
namespace Multithreading_using_parallel_process
{
class MultiThreading
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreaded-using-tasks", "Multithreaded-using-tasks\Multithreaded-using-tasks.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading-using-tasks", "Multithreading-using-tasks\Multithreading-using-tasks.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multithreaded_using_tasks</RootNamespace>
<RootNamespace>Multithreading_using_tasks</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.IO;
using System.Threading.Tasks;

namespace Multithreaded_using_tasks
namespace Multithreading_using_tasks
{
class MultiThreading
{
Expand Down
Loading