Skip to content

implemented-ueses-cases-attach-images #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions UsesCases/Attachments/add/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
1. Import required libraries
2. Define callback functiions for append new attachment into this Pdf file
3. Initialize credentials for calling Pdf.Cloud.Python REST API functions
4. Create Pdf.Cloud.Python REST API object
5. Initialize new AttachmentInfo object
6. Perform appending attachment into Pdf file using post_add_document_attachment() function

All values of variables starting with "YOUR_****" should be replaced by real user values
"""

import asposepdfcloud
from asposepdfcloud.models import AttachmentInfo

pdf_api_client = asposepdfcloud.ApiClient("YOUR_APP_KEY", "YOUR_APP_SECRET")
api = asposepdfcloud.PdfApi(pdf_api_client)

document_name = "C:/Samples/Sample-Document-01.pdf"
storage_file_name = "Sample-Document-01.pdf"

new_attachment_file = "C:/Samples/Sample-Attachment-Image.png"
new_attachment_mime = "image/png"

attachment_info = AttachmentInfo(
name=new_attachment_file,
mime_type=new_attachment_mime
)

def uploadFile(name, storage):
upload_result = api.upload_file(storage, name)
print(upload_result.status)
print(upload_result.uploaded[0])

def show_attachment(response):
print("Attachment was successfully added : \n Links : ", response.links)
print("\n Description : ", response.description)
print("\n MIME Type : ", response.mime_type)
print("\n Name : ", response.name)
print("\n Creation Date : ", response.creation_date)
print("\n Modification date : ", response.modification_date)
print("\n Size : ", response.size)

uploadFile(document_name, storage_file_name)

api.post_add_document_attachment(document_name, attachment_info, callback=show_attachment)
41 changes: 41 additions & 0 deletions UsesCases/Attachments/get/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
1. Import required libraries
2. Define callback functiions for read attachments from Pdf file and append new attachment into this Pdf file
3. Initialize credentials for calling Pdf.Cloud.Python REST API functions
4. Create Pdf.Cloud.Python REST API object
5. Initialize Pdf ile name and attachment index
6. Perform reading attachments from Pdf file using get_document_attachment_by_index() function

All values of variables starting with "YOUR_****" should be replaced by real user values
"""

import asposepdfcloud

pdf_api_client = asposepdfcloud.ApiClient("YOUR_APP_KEY", "YOUR_APP_SECRET")
api = asposepdfcloud.PdfApi(pdf_api_client)

document_name = "C:/Samples/Sample-Document-01.pdf"
storage_file_name = "Sample-Document-01.pdf"

def uploadFile(name, storage):
upload_result = api.upload_file(storage, name)
print(upload_result.status)
print(upload_result.uploaded[0])

def show_attachments(response):
print("Attachments received successfully : \n List : ", response.list)

for attach in response.list:
print("Attachment was successfully added : \n Links : ", attach.links)
print("\n Description : ", attach.description)
print("\n MIME Type : ", attach.mime_type)
print("\n Name : ", attach.name)
print("\n Creation Date : ", attach.creation_date)
print("\n Modification date : ", attach.modification_date)
print("\n Size : ", attach.size)
print("\n Check Sum : ", attach.check_sum)


uploadFile(document_name, storage_file_name)

api.get_document_attachments(document_name, callback=show_attachments)
56 changes: 56 additions & 0 deletions UsesCases/Attachments/get_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
1. Import required libraries
2. Define callback functiions for read attachments from Pdf file and append new attachment into this Pdf file
3. Initialize credentials for calling Pdf.Cloud.Python REST API functions
4. Initialize Pdf ile name
5. Create Pdf.Cloud.Python REST API object
6. Perform reading attachments from Pdf file using get_document_attachments() fiunction
7. Initialize new AttachmentInfo object
8. Perform appending attachment into Pdf file using post_add_document_attachment() function
9. Check result and making adding actions in callback function callback_add_attachment() (AttachmentsResponse)[https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python/blob/master/docs/AttachmentResponse.md]

All values of variables starting with "YOUR_****" should be replaced by real user values
"""
import asposepdfcloud
from asposepdfcloud.models import AttachmentInfo

pdf_api_client = asposepdfcloud.ApiClient("YOUR_APP_KEY", "YOUR_APP_SECRET")
api = asposepdfcloud.PdfApi(pdf_api_client)

document_name = "C:/Samples/Sample-Document-01.pdf"
storage_file_name = "Sample-Document-01.pdf"

new_attachment_file = "C:/Samples/Sample-Attachment-Image.png"
new_attachment_mime = "image/png"

attachment_info = AttachmentInfo(
name=new_attachment_file,
mime_type=new_attachment_mime
)

def uploadFile(name, storage):
upload_result = api.upload_file(storage, name)
print(upload_result.status)
print(upload_result.uploaded[0])

def show_attachments(response):
print("Attachments received successfully : \n List : ", response.list)

for attach in response.list:
show_attachment(attach)

def show_attachment(response):
print("Attachment was successfully added : \n Links : ", response.links)
print("\n Description : ", response.description)
print("\n MIME Type : ", response.mime_type)
print("\n Name : ", response.name)
print("\n Creation Date : ", response.creation_date)
print("\n Modification date : ", response.modification_date)
print("\n Size : ", response.size)
print("\n Check Sum : ", response.check_sum)

uploadFile(document_name, storage_file_name)

api.get_document_attachments(document_name, callback=show_attachments)

api.post_add_document_attachment(document_name, attachment_info, callback=show_attachment)
61 changes: 61 additions & 0 deletions UsesCases/Images/add/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
1. Load your Application Secret and Key from the JSON file or set credentials in another way
2. Create an object to connect to the Pdf.Cloud API
3. Initialize parameters (folders and Pdf document) for next actions
4. Upload Yyour Pdf document file
5. Upload Your image file
6. Initialize image appending parameters (page and position on page)
7. Perform the appending image into Pdf document using post_insert_image() function
8. Check result and perform some actions with response object

All values of variables starting with "YOUR_****" should be replaced by real user values
"""

import os
import json

import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi

def uploadFile(pdf_api, name, temp_folder, data_path):
pdf_api.upload_file(temp_folder + '/' + name, data_path + name)


json_file = open('YOUR_APPLICATION_CREDENTIALS.json')

data = json.load(json_file)

pdf_api_client = asposepdfcloud.api_client.ApiClient(
app_key=str(data.get('AppKey', '')),
app_sid=str(data.get('AppSID', '')),
host=str(data['ProductUri']),
self_host=bool(data.get('SelfHost', False)),
)

api = PdfApi(pdf_api_client)

output_path = str(data['YOUR_OUTPUT_FOLDER'])
temp_folder = 'YOUR_TEMP_PDF_CLOUD_FOLDER'

file_name = 'YOUR_PDF_DOCUMENT.pdf'

uploadFile(api, file_name, temp_folder, 'YOUR_PDF_DOCUMENT_FOLDER')

image_file_name = 'YOUR_IMAGE_FILE_NAME.YOUR_EXTENSION'
uploadFile(api, file_name, temp_folder, 'YOUR_IMAGE_FILE_FOLDER')

# Replace values with Your datas
page_number = 1
llx = 10
lly = 10
urx = 100
ury = 100

opts = {
"image_file_path" : temp_folder + "/" + image_file_name,
"folder" : temp_folder
}

response = api.post_insert_image(file_name, page_number, llx, lly, urx, ury, **opts)

print (response)
57 changes: 57 additions & 0 deletions UsesCases/Images/get/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
1. Load your Application Secret and Key from the JSON file or set credentials in another way
2. Create an object to connect to the Pdf.Cloud API
3. Initialize parameters (folders and Pdf document) for next actions
4. Upload your Pdf document file
5. Perform the retreiving image Id from Pdf document using get_images() function
6. Perform the reading image from Pdf document using get_image() function and image Id value
172. Check result and perform some actions with response object

All values of variables starting with "YOUR_****" should be replaced by real user values
"""


import os
import json

import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi

from urllib.parse import urlparse

def uploadFile(pdf_api, name, temp_folder, data_path):
pdf_api.upload_file(temp_folder + '/' + name, data_path + name)


json_file = open('YOUR_APPLICATION_CREDENTIALS.json')

data = json.load(json_file)

pdf_api_client = asposepdfcloud.api_client.ApiClient(
app_key=str(data.get('AppKey', '')),
app_sid=str(data.get('AppSID', '')),
host=str(data['ProductUri']),
self_host=bool(data.get('SelfHost', False)),
)

api = PdfApi(pdf_api_client)

output_path = str(data['YOUR_OUTPUT_FOLDER'])
temp_folder = 'YOUR_TEMP_PDF_CLOUD_FOLDER'

file_name = 'YOUR_PDF_DOCUMENT.pdf'

uploadFile(api, file_name, temp_folder, 'YOUR_PDF_DOCUMENT_FOLDER')

opts = {
"folder" : temp_folder
}

page_number = 1
responseImages = api.get_images(file_name, page_number, **opts)

image_id = responseImages.images.list[0].id

response = api.get_image(file_name, image_id, **opts)

print (response)
72 changes: 72 additions & 0 deletions UsesCases/Images/get_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
1. Load your Application Secret and Key from the JSON file or set credentials in another way
2. Create an object to connect to the Pdf.Cloud API
3. Initialize parameters (folders and Pdf document) for next actions
4. Upload your Pdf document file
5. Perform the retreiving image Id from Pdf document using get_images() function
6. Perform the reading image from Pdf document using get_image() function and image Id value
7. Parse response for upload image into temporary folder
8. Initialize second Pdf document for inserting uploaded image from first Pdf document
9. Perform the retreiving image Id from second Pdf document using get_images() function
10. Initialize parameters for replace image in second Pdf document
11. Perform replacing image actiont using put_replace_image() function
12. Check result and perform some actions with response object

All values of variables starting with "YOUR_****" should be replaced by real user values
"""

import os
import shutil
import asposepdfcloud

from urllib.parse import urlparse

pdf_api_client = asposepdfcloud.ApiClient("YOUR_APP_KEY", "YOUR_APP_SECRET")
api = asposepdfcloud.PdfApi(pdf_api_client)

temp_folder = 'TempPdfCloud'
data_path = "C:/Samples/"
output_path = "C:/Samples/Output"

document_name = "Sample-Document-01.pdf"
document_name2 = "Sample-Document-02.pdf"

page_number = 1
def uploadFile(name):
api.upload_file(temp_folder + '/' + name, data_path + name)

uploadFile(document_name)
uploadFile(document_name2)

opts = {
"folder" : temp_folder
}

responseImages = api.get_images(document_name, page_number, **opts)

image_id = responseImages.images.list[0].id

responseImage = api.get_image(document_name, image_id)

print (responseImage)

file_name_link = responseImage.image.links[0].href

a = urlparse(file_name_link)
image_file_name = os.path.basename(a.path)
image_file_path = temp_folder +"/" + image_file_name

resImage = api.download_file(file_name_link)
shutil.move(resImage, image_file_path)

uploadFile(api, image_file_path, image_file_name)

opts = {
"image_file_path" : self.temp_folder + '/' + image_file_name,
"folder" : self.temp_folder
}
response = self.pdf_api.put_replace_image(file_name, image_id, **opts)

response = api.put_replace_image(file_name2, image_id2, **opts)

print (response)
55 changes: 55 additions & 0 deletions UsesCases/Images/remove/remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
1. Load your Application Secret and Key from the JSON file or set credentials in another way
2. Create an object to connect to the Pdf.Cloud API
3. Initialize parameters (folders and Pdf document) for next actions
4. Upload your Pdf document file
5. Perform the retreiving image Id from Pdf document using get_images() function
6. Perform the deleting image from Pdf document using delete_image() function and image Id value
7. Check result and perform some actions on calback function callback_delete_image() with response object

All values of variables starting with "YOUR_****" should be replaced by real user values
"""


import os
import json

import asposepdfcloud
from asposepdfcloud.apis.pdf_api import PdfApi

def uploadFile(pdf_api, name, temp_folder, data_path):
pdf_api.upload_file(temp_folder + '/' + name, data_path + name)

def callback_delete_image(response):
print("Attachment successfully deleted: ", response)

json_file = open('YOUR_APPLICATION_CREDENTIALS.json')

data = json.load(json_file)

pdf_api_client = asposepdfcloud.api_client.ApiClient(
app_key=str(data.get('AppKey', '')),
app_sid=str(data.get('AppSID', '')),
host=str(data['ProductUri']),
self_host=bool(data.get('SelfHost', False)),
)

api = PdfApi(pdf_api_client)

output_path = str(data['YOUR_OUTPUT_FOLDER'])
temp_folder = 'YOUR_TEMP_PDF_CLOUD_FOLDER'

file_name = 'YOUR_PDF_DOCUMENT.pdf'

uploadFile(api, file_name, temp_folder, output_path)

opts = {
"folder" : temp_folder
}

page_number = 1
responseImages = api.get_images(file_name, page_number, **opts)

image_id = responseImages.images.list[0].id

api.delete_image(file_name, image_id, callback=callback_delete_image)
Loading