Archive

Archive for the ‘.NET’ Category

Scripting PDF Documents through PowerShell

September 8th, 2009 Niklas Goude 3 comments

Microsoft Office Supports Scripting possibilities through Com Objects. But what about PDF Documents ? Luckily there is a DLL available for download that let’s us Create PDF documents through .NET

First you have to Download the ITextSHarp.dll

Next, Time for some PowerShell Magic.

Step One: Load the DLL.


PS > [System.Reflection.Assembly]::LoadFrom("C:\itextsharp\itextsharp.dll")

Step Two: Create a Variable holding the iTextSharp.text.Document Object


PS > $Doc = New-Object iTextSharp.text.Document

Step Three: Create a PDF Document with a connection to the $Doc variable


PS > [void][iTextSharp.text.pdf.PdfWriter]::GetInstance($Doc, [System.IO.File]::Create("C:\PDF\Goude.pdf") )

Step Four: Open The Document ( In order to add information to the PDF File )


PS > $Doc.Open()

Step Five: Create a Text Chunk and append Text to Display in Pdf Document


PS > $Chunk = New-Object iTextSharp.text.Chunk
PS > [void]$Chunk.Append(“Demo From PowerShell.nu”)

Step Six: Add the Chunk of Text, Set an Author with the AddAuthor() method and finally Close it with the Close() method


PS > $Doc.Add($Chunk)
PS > $Doc.AddAuthor("Niklas Goude")
PS > $Doc.Close()

Voila, you now have a PowerShell made PDF File. You can, of course, create far more advanced PDF files containing tons of information. I’ll get back to this in a later Post.

Rating 3.00 out of 5
[?]
Categories: .NET Tags: