locked
Keep getting exception when trying to create pdf file using iTextSharp library RRS feed

  • Question

  • I am trying to fill a PDF form with data from a SQL Server db. I got it to work at home, but I can't get it to work here at work. This function does nothing but grab data from a csv file (will incorporate SQL data later), create a new pdf file from a template, fill in the data, then return the file. I just don't get why it won't go. I'm getting an "Exception calling 'Create' with '2' argument(s): 'Could not find part of the path...'" I hope someone can see what's missing. I've tried changing the buffer size, but I don't think that's the problem.

    function Create-BuildSheet($ComputerName)
    {
    	$iTextSharpLibFullname = "X:\ircstm\PCTechs\~~TOOLBOX~~\Powershell Modules\iTextSharp\itextsharp.dll"
    	
    	[System.Reflection.Assembly]::LoadFrom($iTextSharpLibFullname)
    		
    	$templateFileFullname = "Y:\RefreshTest\BuildSheetTemplate.pdf"
    	$reader = New-Object iTextSharp.text.pdf.PdfReader($templateFileFullname)
    	
    	$outputFileFullname = ("Y:\RefreshTest\BuildSheets\BuildSheet_" + $ComputerName + ".pdf")
    	#$outputFileFullname = ("Y:\RefreshTest\BuildSheets\BuildSheet.pdf")
    	$stamper = New-Object iTextSharp.text.pdf.PdfStamper($reader, [System.IO.File]::Create($outputFileFullname))
    		
    	$buildSheetData = Import-Csv ("Y:\RefreshTest\Output\RefreshData_$ComputerName.csv")
    		
    	$stamper.AcroFields.SetField('ComputerName', "$($buildSheetData.ComputerName)")
    	$stamper.AcroFields.SetField('Autologon', "$($buildSheetData.Autologon)")
    	$stamper.AcroFields.SetField('BCA', "$($buildSheetData.BCA)")
    	#$stamper.AcroFields.SetField('NewWNum', "$($buildSheetData.NewWNum)")
    	$stamper.AcroFields.SetField('Description', "$($buildSheetData.Description)")
    	$stamper.AcroFields.SetField('Model', "$($buildSheetData.Model)")
    	$stamper.AcroFields.SetField('Site', "$($buildSheetData.Site)")
    	$stamper.AcroFields.SetField('OS', "$($buildSheetData.OS)")
    	$stamper.AcroFields.SetField('Monitor1', "$($buildSheetData.Monitor1)")
    	$stamper.AcroFields.SetField('Monitor2', "$($buildSheetData.Monitor2)")
    	$stamper.AcroFields.SetField('Printers', "$($buildSheetData.Printers)")
    	#$stamper.AcroFields.SetField('Adapter', "$($buildSheetData.Adapter)")
    	#$stamper.AcroFields.SetField('MACAddress', "$($buildSheetData.MACAddress)")
    	$stamper.AcroFields.SetField('DHCPEnabled', "$($buildSheetData.DHCPEnabled)")
    	$stamper.AcroFields.SetField('IPAddress', "$($buildSheetData.IPAddress)")
    	#$stamper.AcroFields.SetField('Gateway', "$($buildSheetData.Gateway)")
    	#$stamper.AcroFields.SetField('DNSPrimary', "$($buildSheetData.DNSPrimary)")
    	#$stamper.AcroFields.SetField('DNSSecondary', "$($buildSheetData.DNSSecondary)")
    	$stamper.AcroFields.SetField('SCCMApps', "$($buildSheetData.SCCMApps)")
    	$stamper.AcroFields.SetField('ManualApps', "$($buildSheetData.ManualApps)")
    	
    	$tcu = ($($buildSheetData.TCU_FirstName) + " " + $($buildSheetData.TCU_LastName))
    	$stamper.AcroFields.SetField('TopConsoleUser', "$($tcu)")
    	
    	$manager = ($($buildSheetData.MgrFirstName) + " " + $($buildSheetData.MgrLastName))
    	$stamper.AcroFields.SetField('Manager', "$($manager)")
    		
    	$stamper.Close()
    }

    Friday, May 26, 2017 9:33 PM

All replies

  • My guess is that you don't have a "Y" drive.  Check all of your paths.  That is what the error is telling you.


    \_(ツ)_/

    Friday, May 26, 2017 10:03 PM
  • Could it be a permissions issue?
    Saturday, May 27, 2017 2:12 AM
  • Could it be a permissions issue?

    run your function with admin privileges.

    just need to right click the powershell icon, selected run as admin...

    >>Exception calling 'Create' with '2' argument(s): 'Could not find part of the path...'"

    Besides, please check this line: 

    $outputFileFullname = ("Y:\RefreshTest\BuildSheets\BuildSheet_" + $ComputerName + ".pdf")

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Saturday, May 27, 2017 2:19 AM
  • I removed the extra quotes, but it still failed. My script didn't have a problem writing csv files to the parent drive, so I don't see how it would be a permissions issue. Unless it's something to do with the iTextSharp library. I wonder if it needs something in the command to force it to overwrite an existing file or create a new one.
    Saturday, May 27, 2017 2:29 AM
  • I just tried it again but changed the output to my C drive. It failed to even create the non-existent folder, so there must be some issue with permissions with the iTextSharp library.
    Saturday, May 27, 2017 2:34 AM
  • I just tried it again but changed the output to my C drive. It failed to even create the non-existent folder, so there must be some issue with permissions with the iTextSharp library.

    I'm agree with this: so there must be some issue with permissions with the iTextSharp library.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Hello_2018 Monday, June 5, 2017 6:39 AM
    Monday, June 5, 2017 6:38 AM