Creating PDF files dynamically with PowerShell

So how does one go about creating PDF files dynamically? You need a pinch of the open source PdfSharp library and 10 lines of PowerShell.

Add-Type -Path .\PdfSharp.dll
$doc = New-Object PdfSharp.Pdf.PdfDocument
$doc.Info.Title = "Created dynamically"
$page = $doc.AddPage()
$gfx = [PdfSharp.Drawing.XGraphics]::FromPdfPage($page)
$font = New-Object PdfSharp.Drawing.XFont("Verdana", 20, [PdfSharp.Drawing.XFontStyle]::BoldItalic)
$msg = "Hello World"
$rect = New-Object PdfSharp.Drawing.XRect(0,0,$page.Width, $page.Height)
$gfx.DrawString($msg, $font, [PdfSharp.Drawing.XBrushes]::Black, $rect, [PdfSharp.Drawing.XStringFormats]::Center)
$doc.Save("HelloWorld.pdf")
  • http://www.friv4game.net/ Friv 4

    i like it

  • http://www.friv4game.net/ Friv 4

    thanks for share

  • couponshawker

    Very useful.

  • http://www.friv2friv3friv4.com/ friv 2 friv 3 friv 4

    thank for sharing…..

  • http://www.kizi1.org/ Kizi 1

    That’s what I was looking for, it’s very useful for my job

  • Davide

    Hello, did you know how to apply SecuritySettings to PDF, like PermitPrint or PermitModifyDocument (.. from PDFSharp.dll)?