Powershell and iTextsharp add multiple images to PDF -
i'm trying use itextsharp (could change pdfsharp if better option) in powershell make pdf out of images. i've managed create pdf-file 1 image don't know how create 1 images in folder.
as these images in scale fit pdf perfectly, set scale fills page 100%. possible?
i'm not experienced powershell-user got far:
[system.reflection.assembly]::loadfrom("c:\temp\itextsharp.dll") $doc = new-object itextsharp.text.document $filestream = new-object io.filestream("c:\temp\output5.pdf", [system.io.filemode]::create) [itextsharp.text.pdf.pdfwriter]::getinstance($doc, $filestream) $jpg = [itextsharp.text.image]::getinstance( "c:\temp\horse.jpg" ) $doc.open() $doc.add($jpg); $doc.close()
if has idea, please let me know, thanks.
you'll want use get-childitem
of images in given folder. you'll want use foreach-object
(sometimes shortened foreach
) on images calling $doc.add()
right before calling $doc.newpage()
.
the below code shows off. 1 common request have each page sized fit image i've added that, too. instantiate system.drawing.bitmap
each image dimensions, create itextsharp rectangle
dimensions , use set page's size via $doc.setpagesize()
.
i've moved of variable top make things easier, you'll want update them match needs. comments should rest of way.
## set various paths $itextsharpfilepath = "d:\dlls\itextsharp.dll" $imagefolderpath = "d:\images" $pdffilepath = "d:\temp.pdf" ## load itextsharp , system.drawing [system.reflection.assembly]::loadfrom($itextsharpfilepath) [system.reflection.assembly]::loadwithpartialname("system.drawing") ## of images in folder ## change filter if needed $images = get-childitem $imagefolderpath -filter *.png ## create our stream, document , bind writer $filestream = new-object system.io.filestream($pdffilepath, [system.io.filemode]::create) $doc = new-object itextsharp.text.document $writer = [itextsharp.text.pdf.pdfwriter]::getinstance($doc, $filestream) ## open document writing $doc.open() ## remove document margins $doc.setmargins(0, 0, 0, 0) ## loop through each image in folder foreach($image in $images) { ## create .net image can image dimensions $bmp = new-object system.drawing.bitmap($image.fullname) ## create itextsharp rectangle corresponds dimensions $rect = new-object itextsharp.text.rectangle($bmp.width, $bmp.height) ## set next page size dimensions , add new page $doc.setpagesize( $rect ) $doc.newpage() ## add our image page $doc.add([itextsharp.text.image]::getinstance( $image.fullname )); ## cleanup $bmp.dispose() } ## cleanup $doc.close() $doc.dispose() $writer.dispose()
Comments
Post a Comment