Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hello,
We are trying to implement itextsharp to fill up pdf forms for our software.
We are having issues that its working fine if we run it form the same machine we created the source code on it.
but when we try it on another machine it creates the pdf but does not fill it up.

What I have tried:

Imports ADODB
Imports System.Text
Imports System.Runtime.InteropServices
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
Imports System.IO

Public Class mainForm
    Private hWnd As Long = Me.Handle
    Private conn As New ADODB.Connection
    Private rs As New ADODB.Recordset
    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        Dim bOk As Boolean : bOk = False
        conn.Provider = "Microsoft.Jet.OLEDB.4.0"
        conn.CursorLocation = CursorLocationEnum.adUseClient
        conn.Open(GetFullDataPath)
        Dim pdfMainForm As String = "C:\test\FORM1.TXT"
        Dim pdfFile As String = "c:\test\reg343.pdf"
        Dim pdfFileTo As String

        pdfMainForm = Environment.GetCommandLineArgs(1)
        pdfFile = Environment.GetCommandLineArgs(2)

        Dim frmTitle As String = pdfFile ' _left(pdfMainForm, Len(pdfMainForm) - 4) & Format(Now(), "_yyyy-mm-dd_HHMM") & ".pdf" '"testing"
        Dim fromSql As String = " FROM  Vendors AS Lender RIGHT JOIN (Customer AS Spouse RIGHT JOIN (Customer AS Cobuyer RIGHT JOIN (Vendors INNER JOIN (Sale INNER JOIN (Customer INNER JOIN (Vehicle INNER JOIN Deal ON Vehicle.recNum = Deal.carRecNum) ON Customer.RecNum = Deal.buyerRecNum) ON Sale.recNum = Deal.saleRecNum) ON Vendors.recNum = Vehicle.vendorRecNum) ON Cobuyer.RecNum = Deal.coBuyerRecNum) ON Spouse.RecNum = Deal.spouseRecNum) ON Lender.recNum = Deal.lenderRecNum"


        Dim formApp As PdfReader = New PdfReader(pdfFile)
        pdfFileTo = _left(pdfFile, Len(pdfFile) - 4) & Format(Now(), "_yyyy-mm-dd_HHMM") & ".pdf"
        Dim pdfStamper As New PdfStamper(formApp, New FileStream(pdfFileTo, FileMode.Create))

        Dim acroForm As AcroFields
        acroForm = pdfStamper.AcroFields
        Dim xList As String
        Dim sql As String
        Dim fldName As String
        xList = ""
        Dim field As New DictionaryEntry
        For Each field In acroForm.Fields
            fldName = GetDataPath("Forms Fields", field.Key.ToString(), "", pdfMainForm)
            If fldName <> "" Then
                xList = xList & fldName & ","
            End If
        Next

        sql = " SELECT " & xList & "Deal.recnum "
        sql = sql + fromSql
        sql = sql + " where Deal.recNum = " & Environment.GetCommandLineArgs(3)
        rs.Open(sql, conn, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic)
        If rs.RecordCount > 0 Then
            rs.MoveFirst()
            For Each field In acroForm.Fields
                fldName = GetDataPath("Forms Fields", field.Key.ToString(), "", pdfMainForm)
                If InStr(UCase(fldName), " AS ") > 0 Then
                    fldName = Trim(Mid(fldName, InStr(UCase(fldName), " AS ") + 4, Len(fldName) - InStr(UCase(fldName), " AS ") + 4))
                End If
                If fldName <> "" Then
                    'field.Value = rs.Fields(fldName).Value & ""
                    acroForm.SetField(field.Key.ToString(), rs.Fields(fldName).Value & "")
                End If
            Next
        End If
        xList = ""

        sql = " SELECT * from DealerTemp "
        If rs.State > 0 Then rs.Close()
        rs.Open(sql, conn, CursorTypeEnum.adOpenDynamic, LockTypeEnum.adLockOptimistic)
        If rs.RecordCount > 0 Then
            rs.MoveFirst()
            For Each field In acroForm.Fields
                fldName = GetDataPath("DealerTemp", field.Key.ToString(), "", pdfMainForm)
                If InStr(UCase(fldName), " AS ") > 0 Then
                    fldName = Trim(Mid(fldName, InStr(UCase(fldName), " AS ") + 4, Len(fldName) - InStr(UCase(fldName), " AS ") + 4))
                End If
                If fldName <> "" Then
                    '                    field.Value = rs.Fields(fldName).Value & ""
                    acroForm.SetField(field.Key.ToString(), rs.Fields(fldName).Value & "")
                End If
            Next
        End If
        '        pddoc = avDoc.GetPDDoc
        '        pddoc = avDoc.GetPDDoc
        '        bOk = pddoc.Save(1, _left(pdfFile, Len(pdfFile) - 4) & Format(Now(), "_yyyy-mm-dd_HHMM") & ".pdf")
        pdfStamper.FormFlattening = True
        pdfStamper.Close()
        SetFileAttributes(pdfFileTo, vbReadOnly)
        ShellExecute(hWnd, "Open", pdfFileTo, "", "", 0)
        End
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
Posted
Comments
Richard MacCutchan 7-Mar-18 5:08am    
You need to gather more details and add them to your question. We cannot guess what happens when you try to run this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900