Add a New Page in PDF Introduction
You can add or create pdf page by using Aspose.PDF Cloud REST API. The SDKs are available in various languages such as, C#, Java, Python, Ruby, PHP, Node.js, Swift, Android and Go.
The following example shows how the API works. The API has the following properties:
API
Type
Description
/pdf/{name}/pages
PUT
Add a new Page to the end of the document
and the following parameters:
Name
Required
Description
Parameter Type
Data Type
name
True
The name of the document in Storage to add the new page to
Path
String
Resource
The following Aspose.PDF Cloud REST API resource has been used in the examples: Add Pages .
cURL Example
Input File: 4pages.pdf
Request
Get Authentication Token
Copy
curl - v "https://api.aspose.cloud/connect/token" \
- X POST \
- d "grant_type=client_credentials&client_id=xxxx&client_secret=xxxx" \
- H "Content-Type: application/x-www-form-urlencoded" \
- H "Accept: application/json"
Copy
curl - X PUT "https://api.aspose.cloud/v3.0/pdf/4pages.pdf/pages" \
- H "accept: application/json" \
- H "authorization: Bearer <jwt token>"
Response
Copy
{
"Pages" : {
"List" : [
{
"Id" : 0 ,
"Images" : null ,
"Rectangle" : null ,
"Links" : [
{
"Href" : "/1" ,
"Rel" : "self" ,
"Type" : null ,
"Title" : null
}
]
},
{
"Id" : 0 ,
"Images" : null ,
"Rectangle" : null ,
"Links" : [
{
"Href" : "/2" ,
"Rel" : "self" ,
"Type" : null ,
"Title" : null
}
]
}
],
"Links" : [
{
"Href" : "http://api.aspose.cloud/v3.0/pdf/sample.pdf/pages" ,
"Rel" : "self" ,
"Type" : null ,
"Title" : null
}
]
},
"Code" : 200 ,
"Status" : "OK"
}
SDK Source
The Aspose for Cloud SDKs can be downloaded from the following page: Aspose.PDF Cloud GitHub
SDK Examples
Add new page in PDF
C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Cloud
PdfApi pdfApi = new PdfApi("API_KEY", "APP_SID");
String fileName = "sample-input.pdf";
String storage = "";
String folder = "";
try
{
// Upload source file to aspose cloud storage
pdfApi.UploadFile(fileName, System.IO.File.ReadAllBytes(Common.GetDataDir() + fileName));
// Invoke Aspose.PDF Cloud SDK API to add new page
DocumentPagesResponse apiResponse = pdfApi.PutAddNewPage(fileName, storage, folder);
if (apiResponse != null && apiResponse.Status.Equals("OK"))
{
Console.WriteLine("Add a New Page in PDF, Done!");
Console.ReadKey();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-Cloud
String fileName = "sample-input.pdf";
String storage = "";
String folder = "";
Path inputFile = Utils.getPath(CreatePDFFromHtmlExample.class, fileName);
try
{
// Instantiate Aspose Words API SDK
PdfApi pdfApi = new PdfApi(Configuration.apiKey, Configuration.appSID);
// Upload source file to aspose cloud storage
pdfApi.uploadFile(fileName, inputFile.toFile(),null);
// Invoke Aspose.PDF Cloud SDK API to add new page
DocumentPagesResponse apiResponse = pdfApi.PutAddNewPage(fileName, storage, folder);
if (apiResponse != null && apiResponse.getStatus().equals("OK"))
{
System.out.println("Add a New Page in PDF, Done!");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Cloud
<?php
require_once realpath(__DIR__ . '/..') . '/vendor/autoload.php';
require_once realpath(__DIR__ . '/..') . '/Utils.php';
use Aspose\PDF\PdfApi;
use Aspose\PDF\AsposeApp;
class Pages {
public $pdfApi;
public function __construct() {
AsposeApp::$appSID = Utils::appSID;
AsposeApp::$apiKey = Utils::apiKey;
$this->pdfApi = new PdfApi();
}
public function putAddNewPage() {
// Upload file to Aspose Cloud Storage
$fileName = "sample-input.pdf";
Utils::uploadFile($fileName);
$result = $this->pdfApi->PutAddNewPage($fileName, $storage = "", $folder = "");
print_r ( $result );
}
}
$pages = new Pages();
$pages->putAddNewPage();
?>
Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Cloud
require 'aspose_pdf_cloud'
class Page
include AsposePDFCloud
include AsposeStorageCloud
def initialize
#Get App key and App SID from https://cloud.aspose.com
AsposeApp.app_key_and_sid("APP_KEY", "APP_SID")
@pdf_api = PdfApi.new
end
def upload_file(file_name)
@storage_api = StorageApi.new
response = @storage_api.put_create(file_name, File.open("../../../data/" << file_name,"r") { |io| io.read } )
end
# Add new page to end of the document.
def add_new_page_to_end_of_document
file_name = "sample-input.pdf"
upload_file(file_name)
response = @pdf_api.put_add_new_page(file_name)
end
end
page = Page.new()
puts page.add_new_page_to_end_of_document
Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asposepdfcloud
from asposepdfcloud.PdfApi import PdfApi
from asposepdfcloud.PdfApi import ApiException
import asposestoragecloud
from asposestoragecloud.StorageApi import StorageApi
from asposestoragecloud.StorageApi import ResponseMessage
apiKey = "XXXXX" #sepcify App Key
appSid = "XXXXX" #sepcify App SID
apiServer = "http://api.aspose.com/v1.1"
data_folder = "../../data/"
#Instantiate Aspose Storage API SDK
storage_apiClient = asposestoragecloud.ApiClient.ApiClient(apiKey, appSid, True)
storageApi = StorageApi(storage_apiClient)
#Instantiate Aspose Pdf API SDK
api_client = asposepdfcloud.ApiClient.ApiClient(apiKey, appSid, True)
pdfApi = PdfApi(api_client);
#set input file name
name = "sample-input.pdf"
try:
#upload file to aspose cloud storage
response = storageApi.PutCreate(name, data_folder + name)
#invoke Aspose.Pdf Cloud SDK API to a add new page
response = pdfApi.PutAddNewPage(name)
if response.Status == "OK":
#download appended pdf from response
response = storageApi.GetDownload(Path=name)
outfilename = "c:/temp/" + name
with open(outfilename, 'wb') as f:
for chunk in response.InputStream:
f.write(chunk)
except ApiException as ex:
print "ApiException:"
print "Code:" + str(ex.code)
print "Message:" + ex.message
Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-Cloud
String fileName = "sample1-input.pdf";
String storage = "";
String folder = "";
File input = Utils.stream2file("input","pdf", context.getResources().openRawResource(R.raw.sample1));
try
{
StorageApi storageApi = new StorageApi(Configuration.apiKey, Configuration.appSID, true);
// Instantiate Aspose Words API SDK
PdfApi pdfApi = new PdfApi(Configuration.apiKey, Configuration.appSID, true);
// Upload source file to aspose cloud storage
// Upload source file to aspose cloud storage
storageApi.PutCreate(fileName, "", "", input);
// Invoke Aspose.PDF Cloud SDK API to add new page
DocumentPagesResponse apiResponse = pdfApi.PutAddNewPage(fileName, storage, folder);
if (apiResponse != null && apiResponse.getStatus().equals("OK"))
{
System.out.println("Add a New Page in PDF, Done!");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Cloud
fileName := "4pages.pdf"
filePath := "data/4pages.pdf"
// init words cloud api
config := asposepdfcloud.NewConfiguration(AppSid, AppKey, BaseURL)
client := asposepdfcloud.NewAPIClient(config)
// Upload 1st document
file, _ := os.Open(filePath)
_, _, err1 := client.PdfApi.UploadFile(fileName, file, nil)
if err1 != nil {
fmt.Println(err1)
}
options := map[string]interface{}{
"storage": "First Storage",
}
//Add new page to PDF doc
documentPageResponse, response, _ := client.PdfApi.PutAddNewPage(fileName, options)
fmt.Println(response.Status)
fmt.Println(documentPageResponse.Pages.List[0].Id)