Images by Daniel Goude... Refresh for more!
Scripting PDF Documents through PowerShell
This is my site Written by Niklas Goude on September 8, 2009 – 12:06 pm

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.

Posted in  

Tags:

2 Responses »

  1. Is there a way I can set the font size, font face, and document margins? If there is also a reference you could point me to that you think would be helpful, that would be great.

    Thank you.

  2. Hi. here’s a great reference site with great tutorials on itextsharp.
    http://itextsharp.sourceforge.net/tutorial/index.html

    hope this helps.

Leave a Reply

Spam Protection by WP-SpamFree