Create Image
Creates a snapshot of a specific PDF page and saves it as a picture.
Code Samples
Try the API in the language you prefer
- C#
- Java
- JavaScript
- PHP
- Python
- Ruby
// create createImages object
var createImages = new CreateImages()
{
// document
Document = new Document()
{
DocData = File.ReadAllBytes("myPdf.pdf"),
Name = "myPdf.pdf",
},
// action
ImageAction = new ImageAction()
{
ImageExtension = ImageActionImageExtension.Jpeg,
WidthPixel = 4000,
PageSelection = new PageSelection()
{
PageNrs = new System.Collections.Generic.HashSet() { 1, 2, 3 }
}
},
};
// create image
var res = await Pdf4meClient.Pdf4me.Instance.ImageClient.CreateImagesAsync(createImages);
// extract thumbnail and writing it to disk
foreach (var page in res.Document.Pages)
File.WriteAllBytes("thumbnail_" + page.PageNumber + "+.jpg", page.Thumbnail);
// setup the imageClient
ImageClient imageClient = new ImageClient(pdf4meClient);
// create createImages object
CreateImages createImages = new CreateImages();
// document
Document document = new Document();
byte[] file = Files.readAllBytes(Paths.get("myPdf.pdf"));
document.setDocData(file);
createImages.setDocument(document);
// action
ImageAction imageAction = new ImageAction();
imageAction.setWidthPixel(4000);
imageAction.setPageSelection(new PageSelection().addPageNrsItem(1));
imageAction.setImageExtension(ImageExtensionEnum.JPEG);
createImages.setImageAction(imageAction);
// create image
CreateImagesRes res = imageClient.createImages(createImages);
// extracting the generated picture and writing it to disk
byte[] thumbnail = res.getDocument().getPages().get(0).getThumbnail();
FileUtils.writeByteArrayToFile(new File("thumbnail.jpg"), thumbnail);
// setup the pdf4meClient
const pdf4meClient = pdf4me.createClient('YOUR API KEY')
// create the createImages object
const createImagesReq = {
// document
document: {
docData: fs.readFileSync(path.join(__dirname, 'myPdf.pdf')).toString('base64'),
},
// action
imageAction: {
pageSelection: {
pageNrs: [1, 2, 3],
},
imageQuality: 99,
widthPixel: 2000,
heightPixel: 2000,
imageExtension: 'Jpeg',
},
}
// create images
pdf4meClient.createImages(createImagesReq)
.then(createImagesRes => {
// extract thumbnails and writing it to disk
createImagesRes.document.pages.forEach((page, index) => {
const image = Buffer.from(page.thumbnail, 'base64')
fs.writeFileSync(path.join(__dirname, `thumbnail_${index + 1}.jpg`), image)
})
})
.catch(error => {
console.error(error)
})
# setup the image_client
image_client = ImageClient(pdf4me_client)
# create the create_images object
create_images = CreateImages(
document=Document(
doc_data=FileReader().get_file_data('myPdf.pdf')
),
image_action=ImageAction(
width_pixel=4000,
page_selection=PageSelection(
page_nrs=[1, 2, 3]
),
image_extension='Jpeg'
)
)
# image creation
res = image_client.create_images(create_images=create_images)
for page in res['document']['pages']:
# extracting the generated image
image = base64.b64decode(page['thumbnail'])
# writing it to disk
with open('thumbnail_'+str(page['page_number'])+'.jpg', 'wb') as f:
f.write(image)
$res = $client->pdf4me()->createImages([
// document
"document" => [
'docData' => $client->getFileData('myPdf.pdf')
],
//action
"imageAction" => [
"widthPixel" => 4000,
"pageSelection" => [
"pageNrs" => [ 1, 2, 3 ]
],
"imageExtension" => "Jpeg"
]
]);
foreach($res->document->pages as $page){
// extract thumbnails and writing it to disk
$image = base64_decode($page->thumbnail);
file_put_contents( 'thumbnail_'.$page->pageNumber.'.jpg', $image);
}
file_path = 'myPdf.pdf'
action = Pdf4me::CreateImages.new(
document: Pdf4me::Document.new(
doc_data: Base64.encode64(File.open(file_path, 'rb', &:read))
),
image_action: Pdf4me::ImageAction.new(
page_selection: Pdf4me::PageSelection.new(
page_nrs: [1, 2, 3]
),
image_extension: 'Jpeg',
center: true,
fit_page: true,
bits_per_pixel: 24,
bilevel_threshold: 181,
render_options: %w(noAntialiasing),
rotate_mode: 'none',
preserve_aspect_ratio: true,
compression: 'raw'
)
)
response = action.run
# saving the extracted thumbnail
File.open('./out/thumbnail.jpg', 'wb') do |f|
f.write(Base64.decode64(response.document.pages.first.thumbnail))
end