PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Write to PDF with Powershell Function
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2912
Page 1 of 1

Author:  litu [ Mon Aug 25, 2014 3:01 pm ]
Post subject:  Write to PDF with Powershell Function

Hi@all,
first: thanks for this nice tool. very userfull and for me (i'm not a pro-coder) good/easy to use.

i want to implement a PDF-creating-routine into a posh-script.
My promblem at the moment: i can't get the "gfx.drawstring" to work in a function.
Without the "fill"-function (see the code) it works, so i thought it was a scope-problem, but "global/script"-Variable doesn't work either.

EDIT: the error is written below the code

Can someone help me, please. I'm thankfull for hints or solutions

EDIT: sorry, i forgot: its about PDFSharp / Ver 1.32.2608.0 / GDI+

(it's not the final script, just testing at the moment)

Code:
[reflection.assembly]::loadfrom("c:\pdfSharp.dll") | out-null

$filename="c:\temp\_test.pdf"
$text= "TEST test TEST test"

$options = new-object PdfSharp.Drawing.XPdfFontOptions([PdfSharp.Pdf.PdfFontEncoding]"Unicode", [PdfSharp.Pdf.PdfFontEmbedding]"Always")
$font = new-object PdfSharp.Drawing.XFont("Arial", 12, [PdfSharp.Drawing.XFontStyle]"regular", $options)
$PdfReader = [PdfSharp.Pdf.IO.PdfReader]           
$PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode] 
$input2=new-object pdfsharp.pdf.pdfdocument
$page=$input2.addpage()
# use existing pdf:
# $input2=$PdfReader::Open("c:\test.pdf", $PdfCodumentOpenMode::import)
# $page=$input2.pages[0]

$gfx = [PdfSharp.Drawing.XGraphics]::FromPdfPage($page)


function fill ($text="bla",[int]$x=20,[int]$y=20){
   process{

$script:gfx.DrawString($text,$script:font,[PdfSharp.Drawing.XBrushes]::Black,(new-object PdfSharp.Drawing.XRect( $x, $y, $script:page.Width, $script:page.Height) ),[PdfSharp.Drawing.XStringFormats]::Center)

   }
}

$input2.Save($filename )



   . fill -text $env:computername -x "-130" -y "-315"
   . fill -text $((gp HKLM:\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters).srvcomment) -x "-130" -y "-310"




Code:

Ausnahme beim Aufrufen von "DrawString" mit 5 Argument(en):  "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."
Bei P:\cod\func\pdf_create.ps1:54 Zeichen:23
+ $script:gfx.DrawString <<<< ($text,$script:font,[PdfSharp.Drawing.XBrushes]::Black,(new-object PdfSharp.Drawing.XRect( $x, $y, $script:page.Width, $script:page.Height) ),[PdfSharp.Drawing.XStringFormats]::Center)
DrawString(
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException


-----
in english sth. like:
Exception when calling "DrawString" with 5 Arguments:  Object reference not set to an instance of an object.


Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/