I am generating a document using powershell. I need to insert some PDF files and some TIFF images in the document contents. It only inserts the first page of TIF or PDF file in the page.
I am using inlineShapes.AddPicture or inlineShapes.AddOLEObject to add these to the document.
How can we add a iterator or something similar to insert a page at a time in the document object?
Most answers mention that we need to download or install a utility such as iTextSharp for splitting or some imaging utility to split the images. However, after some more searches, I was able to implement this as follows:
For Images: Use built-in imaging library in Windows to split the Tiff files into pages and then insert in word each file separately.
For PDF: Open PDF in Word and save as word. Then insert the word file in the document.
PDF Code Sample
$word=New-Object -ComObject word.application
$word.visible=$false
$doc=$word.documents.add()
$sel = $word.selection
$sel.typetext ("Just some test text")
$sel.typeParagraph()
$newDoc=$word.documents.open("c:\apps\sample.pdf")
$filebasename = [System.IO.Path]::GetFileNameWithoutExtension("c:\apps\sample.pdf")
$newDoc.saveAs("c:\apps\sample.docx")
$newDoc.close()
$doc.activate()
$rng=$word.activedocument.range()
$rng.insertFile("c:\apps\sample.docx")
$doc.saveas("C:\apps\new.docx")
$doc.close()
$word.quit()
First time execution of above code, will display a dialog box which asks if you want to convert the PDF to word. It will have a checkbox to skip this dialog next time. We need to check that box so that it doesn't appear next time. This can also be done through adding a registry entry but that is out of scope of this answer.
Code for Images:
$a=[System.Drawing.Bitmap]]::FromFile("c:\apps\sample.tiff")
$pages=$a.getFrameCount([System.Drawing.Imaging.FrameDimension]::Page)
for ($page=0;$page -lt $pages;$page++) {
$a.selectActiveFrame([System.Drawing.Imaging.FrameDimension]::Page,$page)
$pageFileName = "C:\apps\sample-" + $page + ".tiff"
$a.save($pageFileName)
$newPic = $sel.inlineShapes.AddPicture("$pageFileName","false","true")
}
In above code, "$sel" is a selection object set to a previously opened Word document.