簡體   English   中英

用XDP填充PDF

[英]Populating PDF with XDP

我有一個用於使用簡單XFDF文件填充的PDF填充文件。 使用VS.NET 2010,我閱讀了XFDF文檔,並使用ds.WriteXML(XFDFName)填充了所有必要的信息並填充了PDF。 XFDF通過Process.Start(XFDFName)編寫並啟動。 這一切都在WinForms應用程序中。 幾年來,這種方法一直是冠軍。 到現在...

我遇到的問題是由於在Adobe LiveCycle中創建了文件,因此無法將數據導出為XFDF格式。 我注意到導出選項是(1)XML或(2)XDP。 過去,我可以導出到XFDF。 我認為沒什么大不了的,只是另一種格式。 但是,我一直在為這兩種選擇而苦苦掙扎。 經過深思熟慮后,我決定使用XDP格式。

我已經用所需的所有信息構建了數據集,使用Process.Start(XDPName)打開新創建的XDP時收到錯誤。 Reader打開,出現以下錯誤: “ Adob​​e Reader無法打開'GUID_HERE.xdp',因為它不是受支持的文件類型,或者由於文件已損壞(例如,它作為電子郵件附件發送,並且沒有正確解碼)。”

我嘗試使用直接指向PDF的href,但這也不起作用。 因此,我選擇在該部分中將序列化的PDF保留在XDP中。

這個XDP文件如下所示( 感謝Dean J ):

<?xml version='1.0' encoding='UTF-8'?>
<?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>
<xdp:xdp xmlns:xdp='http://ns.adobe.com/xdp/'>
    <xfa:datasets xmlns:xfa='http://www.xfa.org/schema/xfa-data/1.0/'>
        <xfa:data>
           XML is here - matching the dynamic fields in the PDF.
        </xfa:data>
    </xfa:datasets>
    <pdf xmlns=\"http://ns.adobe.com/xdp/pdf/\"><document>
       <chunk>
          Base64 encoded PDF
       </chunk>
    </document></pdf>
</xdp:xdp>

我認為我正在生成的XDP是偽造的-但進一步使事情復雜化-如果我打開Reader,請單擊工具>表單>更多表單選項>管理表單數據>導入數據,然后選擇我生成的XDP文件,所有字段按我期望的那樣填充。

因此,基本上,顯然某個地方存在斷開連接:我有一個XDP文件,其中包含我需要的所有信息。 我有一個PDF表單,需要用XDP文件填充。 XDP中的信息正確匹配PDF中的所有控件名稱。 但是當我啟動XDP文件時,Reader告訴我它已損壞/不受支持。 據我了解,啟動XDP文件時,應使用Reader正確啟動/填充文件,對嗎?

任何信息都會極大地幫助我。 謝謝。

我在完成此操作時也遇到問題。

在VB.net中做類似的事情

使用塊時似乎不起作用,但是如果我使用href和本地文件,它確實起作用...

測試例

Public Sub BuildContent(ByVal slno As String)

    Dim strXML As String

    Dim fs As System.IO.FileStream = Nothing
    Dim bw As System.IO.BinaryWriter = Nothing
    Dim Buffer() As Byte
    'fs = New System.IO.FileStream("kpiAlert10.pdf", IO.FileMode.Create)
    'bw = New System.IO.BinaryWriter(fs)
    'Response.ContentType = "application/vnd.adobe.xdp+xml"
    '
    ' Constant XPD Header
    '
    strXML = "<?xml version='1.0' encoding='UTF-8'?>"
    strXML = strXML & "<?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>"
    strXML = strXML & "<xdp:xdp xmlns:xdp='http://ns.adobe.com/xdp/'>"
    strXML = strXML & "<xfa:datasets xmlns:xfa='http://www.xfa.org/schema/xfa-data/1.0/'>"
    strXML = strXML & "<xfa:data>"
    '
    ' Place code here to get the current Logged in user ID
    ' and perform a query to the back end database and filter by ID
    ' then generate the following XML Data using the resultant Recordset ...etc.
    '

    strXML = strXML & "<transaction><kpi><name>Ego ille</name><data><sn>Si manu vacuas</sn><amt>Apros tres et quidem</amt><delta>Mirum est</delta></data></kpi></transaction>"
    '
    '
    '
    strXML = strXML & "</xfa:data>"
    strXML = strXML & "</xfa:datasets>"
    '
    ' Point the XPD to the PDF Form created under Adobe LiveCycle Desinger
    '
    Dim contents As String

    contents = EncodeFile("kpiAlert.pdf")
    'Buffer = Convert.FromBase64String(contents)
    strXML = strXML & "<pdf xmlns='http://ns.adobe.com/xdp/pdf/'>"
    strXML = strXML & "<document>"
    strXML = strXML & "<chunk>" & contents & "=</chunk>"
    strXML = strXML & "</document>"
    strXML = strXML & "</pdf>"

    'strXML = strXML & "<pdf href='C:/kpiAlert.pdf' xmlns='http://ns.adobe.com/xdp/pdf/' />"
    '
    ' Close the XPD File
    '
    strXML = strXML & "</xdp:xdp>"

    Using outfile As New StreamWriter("kpiAlert_" & slno & ".pdf")
        outfile.Write(strXML.ToString())
    End Using

End Sub

Function EncodeFile(ByVal srcFile As String) As String

    Dim srcBT As Byte()
    Dim dest As String
    Dim sr As New IO.FileStream(srcFile, IO.FileMode.Open)
    ReDim srcBT(sr.Length)
    sr.Read(srcBT, 0, sr.Length)
    sr.Close()
    dest = EncodeByte(srcBT)
    Return dest

End Function

enter code here

函數EncodeByte(ByVal bt()作為字節)作為字符串Dim enc作為字符串enc = System.Convert.ToBase64String(bt)返回enc結束函數

我還要感謝Dean J的出色回答。 我以前使用過它,它比商業iText許可證或LiveCycle服務器節省了很多時間和金錢。 在引用的代碼中,有一個轉義的字符串:

<pdf xmlns=\"http://ns.adobe.com/xdp/pdf/\">

注意雙引號前的反斜杠。 那些不應包含在您的XML中,因為它將使其無效。 可能Dean J實際上具有C#或PHP中的所有代碼,因此需要為他轉義引號。

不知道這是否相關,但是在不同情況下在VBA中處理合並子例程時,無法打開XDP文件。 有時它會起作用,而其他時候則不會。

最終,我注意到當Adobe Acrobat(正確-不是Reader)已打開或設置為PDF的默認值時發生了這種情況,並且出於某種原因試圖打開XDP而不是Adobe Reader。

以下XML對我有用:

<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
  <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
    <xfa:data>
      <PlannedCycles>999</PlannedCycles>
      <cyclenumber>1</cyclenumber>
      <ConsultantName>Dr Jonathon Hogan-Doran</ConsultantName>
      <Name>SMITH, Bob</Name>
      <URN>9795240</URN>
      <DOB>14/04/1901</DOB>
      <ward>CDCO</ward>
      <ht>100</ht>
      <wt>99</wt>
      <Diagnosis>Metastatic Adenocarcinoma</Diagnosis>
      <chemoD1>17/06/2015</chemoD1>
    </xfa:data>
  </xfa:datasets>
  <pdf xmlns="http://ns.adobe.com/xdp/pdf/" href="\\xxxxxxx.gov.au\Medical Oncology\Chemotherapy Scripts\S\SMITH, Bob- (75240) - dob 14.04.1901 - Capecitabine with Bevacizumab - Cycle 1.pdf"/>
</xdp:xdp>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM