C# ASP.NET MVC Create PDF from view Rotativa or iTextSharp? [closed]

人盡茶涼 提交于 2019-12-25 05:13:18

问题


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.

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: What method should I use? What do you suggest? using Rotativa or iTextSharp?

Q2: Is this doable for someone without much experience in programming or should I try to find another way? I

Q3: Am I still missing a way to do this? I would prefer a method without using 3rd party software.


回答1:


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;

}




回答2:


Use ITextSharp its a .NET PDF library which allows you to generate PDF (Portable Document Format).

So first of all need to add reference of iTextSharp in your project.

You can get iTextSharp reference using package manager, you just need to execute following command to download and add reference.

PM> Install-Package iTextSharp

Now you need to create a method which will give you byte array of PDF content, so our code will be



来源:https://stackoverflow.com/questions/43596769/c-sharp-asp-net-mvc-create-pdf-from-view-rotativa-or-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!