Thursday, May 28, 2009

PDFs with Powershell and PDF Sharp

I found this script today on the internet searching as I do everyday for anything new with powershell. I was searching to see if itextsharp could be used to create PDFs with Powershell and stumble onto the script below that shows how to do it with PDF Sharp.

http://www.pdfsharp.net

Code:

param($text="Hello World",$fileName="c:\test.pdf")

[reflection.assembly]::loadfrom("C:\Downloads\Powershell\PDFSharp\bin\PdfSharp.dll") | out-null

# Create a new PDF document
$document = new-object PdfSharp.Pdf.PdfDocument

# Create an empty page
$page = $document.AddPage()

# Get an XGraphics object for drawing
$gfx = [PdfSharp.Drawing.XGraphics]::FromPdfPage($page)

$options = new-object PdfSharp.Drawing.XPdfFontOptions([PdfSharp.Pdf.PdfFontEncoding]"Unicode", [PdfSharp.Pdf.PdfFontEmbedding]"Always")

# Create a font
$font = new-object PdfSharp.Drawing.XFont("Arial", 20, [PdfSharp.Drawing.XFontStyle]"Bold", $options)

# Draw the text
$gfx.DrawString(
$text,
$font,
[PdfSharp.Drawing.XBrushes]::Black,
(new-object PdfSharp.Drawing.XRect(0, 0, $page.Width, $page.Height) ),
[PdfSharp.Drawing.XStringFormats]::Center)

# Save the document...
$document.Save( $filename )

No comments:

Post a Comment