Scripting PDF Documents through PowerShell

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
[?]

7 thoughts on “Scripting PDF Documents through PowerShell

  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. It works :)

    But let’s say the variabel $Chunk consist of multiple lines of text – how to implement newline? My result now is that all text is on the same line and makes it useless to read. And what if I want to set fontsize, and make headlines with bold text?

    And finally – how can I make the pdffile read-only so that no one can edit the document?

    Thank you – great post :)

  3. I’m trying to load an iTextSharp.text.Image but I keep getting ‘unable to find appropriate constructor’ error.

    Thanks for the post, it’s already helped a lot.

  4. You never did a followup on this as you wrote?
    I just found iTextSharp and it seems to be what I need, I am planning to use it to beautify a csv in utf8 format for a daily update of some important information, so I guess pdfTable something something :D

    I will look for some docs on the net, but they seem to be pointing to the book all of them…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Protection by WP-SpamFree