Home > .NET > Scripting PDF Documents through PowerShell

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
[?]
Categories: .NET Tags:
  1. psuser
    February 2nd, 2010 at 00:58 | #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. February 3rd, 2010 at 21:58 | #2

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

    hope this helps.

  3. muha
    May 3rd, 2010 at 18:11 | #3

    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 :)

  1. No trackbacks yet.

Spam Protection by WP-SpamFree