2011-10-06 94 views
0

我有一個MVC3應用程序(C#,剃鬚刀),其中有一個對象,其中包含我需要的動態PDF報告所需的所有數據用戶瀏覽器。我的公司VS2010解決方案已經引用了iTextSharp.dll v 5.0.5,並在其他地方用於在靜態PDF表單上「標記」值。如何使用iTextSharp(或其他方法)在C#中生成動態PDF

我爲iTextSharp找到的所有例子都是使用壓模(簡單)進行靜態PDF,或者他們使用4.1.6,他們利用iTextHandler。 ITextHandler不在v 5.0.5中。我嘗試使用HTMLWorker沒有運氣。我的代碼如下

3種方式做動態PDF

首選方案: 1.我要綁定到一個動態的PDF表單。動態的,我的意思是能夠做重複的子表單等。我有一個PDF,我在Adobe LifeCycle ES2中創建並保存爲XLA。我看到所有的Adobe Docs都提到了與XML等進行連接,但沒有例子說明如何真正實現這一點。我知道這並不難。在C#中的例子好嗎?

可選的解決方案: 2.使用什麼我現在(工程W/iTextSharp.dll 4.1.6只)使用

可選的解決方案: 3.生成HTML和開始尋找HTML爲PDF方法

選項2代碼: 該代碼是在控制器類:

/// <summary> 
    /// Returns a PDF action result. This method renders the view to a string then 
    /// use that string to generate a PDF file. The generated PDF file is then 
    /// returned to the browser as binary content. The view associated with this 
    /// action should render an XML compatible with iTextSharp xml format. 
    /// </summary> 
    /// <param name="model">The model to send to the view.</param> 
    /// <returns>The resulted BinaryContentResult.</returns> 
    protected ActionResult ViewPdf(object model) 
    { 
     // Create the iTextSharp document. 
     Document doc = new Document(); 
     // Set the document to write to memory. 
     MemoryStream memStream = new MemoryStream(); 
     PdfWriter writer = PdfWriter.GetInstance(doc, memStream); 
     writer.CloseStream = false; 
     doc.Open(); 

     //// Render the view xml to a string, then parse that string into an XML dom. 
     string xmltext = this.RenderActionResultToString(this.View(model)); 

     #region This code works with iTextSharp version 4.1.6.0 (free version of iTextSharp) 
     /* 
     XmlDocument xmldoc = new XmlDocument(); 
     xmldoc.InnerXml = xmltext.Trim(); 

     // Parse the XML into the iTextSharp document. 
     ITextHandler textHandler = new ITextHandler(doc); 
     textHandler.Parse(xmldoc); 
     */ 
     #endregion 

     #region This code works with iTextSharp version 5.0.5 (not free version of iTextSharp) 
     HTMLWorker htmlWorker = new HTMLWorker(doc); 
     StringReader reader = new StringReader(xmltext); 
     htmlWorker.Parse(reader); 
     #endregion 

     // Close and get the resulted binary data. 
     doc.Close(); 
     byte[] buf = new byte[memStream.Position]; 
     memStream.Position = 0; 
     memStream.Read(buf, 0, buf.Length); 

     // Send the binary data to the browser. 
     return new BinaryContentResult(buf, "application/pdf"); 
    } 
    #endregion 

選項2代碼: 此代碼的視圖(CSHTML)文件:

@model SomeModelSpecificToMyPurpose 
<?xml version="1.0" encoding="UTF-8" ?> 
<itext creationdate="@DateTime.Today" producer="iTextSharpXML"> 
<paragraph leading="18.0" font="unknown" size="16.0" align="Default"> 
    <chunk>RTPA Result in PDF</chunk> 
</paragraph> 
<paragraph leading="18.0" font="unknown" size="16.0" align="Default"> 
    <chunk>@DateTime.Today</chunk> 
</paragraph> 
<paragraph leading="18.0" font="unknown" size="10.0" align="Default"> 
    <chunk>Customer Name: @this.Model.Transaction.PatientFirstName</chunk><newline /> 
    <chunk>Address: @this.Model.Transaction.ProviderFullName</chunk><newline /> 
</paragraph> 
<paragraph leading="18.0" font="unknown" size="10.0" align="Default"> 
<chunk font="unknown" size="12.0">Orders:</chunk><newline /> 
</paragraph> 

回答

1

生命週期的形式和iText的不來那麼好。它會得到/設定值,但就是這樣。

您可以通過在代碼中爲每個用戶重新生成表單來做類似的事情,但這是一項不重要的任務。

相關問題