C#ASP.NET MVC是否从视图Rotativa或iTextSharp创建PDF? [英] C# ASP.NET MVC Create PDF from view Rotativa or iTextSharp?
问题描述
我不熟悉从多种形式生成PDF. 实际上,我从未使用任何一种编程语言生成PDF.现在,我一直在尝试两种我想做的方法.不幸的是几天后,我仍然没有发现如何做到这一点.
I am very unfamiliar with generating PDF from multiple forms. In fact, I've never generated a PDF using any kind of programming language. Now I have been trying 2 different methods for what I want to do. Unfortunately after a few days I still haven't found out how to do this.
我有多个表单,这些表单中的数据已预先填充在使用3个不同列表的表单中,现在我想生成此表单的PDF,为此我一直在使用Rotativa.不幸的是,一旦我调用了生成PDF的方法,它就会生成视图的PDF,但是数据丢失了.有了iTextSharp,我可以获得1个列表的数据,而不是全部,而且我也不知道是否可以使用iTextSharp将PDF保存在服务器上.
I have multiple forms with data pre-filled in those using 3 different lists, now I want to generate a PDF of this form and I have been using Rotativa for this. Unfortunately, once I call the method to generate the PDF, it generates a PDF of the view, but the data is missing. With iTextSharp I could have the data of 1 single list, not all of them, also I dont know if it is possible to save this PDF on the server using iTextSharp.
Q1:我应该使用哪种方法?你有什么建议?使用Rotativa或iTextSharp?
Q1: What method should I use? What do you suggest? using Rotativa or iTextSharp?
Q2:这对没有太多编程经验的人来说是否可行,还是我应该尝试寻找另一种方法?我
Q2: Is this doable for someone without much experience in programming or should I try to find another way? I
Q3:我是否仍然缺少执行此操作的方法?我希望不使用第三方软件的方法.
Q3: Am I still missing a way to do this? I would prefer a method without using 3rd party software.
推荐答案
公共字节[] GetPDF(字符串pHTML){ byte [] bPDF = null;
public byte[] GetPDF(string pHTML) { byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 25, 25, 25, 25);
// 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
// 3: we create a worker parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);
// 4: we open document and start the worker on the document
doc.Open();
htmlWorker.StartDocument();
// 5: parse the html into the document
htmlWorker.Parse(txtReader);
// 6: close the document and the worker
htmlWorker.EndDocument();
htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
return bPDF;
}
这篇关于C#ASP.NET MVC是否从视图Rotativa或iTextSharp创建PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!