Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 27, 2021 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/e1805dc8ef1cfc13daa4b8fcd5d392e2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e1805dc8ef1cfc13daa4b8fcd5d392e2 to your computer and use it in GitHub Desktop.
Split MS Word Documents using C++
Examples for splitting MS Word Documents using C++
// Sample Word document
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";
// Load the Word document
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);
// Create and initialize document page splitter
DocumentPageSplitter splitter = DocumentPageSplitter(document);
// Get the page range
auto pageDoc = splitter.GetDocumentOfPageRange(2, 3);
// Output file path
System::String outputPath = u"OutputDirectory\\";
// Save as DOCX file
pageDoc->Save(outputPath + u"SplitDocumentByPageRangeOut.docx");
// Sample Word document
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";
// Load the Word document
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);
// Create and initialize document page splitter
DocumentPageSplitter splitter = DocumentPageSplitter(document);
// Output file path
System::String outputPath = u"OutputDirectory\\";
// Save each page as a separate document
for (int page = 1; page <= document->get_PageCount(); page++)
{
auto pageDoc = splitter.GetDocumentOfPage(page);
pageDoc->Save(outputPath + u"SplitDocumentPageByPageOut_" + System::Convert::ToString(page) + u".docx");
}
// Sample Word document
System::String sampleFile = u"SourceDirectory\\Sample 3.docx";
// Load the Word document
System::SharedPtr<Aspose::Words::Document> document = System::MakeObject<Aspose::Words::Document>(sampleFile);
// Output file path
System::String outputPath = u"OutputDirectory\\";
for (int i = 0; i < document->get_Sections()->get_Count(); i++)
{
// Split a document into smaller parts, in this instance split by section
auto section = document->get_Sections()->idx_get(i)->Clone();
// Create a new document
auto newDoc = System::MakeObject<Aspose::Words::Document>();
newDoc->get_Sections()->Clear();
//Add new section to the newly created document
auto newSection = System::StaticCast<Aspose::Words::Section>(newDoc->ImportNode(section, true));
newDoc->get_Sections()->Add(newSection);
// Save each section as a separate document
newDoc->Save(outputPath + u"SplitDocumentBySectionsOut_" + System::Convert::ToString(i) + u".docx");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment