2011-10-06 29 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的不来那么好。它会得到/设定值,但就是这样。

您可以通过在代码中为每个用户重新生成表单来做类似的事情,但这是一项不重要的任务。

相关问题