I have a script that imports the iTextSharp.dll and creates a single PDF out of each image in a directory and the code looks something like this:
# Load iTextSharp and System.Drawing Add-Type -Path ($iTextSharpFilePath) Add-Type -AssemblyName System.Drawing # Get all of the images in the folder $Images = Get-ChildItem -Filter *.png | Sort-Object { [int]$_.basename } # Create our stream, document and bind a writer $fileStream = New-Object System.IO.FileStream($pdfFilePath, [System.IO.FileMode]::Create) $doc = New-Object iTextSharp.text.Document $writer = [iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $filestream) # Open the document for writing $doc.Open() ## Remove all document margins $doc.SetMargins(0, 0, 0, 0) # Loop through each image in the folder foreach($image in $images) { # Create a .Net image so that we can get the image dimensions $bmp = New-Object System.Drawing.Bitmap($image.FullName) ##Create an iTextSharp rectangle that corresponds to those dimensions $rect = New-Object iTextSharp.text.Rectangle($bmp.Width, $bmp.Height) # Set the next page size to those dimensions and add a new page $doc.SetPageSize( $rect ) $doc.NewPage() # Add our image to the page $doc.Add([iTextSharp.text.Image]::GetInstance( $image.FullName )) ##Cleanup $bmp.Dispose() } # Cleanup $doc.Close() $doc.Dispose() $writer.Dispose()
The script works as you’d expect, although for some reason, when the script runs, it outputs ‘True’ 31 times (even though the PDF is only 10 pages/images).
Is there anything I can change to stop that from happening?
submitted by /u/not-so-dubious
[link] [comments]
The post Importing iTextSharp into a script keeps printing ‘True’ appeared first on How to Code .NET.