2012-02-04 22 views
1

基本方案是我需要以編程方式填寫駐留在Web服務器上的PDF文本域。這些字段將被映射並填寫CSV中包含的數據。 PDF必須在瀏覽器(瀏覽器控件或ie/ff/chrome/etc)中打開並進行編輯。無法下載,填寫和上傳(必須填寫並使用提交按鈕提交;我嘗試編輯按鈕提交路徑無濟於事)。以編程方式在瀏覽器控件中填充PDF表單域

到目前爲止,我已經把web瀏覽器控件的窗體上,並使其導航到該網站,登錄,並加載PDF文件。如何與在Web瀏覽器控件中打開的PDF文件進行交互?通過查看各種PDF庫,他們似乎主要與位於硬盤上的封閉式PDF進行交互,進行修改並重新保存。

編輯:我很開放的替代解決方案。我不知道它是否可能,但如果是這樣的話 - 我的機器上基於PDF的JavaScript,我在表單上運行?如果我下載它,我可以輕鬆地做到這一點,但似乎無法在Web瀏覽器中打開時使用PDFJS。

回答

1

恐怕是不容易做到的,你想做的事情。首先,您必須找到嵌入在WebBrowser控件中的PDF閱讀器的窗口句柄。這裏是如何做到這一點的示例代碼:

Public Function GetPdfViewerHandle() As System.IntPtr 
    Dim tempHandle As System.IntPtr 
    '-------------------------------------- 
    ' get handle to pdf viewer 
    '-------------------------------------- 
    '-------------------------------------- 
    ' first check for the foxit reader 
    '-------------------------------------- 
    tempHandle = FindChildWindow(WebBrowser1.Handle, "AfxWnd42s", "Reader", 1, True) 
    If IntPtr.Zero.Equals(tempHandle) = True Then 
     '--------------------------------- 
     ' if not foxit, check for adobe 
     '--------------------------------- 
     tempHandle = FindChildWindow(WebBrowser1.Handle, "AVL_AVVIEW", "AVPageView", 1, True) 
    End If 

    Return tempHandle 

End Function 

Public Shared Function FindChildWindow(ByVal hParent As IntPtr, ByVal P_childClass As String, ByVal P_childTitle As String, ByVal P_count As Integer, ByVal p_recursive As Boolean) As IntPtr 
    Dim hChild As IntPtr 
    Dim className As String 
    Dim title As String 
    Dim cnt As Integer 
    Dim tempPtr As IntPtr 
    Dim Declare Function FindWindowExA Lib "user32.dll" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr 

    cnt = 0 

    hChild = FindWindowExA(hParent, 0, Nothing, Nothing) 

    While hChild.ToInt32 > 0 

     If P_childClass Is Nothing Then 
      className = GetClassName(hChild) 
     Else 
      className = GetClassName(hChild) 
      If P_childClass.Length < className.Length Then 
       className = className.Substring(0, P_childClass.Length) 
      End If 
     End If 

     If P_childTitle Is Nothing Then 
      title = GetWindowText(hChild).Replace("&", "") 
     Else 
      title = GetWindowText(hChild).Replace("&", "") 

      If P_childTitle.Length < title.Length Then 
       title = title.Substring(0, P_childTitle.Length) 
      End If 
     End If 


     Debug.WriteLine("hwnd=" + Hex$(hChild.ToInt32) + ", className = " + className + ", title = " + title) 
     If (String.Compare(className, P_childClass, True) = 0 And String.Compare(title, P_childTitle, True) = 0) Or (P_childClass = Nothing And String.Compare(title, P_childTitle, True) = 0) Or (String.Compare(className, P_childClass, True) = 0 And P_childTitle = Nothing) Then 
      cnt += 1 
      If cnt >= P_count Then 
       Return hChild 
      End If 
     End If 

     If p_recursive = True Then 
      tempPtr = FindChildWindow(hChild, P_childClass, P_childTitle, 1, p_recursive) 
      If IntPtr.Zero.Equals(tempPtr) = False Then 
       Return tempPtr 
      End If 
     End If 

     hChild = FindWindowExA(hParent, hChild.ToInt32, Nothing, Nothing) 
    End While 

    Return Nothing 

    End Function 

一旦你有窗口句柄,有很多不同的方法來找到表單字段。如果你知道的事物的秩序,你可以簡單地開始發送鍵命令到PDF閱讀器手柄或使用間諜++找到通過WIN32API SendMessageA功能表單字段將數據輸入到他們的句柄:

 Public Declare Function SendMessageA Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer 

        asciiChar = CByte(Asc(data.Substring(0, 1))) 
       rc = SendMessageA(hwnd, WM_CHAR, asciiChar, 0)  

好運氣。

+0

我應該補充,表單的佈局將始終是相同的,我也已經有文本框的名稱(感謝iText的#) – Cloud007 2012-02-04 21:13:32

+0

謝謝!這正是我需要的。 – 2013-05-02 18:49:25

0

如果你必須與對PDF按鈕提交數據只是檢查submited交通,看看它發送,那麼你可以用VB.NET複製,也甚至不會加載PDF文檔。

相關問題