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)
Creating PDF files dynamically with PowerShell
  • Friv 4

    i like it

  • Friv 4

    thanks for share

  • couponshawker

    Very useful.

  • friv 2 friv 3 friv 4

    thank for sharing…..

  • 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)?