Add or Remove PDF Pages using Python

Edit PDF pages

A PDF file is comprised of multiple pages which may contain information related to a similar topic or different context. We may have a requirement to add new pages to the PDF document. Or, we may have a requirement to remove pages containing sensitive information from existing PDF files so that they can be shared onwards or stored for long-term archival. In this article, we are going to discuss the details on how to add or remove pdf pages using Python.

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 download or installation is required and 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. The Python SDK is a wrapper around Cloud API. So the first step to use the API is its installation. The SDK is available for free download over PIP and GitHub repository. Now execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

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.

Aspose.PDF Cloud Python
Image 1:- Aspose.PDF Cloud SDK for Python package.

Aspose.Cloud Dashboard

Since our APIs are only accessible to authorized persons, so the next step is to create an account on Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client credentials
Image 2:- Client credentials on Aspose.Cloud dashboard.

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 input argument
  • Now call put_add_new_page(..) method of PdfApi class to add a blank page at the end of document. The resultant page is saved on Cloud storage.

Delete Pages from PDF using Python

The API also provides the capabilities to delete specific page from PDF document. Please follow the instructions given below to accomplish your requirements.

  • 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 while passing ApiClient object as input an argument
  • Finally, call the delete_page(…) method to remove 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 we need to first generate a JSON Web Token (JWT) based on your individual client credentials specified over Aspose.Cloud dashboard. It is mandatory because our APIs are only accessible to registered users. Please execute the following command to generate the JWT 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"

Now that we have a personalized JWT token, we can execute the following command to add an empty page at the end of the 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

The API also enables you to delete specific pages from PDF documents. Please execute the following command to delete 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 either accomplish this requirement using Python code snippet or using the cURL commands. Please note that the complete source code of Apsose.PDF Cloud SDK for Python is available for download under the MIT license over GitHub. In case you encounter any issues while using the API or you have any further queries, please feel free to contact us via the Free product support forum.

Related Articles

We also recommend visiting the following links to learn more about