Using RemovePagesPdf to Remove Pages from PDF Documents
Leave feedback
On this page
This topic shows how to use the RemovePagesPdf plugin to remove pages from a PDF document. The pages to remove are described by RemoveOptions, and Save writes the remaining pages to a new document.
Prerequisites
Refer to the Developer Guide to learn how to set up your environment and run the code examples in this section.
Pass the numbers of the pages to remove. Page numbers start at 1. The following example removes pages 2 and 4 from an 18-page document.
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);// Remove pages 2 and 4varremover=newRemovePagesPdf("business-plan.pdf",newRemoveOptions(new[]{2,4}));// Save the remaining pagesremover.Save("output/trimmed.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)' Remove pages 2 and 4
DimremoverAsNewRemovePagesPdf("business-plan.pdf",NewRemoveOptions({2,4}))' Save the remaining pages
remover.Save("output/trimmed.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)// Remove pages 2 and 4
letremover=RemovePagesPdf("business-plan.pdf",RemoveOptions([|2;4|]))// Save the remaining pages
remover.Save("output/trimmed.pdf")0
Pass the first and the last page of a range. The following example removes the appendix: pages 15 to 18.
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);// Remove pages 15 to 18varremover=newRemovePagesPdf("business-plan.pdf",newRemoveOptions(15,18));// Save the remaining pagesremover.Save("output/without-appendix.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)' Remove pages 15 to 18
DimremoverAsNewRemovePagesPdf("business-plan.pdf",NewRemoveOptions(15,18))' Save the remaining pages
remover.Save("output/without-appendix.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)// Remove pages 15 to 18
letremover=RemovePagesPdf("business-plan.pdf",RemoveOptions(15,18))// Save the remaining pages
remover.Save("output/without-appendix.pdf")0
Add a RangeMode to narrow the range to its odd or even pages. The following example removes 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);// Remove the even pages from 1 to 6varremover=newRemovePagesPdf("business-plan.pdf",newRemoveOptions(1,6,RangeMode.EvenPages));// Save the remaining pagesremover.Save("output/without-even-pages.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)' Remove the even pages from 1 to 6
DimremoverAsNewRemovePagesPdf("business-plan.pdf",NewRemoveOptions(1,6,RangeMode.EvenPages))' Save the remaining pages
remover.Save("output/without-even-pages.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)// Remove the even pages from 1 to 6
letremover=RemovePagesPdf("business-plan.pdf",RemoveOptions(1,6,RangeMode.EvenPages))// Save the remaining pages
remover.Save("output/without-even-pages.pdf")0
The source document can also be a Stream. It is read from the beginning, and it stays open after Save.
usingSystem;usingSystem.IO;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);// Open the source document as a streamusingvarstream=File.OpenRead("business-plan.pdf");// Remove pages 2 and 4varremover=newRemovePagesPdf(stream,newRemoveOptions(new[]{2,4}));// Save the remaining pagesremover.Save("output/trimmed.pdf");
ImportsSystemImportsSystem.IOImportsGroupDocs.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)' Open the source document as a stream
Usingstream=File.OpenRead("business-plan.pdf")' Remove pages 2 and 4
DimremoverAsNewRemovePagesPdf(stream,NewRemoveOptions({2,4}))' Save the remaining pages
remover.Save("output/trimmed.pdf")EndUsingEndSubEndModule
openSystemopenSystem.IOopenGroupDocs.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)// Open the source document as a stream
usestream=File.OpenRead("business-plan.pdf")// Remove pages 2 and 4
letremover=RemovePagesPdf(stream,RemoveOptions([|2;4|]))// Save the remaining pages
remover.Save("output/trimmed.pdf")0
At least one page number is required, and every page number must exist in the document. Otherwise, Save throws GroupDocsMergerException. See Handling Errors.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.