In today’s digital world, PDF documents have become a staple for sharing and storing information. However, there are times when you may need to make modifications to your PDF files, such as adding or removing pages. Whether you want to insert additional pages to provide additional content, delete unnecessary pages to streamline your document, or rearrange the page order to improve the flow, having the ability to perform these operations is crucial. In this article, we will explore how to add and remove PDF pages using Python Cloud SDK, providing you with the knowledge and tools to efficiently manage and customize your PDF documents according to your specific needs.
- PDF Processing SDK
- Add Pages to PDF using Python
- Delete Pages from PDF using Python
- Add PDF Pages using cURL Command
- Delete PDF Pages using cURL Command
PDF Processing SDK
Aspose.PDF Cloud is a REST-based API providing the capabilities to perform PDF creation and processing capabilities. No Adobe Acrobat or other software installation is required and you can perform all the operations in the cloud. As per the scope of this article, we are going to use Aspose.PDF Cloud SDK for Python for PDF file processing. This SDK is a wrapper around REST API, and you can freely download it from PIP and GitHub repository. So, please execute the following command on the terminal/command prompt to install the latest version of SDK.
pip install asposepdfcloud
MS Visual Studio
You may also directly add the reference in your Python project within the Visual Studio project. Please search asposepdfcloud as a package under the Python environment window. Please follow the steps numbered in the image below to complete the installation process.
Since our APIs are only accessible to authorized persons, so you also need to create an account on cloud dashboard (in case you do not have an existing account). Use your GitHub or Google account to Sign in or, click the create a new account button and provide the required information. Then, login to the dashboard and expand the Applications section over dashboard. Now scroll down towards the Client Credentials section to obtain your personalized Client ID and Client Secret details.
Add Pages to PDF using Python
Please follow the steps given below to add pages to the PDF document using Python code snippet:
- First, we need to create an instance of ApiClient class while providing Client ID Client Secret as arguments.
- Secondly, create an instance of PdfApi class which takes ApiClient object as an input argument.
- Now call the method put_add_new_page(..) of PdfApi class, which adds a blank page at the end of the document. The resultant PDF is then saved on cloud storage.
Delete Pages from PDF using Python
The API also provides the capability to delete a specific page from PDF document. So in order to fulfill this requirement, please follow the instructions given below.
- Firstly, create an instance of ApiClient class while providing Client ID Client Secret as arguments.
- Secondly, create an object of PdfApi class which takes ApiClient object as an input argument.
- Finally, call the delete_page(…) method to remove the specific page from PDF document.
Add PDF Pages using cURL Command
The cURL commands provide a convenient approach to accessing REST APIs through the command line terminal. Since Aspose.PDF Cloud is developed as per REST architecture, so we can also use the cURL commands to accomplish our requirements. However, as the APIs are only accessible to authorized users, so first we need to generate a JSON Web Token (JWT) based on our client credentials. So, please execute the following command to generate the JWT access token.
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Please execute the following command to add an empty page at the end of the PDF document.
curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/URL2PDF.pdf/pages" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-d{}
Delete PDF Pages using cURL Command
We can also remove PDF page using cURL command. So please execute the following command to delete the 2nd page of the PDF file.
curl -v -X DELETE "https://api.aspose.cloud/v3.0/pdf/URL2PDF.pdf/pages/2" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-d{}
Conclusion
In this article, we have discussed the details and steps on how we can add or remove pages from PDF documents using REST API. We have explored the options to accomplish this requirement either using Python code snippet or, using the cURL commands. The complete source code of Apsose.PDF Cloud SDK for Python is available for download over GitHub. Lastly, in case you encounter any issues while using the API or you have any related queries, please feel free to contact us via free product support forum.
Related Articles
We also recommend visiting the following links to learn more about: