This topic shows how to use the SplitPdf plugin to split a PDF document into several PDF documents. You select the pages in the constructor, and Save writes one document per part. The part number is added to the output file name: page.pdf becomes page_1.pdf, page_2.pdf, and so on.
Prerequisites
Refer to the Developer Guide to learn how to set up your environment and run the code examples in this section.
The following example puts pages 1, 2, and 3 into documents of their own. Each part is numbered after the source page it holds: page_1.pdf, page_2.pdf, and page_3.pdf.
usingSystem;usingGroupDocs.Merger.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Select pages 1, 2, and 3varsplitter=newSplitPdf("business-plan.pdf",new[]{1,2,3});// Save one document per partsplitter.Save("output/page.pdf");
ImportsSystemImportsGroupDocs.Merger.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Select pages 1, 2, and 3
DimsplitterAsNewSplitPdf("business-plan.pdf",{1,2,3})' Save one document per part
splitter.Save("output/page.pdf")EndSubEndModule
openSystemopenGroupDocs.Merger.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Select pages 1, 2, and 3
letsplitter=SplitPdf("business-plan.pdf",[|1;2;3|])// Save one document per part
splitter.Save("output/page.pdf")0
In SplitMode.Interval, the pages you list mark where each new part starts. The following example cuts an 18-page document before pages 7 and 13: chapter_0.pdf holds pages 1 to 6, chapter_1.pdf holds pages 7 to 12, and chapter_2.pdf holds pages 13 to 18. Interval parts are numbered from 0.
usingSystem;usingGroupDocs.Merger.Domain.Options;usingGroupDocs.Merger.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Start a new part at pages 7 and 13varsplitter=newSplitPdf("business-plan.pdf",new[]{7,13},SplitMode.Interval);// Save one document per partsplitter.Save("output/chapter.pdf");
ImportsSystemImportsGroupDocs.Merger.Domain.OptionsImportsGroupDocs.Merger.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Start a new part at pages 7 and 13
DimsplitterAsNewSplitPdf("business-plan.pdf",{7,13},SplitMode.Interval)' Save one document per part
splitter.Save("output/chapter.pdf")EndSubEndModule
openSystemopenGroupDocs.Merger.Domain.OptionsopenGroupDocs.Merger.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Start a new part at pages 7 and 13
letsplitter=SplitPdf("business-plan.pdf",[|7;13|],SplitMode.Interval)// Save one document per part
splitter.Save("output/chapter.pdf")0
Pass the first and the last page of a range to put every page of the range into a document of its own. The following example splits out pages 2 to 4.
usingSystem;usingGroupDocs.Merger.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Select pages 2 to 4varsplitter=newSplitPdf("business-plan.pdf",2,4);// Save one document per partsplitter.Save("output/page.pdf");
ImportsSystemImportsGroupDocs.Merger.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Select pages 2 to 4
DimsplitterAsNewSplitPdf("business-plan.pdf",2,4)' Save one document per part
splitter.Save("output/page.pdf")EndSubEndModule
openSystemopenGroupDocs.Merger.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Select pages 2 to 4
letsplitter=SplitPdf("business-plan.pdf",2,4)// Save one document per part
splitter.Save("output/page.pdf")0
Add a RangeMode to narrow the range to its odd or even pages. The following example splits out the even pages from 1 to 6: pages 2, 4, and 6.
usingSystem;usingGroupDocs.Merger.Domain.Options;usingGroupDocs.Merger.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Select the even pages from 1 to 6varsplitter=newSplitPdf("business-plan.pdf",1,6,RangeMode.EvenPages);// Save one document per partsplitter.Save("output/even-page.pdf");
ImportsSystemImportsGroupDocs.Merger.Domain.OptionsImportsGroupDocs.Merger.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Select the even pages from 1 to 6
DimsplitterAsNewSplitPdf("business-plan.pdf",1,6,RangeMode.EvenPages)' Save one document per part
splitter.Save("output/even-page.pdf")EndSubEndModule
openSystemopenGroupDocs.Merger.Domain.OptionsopenGroupDocs.Merger.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Select the even pages from 1 to 6
letsplitter=SplitPdf("business-plan.pdf",1,6,RangeMode.EvenPages)// Save one document per part
splitter.Save("output/even-page.pdf")0
When the output file name contains {0}, the part number replaces it instead of being added at the end. The following example produces business-plan-page-1.pdf and business-plan-page-2.pdf.
usingSystem;usingGroupDocs.Merger.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Select pages 1 and 2varsplitter=newSplitPdf("business-plan.pdf",new[]{1,2});// Save one document per partsplitter.Save("output/business-plan-page-{0}.pdf");
ImportsSystemImportsGroupDocs.Merger.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Select pages 1 and 2
DimsplitterAsNewSplitPdf("business-plan.pdf",{1,2})' Save one document per part
splitter.Save("output/business-plan-page-{0}.pdf")EndSubEndModule
openSystemopenGroupDocs.Merger.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Select pages 1 and 2
letsplitter=SplitPdf("business-plan.pdf",[|1;2|])// Save one document per part
splitter.Save("output/business-plan-page-{0}.pdf")0
The source document can also be a Stream. It is read from the beginning, whatever its current position, and it stays open after Save.
usingSystem;usingSystem.IO;usingGroupDocs.Merger.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Open the source document as a streamusingvarstream=File.OpenRead("business-plan.pdf");// Select pages 1 and 2varsplitter=newSplitPdf(stream,new[]{1,2});// Save one document per partsplitter.Save("output/page.pdf");
ImportsSystemImportsSystem.IOImportsGroupDocs.Merger.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Open the source document as a stream
Usingstream=File.OpenRead("business-plan.pdf")' Select pages 1 and 2
DimsplitterAsNewSplitPdf(stream,{1,2})' Save one document per part
splitter.Save("output/page.pdf")EndUsingEndSubEndModule
openSystemopenSystem.IOopenGroupDocs.Merger.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Open the source document as a stream
usestream=File.OpenRead("business-plan.pdf")// Select pages 1 and 2
letsplitter=SplitPdf(stream,[|1;2|])// Save one document per part
splitter.Save("output/page.pdf")0