我有一个MVC3应用程序(C#,razor),其中包含一个对象,该对象具有我在用户浏览器中查看的动态PDF报告所需的所有数据。我的公司VS2010解决方案已经引用了iTextSharp.dll v 5.0.5,并在其他地方用于在静态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.使用我现在拥有的(仅适用于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>
答案 0 :(得分:1)
LifeCycle表格和iText并不能很好地相处。它会得到/设定值,但这就是它。
您可以通过在每个用户的代码中重新生成表单来执行类似的操作,但这是一项非常重要的任务。